Skip to content

Commit 717bee0

Browse files
committed
Allow running scheduled jobs immediately
This simply schedules the jobs to be run now.
1 parent 367efb1 commit 717bee0

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

app/controllers/mission_control/jobs/discards_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def create
88

99
private
1010
def jobs_relation
11-
ActiveJob.jobs.failed
11+
ActiveJob.jobs
1212
end
1313

1414
def redirect_location

app/controllers/mission_control/jobs/dispatches_controller.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ class MissionControl::Jobs::DispatchesController < MissionControl::Jobs::Applica
33

44
def create
55
@job.dispatch
6-
redirect_to application_jobs_url(@application, :blocked), notice: "Dispatched job with id #{@job.job_id}"
6+
redirect_to redirect_location
77
end
88

99
private
10-
def jobs_relation
11-
ApplicationJob.jobs.blocked
12-
end
10+
def jobs_relation
11+
ActiveJob.jobs
12+
end
13+
14+
def redirect_location
15+
status = @job.status.presence_in(supported_job_statuses) || :blocked
16+
application_jobs_url(@application, status)
17+
end
1318
end
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div class="buttons is-right">
2-
<%= button_to "Dispatch", application_job_dispatch_path(@application, job.job_id), class: "button is-warning is-light mr-0" %>
2+
<%= button_to "Run now", application_job_dispatch_path(@application, job.job_id), class: "button is-warning is-light mr-0" %>
33
</div>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<div class="buttons is-right">
2+
<%= button_to "Run now", application_job_dispatch_path(@application, job.job_id), class: "button is-warning is-light mr-0" %>
23
<%= button_to "Discard", application_job_discard_path(@application, job.job_id), class: "button is-danger is-light mr-0",
34
form: { data: { turbo_confirm: "This will delete the job and can't be undone. Are you sure?" } } %>
45
</div>

lib/active_job/executing.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def discard
2525
end
2626

2727
def dispatch
28-
ActiveJob.jobs.blocked.dispatch_job(self)
28+
ActiveJob.jobs.dispatch_job(self)
2929
end
3030

3131
private

lib/active_job/jobs_relation.rb

+5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ def discard_job(job)
154154
end
155155

156156
# Dispatch the provided job.
157+
#
158+
# This operation is only valid for blocked or scheduled jobs. It will
159+
# raise an error +ActiveJob::Errors::InvalidOperation+ otherwise.
157160
def dispatch_job(job)
161+
raise ActiveJob::Errors::InvalidOperation, "This operation can only be performed on blocked or scheduled jobs, but this job is #{job.status}" unless job.blocked? || job.scheduled?
162+
158163
queue_adapter.dispatch_job(job, self)
159164
end
160165

lib/active_job/queue_adapters/solid_queue_ext.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,13 @@ def execution_error_from_solid_queue_job(solid_queue_job)
123123
end
124124

125125
def dispatch_immediately(job)
126-
SolidQueue::Job.transaction do
127-
job.dispatch_bypassing_concurrency_limits
128-
job.blocked_execution.destroy!
126+
if job.blocked?
127+
SolidQueue::Job.transaction do
128+
job.dispatch_bypassing_concurrency_limits
129+
job.blocked_execution.destroy!
130+
end
131+
else
132+
job.scheduled_execution.update!(scheduled_at: Time.now)
129133
end
130134
end
131135

test/dummy/db/seeds.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def clean_database
1010
end
1111

1212
class JobsLoader
13-
attr_reader :application, :server, :failed_jobs_count, :pending_jobs_count, :finished_jobs_count, :blocked_jobs_count
13+
attr_reader :application, :server, :failed_jobs_count, :pending_jobs_count, :finished_jobs_count, :blocked_jobs_count, :scheduled_jobs_count
1414

1515
def initialize(application, server, failed_jobs_count: 100, pending_jobs_count: 50)
1616
@application = application
@@ -19,6 +19,7 @@ def initialize(application, server, failed_jobs_count: 100, pending_jobs_count:
1919
@pending_jobs_count = randomize(pending_jobs_count)
2020
@finished_jobs_count = randomize(pending_jobs_count)
2121
@blocked_jobs_count = randomize(pending_jobs_count)
22+
@scheduled_jobs_count = randomize(pending_jobs_count)
2223
end
2324

2425
def load
@@ -27,6 +28,7 @@ def load
2728
load_failed_jobs
2829
load_pending_jobs
2930
load_blocked_jobs
31+
load_scheduled_jobs
3032
load_recurring_tasks
3133
end
3234
end
@@ -64,6 +66,15 @@ def load_blocked_jobs
6466
end
6567
end
6668

69+
def load_scheduled_jobs
70+
return unless supported_status?(:scheduled)
71+
72+
puts "Generating #{scheduled_jobs_count} scheduled jobs for #{application} - #{server}..."
73+
scheduled_jobs_count.times do |index|
74+
DummyJob.set(wait: randomize(60).minutes).perform_later(index)
75+
end
76+
end
77+
6778
def load_recurring_tasks
6879
return unless server.queue_adapter.supports_recurring_tasks?
6980

0 commit comments

Comments
 (0)