Skip to content

Commit

Permalink
Merge pull request #65 from garyhtou-forks/garyhtou/add-scheduled-job…
Browse files Browse the repository at this point in the history
…-delay-threshold-configuration

Add `scheduled_job_delay_threshold` configuration
  • Loading branch information
rosa authored Mar 2, 2024
2 parents a82578a + d3ee481 commit c39d4c4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Besides `base_controller_class`, you can also set the following for `MissionCont
- `delay_between_bulk_operation_batches`: how long to wait between batches when performing bulk operations, such as _discard all_ or _retry all_ jobs—defaults to `0`
- `adapters`: a list of adapters that you want Mission Control to use and extend. By default this will be the adapter you have set for `active_job.queue_adapter`.
- `internal_query_count_limit`: in count queries, the maximum number of records that will be counted if the adapter needs to limit these queries. True counts above this number will be returned as `INFINITY`. This keeps count queries fast—defaults to `500,000`
- `scheduled_job_delay_threshold`: the time duration before a scheduled job is considered delayed. Defaults to `1.minute` (a job is considered delayed if it hasn't transitioned from the `scheduled` status 1 minute after the scheduled time).

This library extends Active Job with a querying interface and the following setting:
- `config.active_job.default_page_size`: the internal batch size that Active Job will use when sending queries to the underlying adapter and the batch size for the bulk operations defined above—defaults to `1000`.
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/mission_control/jobs/jobs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def attribute_names_for_job_status(status)
end
end

def job_delayed?(job)
job.scheduled_at.before?(MissionControl::Jobs.scheduled_job_delay_threshold.ago)
end

private
def renderable_job_arguments_for(job)
job.serialized_arguments.collect do |argument|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<td><%= link_to job.queue_name, application_queue_path(@application, job.queue) %></td>
<td>
<%= bidirectional_time_distance_in_words_with_title(job.scheduled_at) %>
<% if job.scheduled_at.before? 1.minute.ago %>
<% if job_delayed?(job) %>
<div class="is-danger tag ml-4">delayed</div>
<% end %>
</td>
Expand Down
1 change: 1 addition & 0 deletions lib/mission_control/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module Jobs
mattr_accessor :logger, default: ActiveSupport::Logger.new(nil)
mattr_accessor :internal_query_count_limit, default: 500_000 # Hard limit to keep unlimited count queries fast enough
mattr_accessor :show_console_help, default: true
mattr_accessor :scheduled_job_delay_threshold, default: 1.minute
end
end
19 changes: 19 additions & 0 deletions test/controllers/jobs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ class MissionControl::Jobs::JobsControllerTest < ActionDispatch::IntegrationTest
assert_select "tr.job", /DummyJob\s+Enqueued 2 minutes ago\s+queue_1\s+(1 minute ago|less than a minute ago)/
assert_select "tr.job", /Discard/
end

test "get scheduled jobs when some are delayed" do
DummyJob.set(wait: 5.minutes).perform_later(37)
DummyJob.set(wait: 7.minutes).perform_later(42)

get mission_control_jobs.application_jobs_url(@application, :scheduled)
assert_response :ok
assert_select "tr.job", 2 do # lists two jobs
assert_select "div.tag", text: /delayed/, count: 0 # no delayed tag
end

travel 5.minutes + MissionControl::Jobs.scheduled_job_delay_threshold + 1.second

get mission_control_jobs.application_jobs_url(@application, :scheduled)
assert_response :ok
assert_select "tr.job", 2 do # lists two jobs
assert_select "div.tag", text: /delayed/, count: 1 # total of one delayed tag
end
end
end

0 comments on commit c39d4c4

Please sign in to comment.