Skip to content

Commit

Permalink
Support new recurring task features
Browse files Browse the repository at this point in the history
  • Loading branch information
rosa committed Sep 9, 2024
1 parent 7f71ec8 commit ca7f352
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
git_source(:bc) { |repo| "https://github.com/basecamp/#{repo}" }

# Specify your gem's dependencies in mission_control-jobs.gemspec.
gemspec

gem "solid_queue", github: "rails/solid_queue", branch: "improve-recurring-tasks-config"

gem "capybara", github: "teamcapybara/capybara"
23 changes: 15 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
GIT
remote: https://github.com/rails/solid_queue.git
revision: 8df600df2d22261cdce3e51e103c1cf58d4da391
branch: improve-recurring-tasks-config
specs:
solid_queue (0.8.2)
activejob (>= 7.1)
activerecord (>= 7.1)
concurrent-ruby (>= 1.3.1)
fugit (~> 1.11.0)
railties (>= 7.1)
thor (~> 1.3.1)

GIT
remote: https://github.com/teamcapybara/capybara.git
revision: c0cbf4024c1abd48b0c22c2930e7b05af58ab284
Expand Down Expand Up @@ -117,7 +130,7 @@ GEM
erubi (1.12.0)
et-orbi (1.2.11)
tzinfo
fugit (1.11.0)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
globalid (1.2.1)
Expand Down Expand Up @@ -294,12 +307,6 @@ GEM
rack-protection (= 4.0.0)
rack-session (>= 2.0.0, < 3)
tilt (~> 2.0)
solid_queue (0.5.0)
activejob (>= 7.1)
activerecord (>= 7.1)
concurrent-ruby (>= 1.3.1)
fugit (~> 1.11.0)
railties (>= 7.1)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -357,7 +364,7 @@ DEPENDENCIES
rubocop-performance
rubocop-rails-omakase
selenium-webdriver
solid_queue (>= 0.5)
solid_queue!
sprockets-rails
sqlite3

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This library extends Active Job with a querying interface and the following sett
## Adapter Specifics

- **Resque**: Queue pausing is supported only if you have `resque-pause` installed in your project
- **Solid Queue**: Requires version >= 0.5.
- **Solid Queue**: Requires version >= 0.9.

## Advanced configuration

Expand Down
2 changes: 1 addition & 1 deletion app/models/mission_control/jobs/recurring_task.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class MissionControl::Jobs::RecurringTask
include ActiveModel::Model

attr_accessor :id, :job_class_name, :arguments, :schedule, :last_enqueued_at
attr_accessor :id, :job_class_name, :command, :arguments, :schedule, :last_enqueued_at, :queue_name, :priority

def initialize(queue_adapter: ActiveJob::Base.queue_adapter, **kwargs)
@queue_adapter = queue_adapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
<table class="table">
<tbody>
<tr>
<th>Job class</th>
<td><%= recurring_task.job_class_name %></td>
</tr>
<tr>
<th>Arguments</th>
<td><div class="is-family-monospace"><%= recurring_task.arguments.join(",") %></div></td>
</tr>
<% if recurring_task.job_class_name.present? %>
<tr>
<th>Job class</th>
<td><%= recurring_task.job_class_name %></td>
</tr>

<tr>
<th>Arguments</th>
<td><div class="is-family-monospace"><%= recurring_task.arguments.join(",") %></div></td>
</tr>
<% elsif recurring_task.command.present? %>
<tr>
<th>Command</th>
<td><div class="is-family-monospace"><%= recurring_task.command %></div></td>
</tr>
<% end %>

<tr>
<th>Schedule</th>
<td><%= recurring_task.schedule %></td>
</tr>
<% if recurring_task.queue_name.present? %>
<tr>
<th>Queue</th>
<td><%= recurring_task.queue_name %></td>
</tr>
<% end %>
<% if recurring_task.priority.present? %>
<tr>
<th>Priority</th>
<td><%= recurring_task.priority %></td>
</tr>
<% end %>
</tbody>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
<%= link_to recurring_task.id, application_recurring_task_path(@application, recurring_task.id) %>
</td>
<td>
<%= link_to recurring_task.job_class_name, application_recurring_task_path(@application, recurring_task.id) %>
<% if recurring_task.job_class_name.present? %>
<%= recurring_task.job_class_name %>
<% if recurring_task.arguments.present? %>
<div class="is-family-monospace"><%= recurring_task.arguments.join(",") %></div>
<% if recurring_task.arguments.present? %>
<div class="is-family-monospace"><%= recurring_task.arguments.join(",") %></div>
<% end %>
<% elsif recurring_task.command.present? %>
<div class="is-family-monospace"><%= recurring_task.command %></div>
<% end %>
</td>
<td> <%= recurring_task.schedule %> </td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def recurring_task_attributes_from_solid_queue_recurring_task(task)
{
id: task.key,
job_class_name: task.class_name,
command: task.command,
arguments: task.arguments,
schedule: task.schedule
schedule: task.schedule,
queue_name: task.queue_name,
priority: task.priority
}
end

