diff --git a/spec/app/jobs/example_job_spec.rb b/spec/app/jobs/example_job_spec.rb new file mode 100644 index 000000000..9312d902b --- /dev/null +++ b/spec/app/jobs/example_job_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true +require 'rails_helper' + +describe ExampleJob do + before do + allow(GoodJob).to receive(:preserve_job_records).and_return(true) + ActiveJob::Base.queue_adapter = GoodJob::Adapter.new(execution_mode: :inline) + end + + describe "#perform" do + describe ":success" do + it 'completes successfully' do + active_job = described_class.perform_later(:success) + good_job = GoodJob::Job.find(active_job.provider_job_id) + expect(good_job.error).to be_nil + end + end + + describe ":error_once" do + it 'errors once then succeeds' do + active_job = described_class.perform_later(:error_once) + good_jobs = GoodJob::Job.where(active_job_id: active_job.job_id).order(created_at: :asc) + expect(good_jobs.size).to eq 2 + expect(good_jobs.last.error).to be_nil + end + end + + describe ":error_five_times" do + it 'errors five times then succeeds' do + active_job = described_class.perform_later(:error_five_times) + good_jobs = GoodJob::Job.where(active_job_id: active_job.job_id).order(created_at: :asc) + expect(good_jobs.size).to eq 6 + expect(good_jobs.last.error).to be_nil + end + end + + describe ":dead" do + it 'errors but does not retry' do + active_job = described_class.perform_later(:dead) + good_jobs = GoodJob::Job.where(active_job_id: active_job.job_id).order(created_at: :asc) + expect(good_jobs.size).to eq 3 + expect(good_jobs.last.error).to be_present + end + end + end +end diff --git a/spec/test_app/app/jobs/cleanup_job.rb b/spec/test_app/app/jobs/cleanup_job.rb index bae2ef412..9a9b80031 100644 --- a/spec/test_app/app/jobs/cleanup_job.rb +++ b/spec/test_app/app/jobs/cleanup_job.rb @@ -1,6 +1,10 @@ class CleanupJob < ApplicationJob - def perform(limit: 5_000) - earliest = GoodJob::Job.finished.order(created_at: :desc).limit(limit).last.created_at - GoodJob::Job.where("created_at < ?", earliest).delete_all + self.queue_name = :cleanup + + def perform(limit = 2_000) + earliest_job_to_preserve = GoodJob::Job.finished.order(created_at: :desc).limit(limit).last + return if earliest_job_to_preserve.blank? + + GoodJob::Job.where("created_at < ?", earliest_job_to_preserve.created_at).delete_all end end diff --git a/spec/test_app/app/jobs/example_job.rb b/spec/test_app/app/jobs/example_job.rb index 276771296..2628971d9 100644 --- a/spec/test_app/app/jobs/example_job.rb +++ b/spec/test_app/app/jobs/example_job.rb @@ -1,4 +1,18 @@ class ExampleJob < ApplicationJob - def perform(*) + ExpectedError = Class.new(StandardError) + DeadError = Class.new(StandardError) + + retry_on DeadError, attempts: 3 + + def perform(type = :success) + if type == :success + true + elsif type == :error_once + raise(ExpectedError, "Executed #{executions} #{"time".pluralize(executions)}.") if executions < 2 + elsif type == :error_five_times + raise(ExpectedError, "Executed #{executions} #{"time".pluralize(executions)}.") if executions < 6 + elsif type == :dead + raise DeadError + end end end diff --git a/spec/test_app/config/environments/demo.rb b/spec/test_app/config/environments/demo.rb index 5a4c247c4..72a81496d 100644 --- a/spec/test_app/config/environments/demo.rb +++ b/spec/test_app/config/environments/demo.rb @@ -12,12 +12,15 @@ config.good_job.cron = { frequent_example: { description: "Enqueue an ExampleJob with a random sample of configuration", - cron: "*/5 * * * * *", # every 5 seconds + cron: "* * * * * *", class: "ExampleJob", - args: [], + args: (lambda do + type = [:success, :error_once, :error_five_times, :dead].sample + [type] + end), set: (lambda do queue = [:default, :elephants, :mice].sample - delay = (0..60).to_a.sample + delay = [0, (0..60).to_a.sample].sample priority = [-10, 0, 10].sample { wait: delay, queue: queue, priority: priority } @@ -25,22 +28,14 @@ }, other_example: { description: "Enqueue an OtherJob occasionally", - cron: "* * * * * *", # every second + cron: "*/15 * * * * *", class: "OtherJob", set: { queue: :default }, }, - fragile_example: { - description: "Enqueue a FragileJob occasionally", - cron: "* * * * * *", # every second - class: "FragileJob", - set: { queue: :default }, - }, cleanup: { - description: "Delete old jobs every hour", - cron: "0 * * * *", # every hour + description: "Delete old jobs.", + cron: "*/15 * * * *", class: "CleanupJob", - set: { queue: :default }, - args: { limit: 1000 }, } } end