<commit_before>require 'resque'
require 'clamp'

module JobCo
  module Commands
    class Resque < Clamp::Command
      subcommand "run_worker", "forks a worker in the background" do
        option(["-q", "--quiet"], :flag, "Be quiet", :default => false)
        option(["-v", "--verbose"], :flag, "Be verbose", :default => false)
        option(["-i", "--interval"],
               "INTERVAL", "Interval at which ",
               :default => (ENV['INTERVAL'] || 5).to_i)
        option(["-b", "--background"], :flag,
               "XXX BACKGROUND DOC",
               :default => ENV['BACKGROUND'] || false)
        option(["-Q", "--queues"],
               "QUEUES", "Job queue(s) the worker is serving.",
               :default => (ENV['QUEUES'] || ENV['QUEUE'] || "*"))
        option(["-p", "--pidfile"],
               "PIDFILE", "XXX PIDFILE DOC",
               :default => ENV['PIDFILE'])
        option(["-k", "--kill"], :flag,
               "Use `resque` to kill any running worker",
               :default => false)

        def queues= v;  @queues = v.to_s.split(','); end

        def execute
          require 'resque/worker'

          require 'jobco/jobs'
          JobCo::Jobs::load_available_jobs

          if kill?
            ids = `resque list`
            if ids != "None\n"
              ids.split("\n").map { |x| x.split(" ").first }.each do |id|
                STDOUT << "resque kill #{id}"
                `resque kill #{id}`
                STDOUT << "  ok (#{$?}).\n"
              end
            end
          end

          JobCo::Job::require_rails if JobCo::Config.require_rails == :once

          if background?
            abort "background requires ruby >= 1.9" unless Process.respond_to?('daemon')
            Process.daemon(true)
          end

          worker = ::Resque::Worker.new(queues)
          worker.verbose = !quiet?
          worker.very_verbose = verbose?
          worker.work(interval)
        end
      end

      subcommand "run_scheduler", "forks a resque::scheduler process" do
        option(["-b", "--background"],
              :flag, "XXX BACKGROUND DOC",
              :default => ENV['BACKGROUND'] || false)
        option(["-d", "--dynamic-schedule"],
              :flag, "Allow schedule manipulation at runtime",
              :default => ENV['DYNAMIC_SCHEDULE'] || false)
        option(["-q", "--quiet"], :flag, "Be quiet", :default => false)

        def execute
          require 'resque_scheduler'
          require 'resque/scheduler'
          ::Resque::Scheduler::dynamic = true

          if background?
            abort "background requires ruby >= 1.9" unless Process.respond_to?('daemon')
            Process.daemon(true)
          end

          ::Resque::Scheduler.dynamic = dynamic_schedule?
          ::Resque::Scheduler.verbose = !quiet?<commit_msg>resque-scheduler needs to load job code so that it can queue jobs<commit_after>require 'resque'
require 'clamp'

module JobCo
  module Commands
    class Resque < Clamp::Command
      subcommand "run_worker", "forks a worker in the background" do
        option(["-q", "--quiet"], :flag, "Be quiet", :default => false)
        option(["-v", "--verbose"], :flag, "Be verbose", :default => false)
        option(["-i", "--interval"],
               "INTERVAL", "Interval at which ",
               :default => (ENV['INTERVAL'] || 5).to_i)
        option(["-b", "--background"], :flag,
               "XXX BACKGROUND DOC",
               :default => ENV['BACKGROUND'] || false)
        option(["-Q", "--queues"],
               "QUEUES", "Job queue(s) the worker is serving.",
               :default => (ENV['QUEUES'] || ENV['QUEUE'] || "*"))
        option(["-p", "--pidfile"],
               "PIDFILE", "XXX PIDFILE DOC",
               :default => ENV['PIDFILE'])
        option(["-k", "--kill"], :flag,
               "Use `resque` to kill any running worker",
               :default => false)

        def queues= v;  @queues = v.to_s.split(','); end

        def execute
          require 'resque/worker'

          require 'jobco/jobs'
          JobCo::Jobs::load_available_jobs

          if kill?
            ids = `resque list`
            if ids != "None\n"
              ids.split("\n").map { |x| x.split(" ").first }.each do |id|
                STDOUT << "resque kill #{id}"
                `resque kill #{id}`
                STDOUT << "  ok (#{$?}).\n"
              end
            end
          end

          JobCo::Job::require_rails if JobCo::Config.require_rails == :once

          if background?
            abort "background requires ruby >= 1.9" unless Process.respond_to?('daemon')
            Process.daemon(true)
          end

          worker = ::Resque::Worker.new(queues)
          worker.verbose = !quiet?
          worker.very_verbose = verbose?
          worker.work(interval)
        end
      end

      subcommand "run_scheduler", "forks a resque::scheduler process" do
        option(["-b", "--background"],
              :flag, "XXX BACKGROUND DOC",
              :default => ENV['BACKGROUND'] || false)
        option(["-d", "--dynamic-schedule"],
              :flag, "Allow schedule manipulation at runtime",
              :default => ENV['DYNAMIC_SCHEDULE'] || true)
        option(["-q", "--quiet"], :flag, "Be quiet", :default => false)

        def execute
          require 'resque_scheduler'
          require 'resque/scheduler'

          require 'jobco/jobs'
          JobCo::Jobs::load_available_jobs

          if background?
            abort "background requires ruby >= 1.9" unless Process.respond_to?('daemon')
            Process.daemon(true)
          end

          ::Resque::Scheduler.dynamic = dynamic_schedule?
          ::Resque::Scheduler.verbose = !quiet?
{'fraction_non_alphanumeric': 0.15636931620964942, 'fraction_numerical': 0.0017355085039916696, 'mean_word_length': 1.829160530191458, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '3281630', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1601, 'n_words': 423}