Expand Down
2 changes: 1 addition & 1 deletion mission_control-jobs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "irb", "~> 1.13"

spec.add_development_dependency "resque"
spec.add_development_dependency "solid_queue", ">= 0.5"
spec.add_development_dependency "solid_queue", ">= 0.9"
spec.add_development_dependency "selenium-webdriver"
spec.add_development_dependency "resque-pause"
spec.add_development_dependency "mocha"
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/recurring_tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class MissionControl::Jobs::RecurringTasksControllerTest < ActionDispatch::Integ
end

test "get recurring task list" do
dispatch_jobs_async(wait: 2.seconds) do
schedule_recurring_tasks_async(wait: 2.seconds) do
get mission_control_jobs.application_recurring_tasks_url(@application)
assert_response :ok

assert_select "tr.recurring_task", 1
assert_select "td a", "periodic_pause_job"
assert_select "td a", "PauseJob"
assert_select "td", "PauseJob"
assert_select "td", "every second"
assert_select "td", /less than \d+ seconds ago/
end
end

test "get recurring task details and job list" do
dispatch_jobs_async(wait: 1.seconds) do
schedule_recurring_tasks_async(wait: 1.seconds) do
get mission_control_jobs.application_recurring_task_url(@application, "periodic_pause_job")
assert_response :ok

Expand All @@ -39,7 +39,7 @@ class MissionControl::Jobs::RecurringTasksControllerTest < ActionDispatch::Integ
end

test "redirect to recurring tasks list when recurring task doesn't exist" do
dispatch_jobs_async do
schedule_recurring_tasks_async do
get mission_control_jobs.application_recurring_task_url(@application, "invalid_key")
assert_redirected_to mission_control_jobs.application_recurring_tasks_url(@application)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This migration comes from solid_queue (originally 20240811173327)
class AddNameToProcesses < ActiveRecord::Migration[7.1]
def change
add_column :solid_queue_processes, :name, :string
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This migration comes from solid_queue (originally 20240813160053)
class MakeNameNotNull < ActiveRecord::Migration[7.1]
def up
SolidQueue::Process.where(name: nil).find_each do |process|
process.name ||= [ process.kind.downcase, SecureRandom.hex(10) ].join("-")
process.save!
end

change_column :solid_queue_processes, :name, :string, null: false
add_index :solid_queue_processes, [ :name, :supervisor_id ], unique: true
end

def down
remove_index :solid_queue_processes, [ :name, :supervisor_id ]
change_column :solid_queue_processes, :name, :string, null: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This migration comes from solid_queue (originally 20240819165045)
class ChangeSolidQueueRecurringTasksStaticToNotNull < ActiveRecord::Migration[7.1]
def change
change_column_null :solid_queue_recurring_tasks, :static, false, true
end
end
6 changes: 4 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_08_06_160416) do
ActiveRecord::Schema[7.1].define(version: 2024_09_09_204136) do
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
Expand Down Expand Up @@ -77,7 +77,9 @@
t.string "hostname"
t.text "metadata"
t.datetime "created_at", null: false
t.string "name", null: false
t.index ["last_heartbeat_at"], name: "index_solid_queue_processes_on_last_heartbeat_at"
t.index ["name", "supervisor_id"], name: "index_solid_queue_processes_on_name_and_supervisor_id", unique: true
t.index ["supervisor_id"], name: "index_solid_queue_processes_on_supervisor_id"
end

Expand Down Expand Up @@ -108,7 +110,7 @@
t.text "arguments"
t.string "queue_name"
t.integer "priority", default: 0
t.boolean "static", default: true
t.boolean "static", default: true, null: false
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down
12 changes: 6 additions & 6 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ class ActionDispatch::IntegrationTest
@worker = SolidQueue::Worker.new(queues: "*", threads: 2, polling_interval: 0.01)

recurring_task = { periodic_pause_job: { class: "PauseJob", schedule: "every second" } }
@dispatcher = SolidQueue::Dispatcher.new(recurring_tasks: recurring_task)
@scheduler = SolidQueue::Scheduler.new(recurring_tasks: recurring_task)
end

teardown do
@worker.stop
@dispatcher.stop
@scheduler.stop
end

private
Expand All @@ -95,7 +95,7 @@ def queue_adapter_for_test
end

def register_workers(count: 1)
count.times { |i| SolidQueue::Process.register(kind: "Worker", pid: i) }
count.times { |i| SolidQueue::Process.register(kind: "Worker", pid: i, name: "worker-#{i}") }
end

def perform_enqueued_jobs_async(wait: 1.second)
Expand All @@ -106,11 +106,11 @@ def perform_enqueued_jobs_async(wait: 1.second)
@worker.stop
end

def dispatch_jobs_async(wait: 1.second)
@dispatcher.start
def schedule_recurring_tasks_async(wait: 1.second)
@scheduler.start
sleep(wait)

yield if block_given?
@dispatcher.stop
@scheduler.stop
end
end

0 comments on commit ca7f352

Please sign in to comment.