<commit_before>
# https://gist.github.com/208581
# http://unicorn.bogomips.org/SIGNALS.html

God.watch do |w|
  w.dir = RAILS_ROOT
  w.name = "puma-bixby"
  w.group = "bixby"
  w.log = "#{RAILS_ROOT}/log/god.#{w.name}.log"
  w.pid_file = "#{RAILS_ROOT}/tmp/pids/puma.pid"

  w.interval = 30.seconds # default

  pumactl = "#{RVM_WRAPPER} bundle exec pumactl -F #{RAILS_ROOT}/config/deploy/puma.conf.rb"
  w.start = "#{pumactl} start"
  w.stop = "#{pumactl} stop"

  # cause the master to re-create itself and spawn a new worker process
  w.restart = "#{pumactl} restart"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds

  w.uid = USER
  w.gid = GROUP

  w.behavior(:clean_pid_file)

  # determine the state on startup
  w.transition(:init, { true => :up, false => :start }) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end
  end

  # determine when process has finished starting
  w.transition([:start, :restart], :up) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end

    # failsafe
    on.condition(:tries) do |c|
      c.times = 8
      c.within = 2.minutes
      c.transition = :start
    end
  end

  # start if process is not running
  w.transition(:up, :start) do |on|
    on.condition(:process_exits)
  end

  # restart if memory or cpu is too high or http error on /
  w.transition(:up, :restart) do |on|
    on.condition(:memory_usage) do |c|
      c.interval = 30
      c.above = 150.megabytes
      c.times = [3, 5] # 3 out of 5
    end

    on.condition(:cpu_usage) do |c|
      c.interval = 20
      c.above = 50.percent
      c.times = 5
    end

    on.condition(:http_response_code) do |c|
      c.host = 'localhost'
      c.port = '8080'
      c.path = '/rack_health'
      c.code_is_not = 200
      c.timeout = 10.seconds
      c.times = [3, 5] # 3 out of 5
    end
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end


# This will ride alongside god and kill any rogue memory-greedy
# processes. Their sacrifice is for the greater good.

worker_memory_limit = 300_000

Thread.new do
  loop do
    begin
      # unicorn workers
      #
      # ps output line format:
      # 31580 275444 unicorn_rails worker[15] -c /data/github/current/config/unicorn.rb -E production -D
      # pid ram command

      lines = `ps -e -www -o pid,rss,command | grep '[u]nicorn worker'`.split("\n")
      lines.each do |line|
        parts = line.split(' ')
        if parts[1].to_i > worker_memory_limit
          # tell the worker to die after it finishes serving its request
          ::Process.kill('QUIT', parts[0].to_i)
        end
      end
    rescue Object
      # don't die ever once we've tested this
      nil
    end

    sleep 30
  end
end<commit_msg>raise mem limit to 250mb<commit_after>
# https://gist.github.com/208581
# http://unicorn.bogomips.org/SIGNALS.html

God.watch do |w|
  w.dir = RAILS_ROOT
  w.name = "puma-bixby"
  w.group = "bixby"
  w.log = "#{RAILS_ROOT}/log/god.#{w.name}.log"
  w.pid_file = "#{RAILS_ROOT}/tmp/pids/puma.pid"

  w.interval = 30.seconds # default

  pumactl = "#{RVM_WRAPPER} bundle exec pumactl -F #{RAILS_ROOT}/config/deploy/puma.conf.rb"
  w.start = "#{pumactl} start"
  w.stop = "#{pumactl} stop"

  # cause the master to re-create itself and spawn a new worker process
  w.restart = "#{pumactl} restart"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds

  w.uid = USER
  w.gid = GROUP

  w.behavior(:clean_pid_file)

  # determine the state on startup
  w.transition(:init, { true => :up, false => :start }) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end
  end

  # determine when process has finished starting
  w.transition([:start, :restart], :up) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end

    # failsafe
    on.condition(:tries) do |c|
      c.times = 8
      c.within = 2.minutes
      c.transition = :start
    end
  end

  # start if process is not running
  w.transition(:up, :start) do |on|
    on.condition(:process_exits)
  end

  # restart if memory or cpu is too high or http error on /
  w.transition(:up, :restart) do |on|
    on.condition(:memory_usage) do |c|
      c.interval = 30
      c.above = 250.megabytes
      c.times = [3, 5] # 3 out of 5
    end

    on.condition(:cpu_usage) do |c|
      c.interval = 20
      c.above = 50.percent
      c.times = 5
    end

    on.condition(:http_response_code) do |c|
      c.host = 'localhost'
      c.port = '8080'
      c.path = '/rack_health'
      c.code_is_not = 200
      c.timeout = 10.seconds
      c.times = [3, 5] # 3 out of 5
    end
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end
{'fraction_non_alphanumeric': 0.12633004449603405, 'fraction_numerical': 0.023215322112594312, 'mean_word_length': 2.3724722765818655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, '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': '2329484', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1900, 'n_words': 501}