lib/good_job/cli.rb in good_job-1.0.3 vs lib/good_job/cli.rb in good_job-1.1.0

- old
+ new

@@ -14,37 +14,33 @@ desc: "Queues to work from. Separate multiple queues with commas (default: *)" method_option :poll_interval, type: :numeric, desc: "Interval between polls for available jobs in seconds (default: 1)" def start - require RAILS_ENVIRONMENT_RB + set_up_application! max_threads = ( options[:max_threads] || ENV['GOOD_JOB_MAX_THREADS'] || ENV['RAILS_MAX_THREADS'] || ActiveRecord::Base.connection_pool.size ).to_i - queue_names = ( + queue_string = ( options[:queues] || ENV['GOOD_JOB_QUEUES'] || '*' - ).split(',').map(&:strip) + ) poll_interval = ( options[:poll_interval] || ENV['GOOD_JOB_POLL_INTERVAL'] ).to_i - job_query = GoodJob::Job.all.priority_ordered - queue_names_without_all = queue_names.reject { |q| q == '*' } - job_query = job_query.where(queue_name: queue_names_without_all) unless queue_names_without_all.size.zero? - job_performer = GoodJob::Performer.new(job_query, :perform_with_advisory_lock) + job_query = GoodJob::Job.queue_string(queue_string) + job_performer = GoodJob::Performer.new(job_query, :perform_with_advisory_lock, name: queue_string) - $stdout.puts "GoodJob worker starting with max_threads=#{max_threads} on queues=#{queue_names.join(',')}" - timer_options = {} timer_options[:execution_interval] = poll_interval if poll_interval.positive? pool_options = { max_threads: max_threads, @@ -60,26 +56,33 @@ Kernel.loop do sleep 0.1 break if @stop_good_job_executable || scheduler.shutdown? end - $stdout.puts "\nFinishing GoodJob's current jobs before exiting..." scheduler.shutdown - $stdout.puts "GoodJob's jobs finished, exiting..." end + default_task :start + desc :cleanup_preserved_jobs, "Delete preserved job records" method_option :before_seconds_ago, type: :numeric, default: 24 * 60 * 60, desc: "Delete records finished more than this many seconds ago" def cleanup_preserved_jobs - require RAILS_ENVIRONMENT_RB + set_up_application! timestamp = Time.current - options[:before_seconds_ago] - result = GoodJob::Job.finished(timestamp).delete_all - $stdout.puts "Deleted #{result} preserved #{'job'.pluralize(result)} finished before #{timestamp}." + ActiveSupport::Notifications.instrument("cleanup_preserved_jobs.good_job", { before_seconds_ago: options[:before_seconds_ago], timestamp: timestamp }) do |payload| + deleted_records_count = GoodJob::Job.finished(timestamp).delete_all + + payload[:deleted_records_count] = deleted_records_count + end end - default_task :start + no_commands do + def set_up_application! + require RAILS_ENVIRONMENT_RB + end + end end end