<commit_before>if _G.js then
  error "Not available"
end

return function (loader)

  local Configuration = loader.load "cosy.configuration"
  local File          = loader.load "cosy.file"
  local Scheduler     = loader.load "cosy.scheduler"
  local Socket        = loader.load "cosy.socket"
  local Rawsocket     = loader.require "socket"
  local Redis_Client  = loader.require "redis"
  local Posix         = loader.require "posix"

  Configuration.load {
    "cosy.redis",
  }

  local assigned = {}

  local Redis = {}

  local configuration_template = [[
  ################################ GENERAL  #####################################
  daemonize       yes
  pidfile         {{{pid}}}
  bind            {{{interface}}}
  port            {{{port}}}
  timeout         0
  tcp-keepalive   60
  loglevel        notice
  logfile         "{{{log}}}"
  databases       1

  ################################ SNAPSHOTTING  ################################
  save            3600  1
  rdbcompression  yes
  rdbchecksum     yes
  dbfilename      "{{{db}}}"
  dir             "{{{home}}}"

  ############################## APPEND ONLY MODE ###############################
  appendonly      yes
  appendfilename  "{{{append}}}"
  appendfsync     everysec
  ]]

  function Redis.configure ()
    if Configuration.redis.port == 0 then
      local server  = Rawsocket.bind ("*", 0)
      local _, port = server:getsockname ()
      server:close ()
      Configuration.redis.port = port
    end
    local configuration = configuration_template % {
      home      = loader.home,
      pid       = Configuration.redis.pid,
      interface = Configuration.redis.interface,
      port      = Configuration.redis.port,
      log       = Configuration.redis.log,
      db        = Configuration.redis.db,
      append    = Configuration.redis.append,
    }
    print ("REDIS CONFIG", configuration)
    local file = assert (io.open (Configuration.redis.configuration, "w"))
    file:write (configuration)
    file:close ()
  end

  function Redis.start ()
    Redis.stop      ()
    Redis.configure ()
    if Posix.fork () == 0 then
      Posix.execp ("redis-server", {
        Configuration.redis.configuration,
      })
    end
    File.encode (Configuration.redis.data, {
      interface = Configuration.redis.interface,
      port      = Configuration.redis.port,
    })
    Posix.chmod (Configuration.redis.data, "0600")
    local i = 0
    repeat
      i = i+1
      Posix.nanosleep (0, 100000) -- sleep 100ms
      local socket = Rawsocket.tcp ()
      socket:connect (Configuration.redis.interface, Configuration.redis.port)
      local client = Redis_Client.connect {
        socket = socket,
      }
      local ok, err = pcall (client.ping, client)
      print (ok, err)
    until ok or i >= 10
  end

  local function getpid ()
    local pid_file = io.open (Configuration.redis.pid, "r")
    if pid_file then
      local pid = pid_file:read "*a"
      pid_file:close ()
      return pid:match "%S+"
    end
  end

  function Redis.stop ()
    local pid = getpid ()<commit_msg>Remove redis debug.<commit_after>if _G.js then
  error "Not available"
end

return function (loader)

  local Configuration = loader.load "cosy.configuration"
  local File          = loader.load "cosy.file"
  local Scheduler     = loader.load "cosy.scheduler"
  local Socket        = loader.load "cosy.socket"
  local Rawsocket     = loader.require "socket"
  local Redis_Client  = loader.require "redis"
  local Posix         = loader.require "posix"

  Configuration.load {
    "cosy.redis",
  }

  local assigned = {}

  local Redis = {}

  local configuration_template = [[
  ################################ GENERAL  #####################################
  daemonize       yes
  pidfile         {{{pid}}}
  bind            {{{interface}}}
  port            {{{port}}}
  timeout         0
  tcp-keepalive   60
  loglevel        notice
  logfile         "{{{log}}}"
  databases       1

  ################################ SNAPSHOTTING  ################################
  save            3600  1
  rdbcompression  yes
  rdbchecksum     yes
  dbfilename      "{{{db}}}"
  dir             "{{{home}}}"

  ############################## APPEND ONLY MODE ###############################
  appendonly      yes
  appendfilename  "{{{append}}}"
  appendfsync     everysec
  ]]

  function Redis.configure ()
    if Configuration.redis.port == 0 then
      local server  = Rawsocket.bind ("*", 0)
      local _, port = server:getsockname ()
      server:close ()
      Configuration.redis.port = port
    end
    local configuration = configuration_template % {
      home      = loader.home,
      pid       = Configuration.redis.pid,
      interface = Configuration.redis.interface,
      port      = Configuration.redis.port,
      log       = Configuration.redis.log,
      db        = Configuration.redis.db,
      append    = Configuration.redis.append,
    }
    local file = assert (io.open (Configuration.redis.configuration, "w"))
    file:write (configuration)
    file:close ()
  end

  function Redis.start ()
    Redis.stop      ()
    Redis.configure ()
    if Posix.fork () == 0 then
      Posix.execp ("redis-server", {
        Configuration.redis.configuration,
      })
    end
    File.encode (Configuration.redis.data, {
      interface = Configuration.redis.interface,
      port      = Configuration.redis.port,
    })
    Posix.chmod (Configuration.redis.data, "0600")
    local i = 0
    repeat
      i = i+1
      Posix.nanosleep (0, 100000) -- sleep 100ms
      local socket = Rawsocket.tcp ()
      socket:connect (Configuration.redis.interface, Configuration.redis.port)
      local client = Redis_Client.connect {
        socket = socket,
      }
      local ok = pcall (client.ping, client)
    until ok or i >= 10
  end

  local function getpid ()
    local pid_file = io.open (Configuration.redis.pid, "r")
    if pid_file then
      local pid = pid_file:read "*a"
      pid_file:close ()
      return pid:match "%S+"
    end
  end

  function Redis.stop ()
    local pid = getpid ()
{'fraction_non_alphanumeric': 0.1630774289002137, 'fraction_numerical': 0.009863554167351636, 'mean_word_length': 2.3837597330367073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323993', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1763, 'n_words': 430}