Skip to content

Commit

Permalink
Load some recurring tasks as part of seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
rosa committed Nov 5, 2024
1 parent 4e31209 commit 8d29af2
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions test/dummy/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,47 @@ def clean_database
end

class JobsLoader
attr_reader :application, :server, :failed_jobs_count, :regular_jobs_count, :finished_jobs_count, :blocked_jobs_count
attr_reader :application, :server, :failed_jobs_count, :pending_jobs_count, :finished_jobs_count, :blocked_jobs_count

def initialize(application, server, failed_jobs_count: 100, regular_jobs_count: 50)
def initialize(application, server, failed_jobs_count: 100, pending_jobs_count: 50)
@application = application
@server = server
@failed_jobs_count = randomize(failed_jobs_count)
@regular_jobs_count = randomize(regular_jobs_count)
@finished_jobs_count = randomize(regular_jobs_count)
@blocked_jobs_count = randomize(regular_jobs_count)
@pending_jobs_count = randomize(pending_jobs_count)
@finished_jobs_count = randomize(pending_jobs_count)
@blocked_jobs_count = randomize(pending_jobs_count)
end

def load
server.activating do
load_finished_jobs
load_failed_jobs
load_regular_jobs
load_pending_jobs
load_blocked_jobs
load_recurring_tasks
end
end

private
def load_failed_jobs
puts "Generating #{failed_jobs_count} failed jobs for #{application} - #{server}..."
failed_jobs_count.times { |index| enqueue_one_of FailingJob => index, FailingReloadedJob => index, FailingPostJob => [ Post.last, 1.year.ago ] }
perform_jobs
end

def perform_jobs
case server.queue_adapter_name
when :resque
worker = Resque::Worker.new("*")
worker.work(0.0)
when :solid_queue
worker = SolidQueue::Worker.new(queues: "*", threads: 1, polling_interval: 0.01)
worker.mode = :inline
worker.start
else
raise "Don't know how to dispatch jobs for #{server.queue_adapter_name} adapter"
end
end

def load_finished_jobs
return unless supported_status?(:finished)

puts "Generating #{finished_jobs_count} finished jobs for #{application} - #{server}..."
regular_jobs_count.times do |index|
pending_jobs_count.times do |index|
enqueue_one_of DummyJob => index, DummyReloadedJob => index
end
perform_jobs
end

def load_regular_jobs
puts "Generating #{regular_jobs_count} regular jobs for #{application} - #{server}..."
regular_jobs_count.times do |index|
def load_failed_jobs
puts "Generating #{failed_jobs_count} failed jobs for #{application} - #{server}..."
failed_jobs_count.times { |index| enqueue_one_of FailingJob => index, FailingReloadedJob => index, FailingPostJob => [ Post.last, 1.year.ago ] }
perform_jobs
end

def load_pending_jobs
puts "Generating #{pending_jobs_count} pending jobs for #{application} - #{server}..."
pending_jobs_count.times do |index|
enqueue_one_of DummyJob => index, DummyReloadedJob => index
end
end
Expand All @@ -77,10 +64,41 @@ def load_blocked_jobs
end
end

def load_recurring_tasks
return unless server.queue_adapter.supports_recurring_tasks?

puts "Loading recurring tasks for #{application} - #{server}..."
create_recurring_tasks
end

def supported_status?(status)
server.queue_adapter.supported_job_statuses.include?(status)
end

def perform_jobs
case server.queue_adapter_name
when :resque
worker = Resque::Worker.new("*")
worker.work(0.0)
when :solid_queue
worker = SolidQueue::Worker.new(queues: "*", threads: 1, polling_interval: 0.01)
worker.mode = :inline
worker.start
else
raise "Don't know how to dispatch jobs for #{server.queue_adapter_name} adapter"
end
end

def create_recurring_tasks
case server.queue_adapter_name
when :solid_queue
SolidQueue::RecurringTask.from_configuration(:dummy_hourly, class: "DummyJob", args: [ 1000 ], schedule: "every hour").save!
SolidQueue::RecurringTask.from_configuration(:post_daily, command: "Post.create!(title: 'Hey')", schedule: "at 5am every day").save!
else
raise "Don't know how to dispatch recurring tasks for #{server.queue_adapter_name} adapter"
end
end

def with_random_queue(job_class)
random_queue = [ "background", "reports", "default", "realtime" ].sample
job_class.tap do
Expand Down Expand Up @@ -109,6 +127,6 @@ def randomize(value)

MissionControl::Jobs.applications.each do |application|
application.servers.each do |server|
JobsLoader.new(application, server, failed_jobs_count: BASE_COUNT, regular_jobs_count: BASE_COUNT / 2).load
JobsLoader.new(application, server, failed_jobs_count: BASE_COUNT, pending_jobs_count: BASE_COUNT / 2).load
end
end

0 comments on commit 8d29af2

Please sign in to comment.