Skip to content

Commit 59b4023

Browse files
authored
Run jobs with dynamic args (#21)
* Allow to run jobs with dynamic args * Added documentation * Fix rubocop warnings * Added dynamic args enum to run jobs * Better doc * Added changelog and Update version * Rollback
1 parent 3ed273d commit 59b4023

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
## [Unreleased]
2+
3+
## [2.0.2] - 2025-03-23
4+
- Dynamic args on Jobs Run
5+
26
## [2.0.1] - 2025-03-11
37
- Fix Support to Sidekiq-ent and Sidekiq-pro v8
48

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
sidekiq-belt (2.0.1)
4+
sidekiq-belt (2.0.2)
55
sidekiq (>= 7.3.8)
66

77
GEM
@@ -88,4 +88,4 @@ DEPENDENCIES
8888
simplecov
8989

9090
BUNDLED WITH
91-
2.5.23
91+
2.6.3

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ Sidekiq::Belt.configure do |config|
113113

114114
config.run_jobs << { class: "CWorker", args: ["a"], group: "Etc" }
115115
config.run_jobs << { class: "DWorker", args: ["a"], group: "Etc" }
116+
117+
config.run_jobs << { class: "EWorker", args: [
118+
{ dynamic: 'text', title: 'Observation' },
119+
{ dynamic: 'integer', title: 'Number of test' },
120+
'fixed value A',
121+
{ dynamic: 'boolean', title: 'Boolean data' },
122+
{ dynamic: 'enum', title: 'Enum data', options: ['option1', 'option2', 'option3'], default: 'option2' },
123+
'fixed value B'
124+
], group: "Etc" }
125+
config.run_jobs << { class: "FWorker", args: ["a"], group: "Etc" }
116126
end
117127
```
118128

lib/sidekiq/belt/community/run_job.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,32 @@ def self.list_grouped_jobs
2020
jobs
2121
end
2222

23-
def self.run_job(job_id)
23+
def self.dynamic_type?(arg, type)
24+
return false unless arg.is_a?(Hash)
25+
26+
arg[:dynamic].to_s == type
27+
end
28+
29+
def self.run_job(job_id, extra_args = {})
2430
job = Sidekiq::Belt.config.run_jobs[job_id.to_i]
2531
job.transform_keys(&:to_sym)
2632

27-
Module.const_get(job[:class]).perform_async(*job.fetch(:args, []))
33+
args = job.fetch(:args, [])
34+
35+
new_args = []
36+
args.each_with_index do |arg, i|
37+
if dynamic_type?(arg, "text") || dynamic_type?(arg, "enum")
38+
new_args[i] = extra_args.shift
39+
elsif dynamic_type?(arg, "integer")
40+
new_args[i] = extra_args.shift.to_i
41+
elsif dynamic_type?(arg, "boolean")
42+
new_args[i] = extra_args.shift == "true"
43+
else
44+
new_args << arg
45+
end
46+
end
47+
48+
Module.const_get(job[:class]).perform_async(*new_args)
2849
end
2950

3051
module SidekiqRunJob
@@ -36,7 +57,9 @@ def self.registered(app)
3657
end
3758

3859
app.post("/run_jobs/:rjid/run") do
39-
Sidekiq::Belt::Community::RunJob.run_job(route_params(:rjid).to_i)
60+
args = url_params("args")
61+
62+
Sidekiq::Belt::Community::RunJob.run_job(route_params(:rjid).to_i, args)
4063

4164
return redirect "#{root_path}run_jobs"
4265
end

lib/sidekiq/belt/community/views/run_jobs.erb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,35 @@
1414
<% jobs.each do |job| %>
1515
<tr>
1616
<td><%= job[:class] %></td>
17-
<td><%= job[:args].inspect %></td>
18-
<td style='width: 100px'>
19-
<form action="<%= root_path %>run_jobs/<%= job[:id] %>/run" method="post">
17+
<td colspan="2">
18+
<form action="<%= root_path %>run_jobs/<%= job[:id] %>/run" class="force-run-form" method="post">
19+
<%= '[' %>
20+
<% (job[:args] || []).each_with_index do |arg, i| %>
21+
<% if Sidekiq::Belt::Community::RunJob.dynamic_type?(arg, 'text') %>
22+
<input type="text" class="form-control" name="args[]" title="<%= arg[:title] %>" required='true' placeholder="<%= arg[:title] %>" value="<%= arg[:default] %>" />
23+
<% elsif Sidekiq::Belt::Community::RunJob.dynamic_type?(arg, 'integer') %>
24+
<input type="number" class="form-control" name="args[]" title="<%= arg[:title] %>" required='true' placeholder="<%= arg[:title] %>" value="<%= arg[:default] %>" />
25+
<% elsif Sidekiq::Belt::Community::RunJob.dynamic_type?(arg, 'boolean') %>
26+
<select class="form-control" name="args[]" required='true' title="<%= arg[:title] %>" placeholder="<%= arg[:title] %>">
27+
<option value="true" <%= arg[:default] == 'true' ? 'selected' : '' %>>true</option>
28+
<option value="false" <%= arg[:default] == 'false' ? 'selected' : '' %>>false</option>
29+
</select>
30+
<% elsif Sidekiq::Belt::Community::RunJob.dynamic_type?(arg, 'enum') %>
31+
<select class="form-control" name="args[]" required='true' title="<%= arg[:title] %>" placeholder="<%= arg[:title] %>">
32+
<% arg[:options].each do |option| %>
33+
<option value="<%= option %>" <%= arg[:default].to_s == option.to_s ? 'selected' : '' %>><%= option %></option>
34+
<% end %>
35+
</select>
36+
<% else %>
37+
<input type="hidden" name="args[]" value="<%= arg %>" />
38+
<%= arg.inspect %>
39+
<% end %>
40+
<%= ',' if i < (job[:args] || []).size - 1 %>
41+
<% end %>
42+
<%= ']' %>
43+
2044
<%= csrf_tag %>
45+
2146
<input class="btn btn-danger" type="submit" name="run" value="<%= t('Run') %>"
2247
data-confirm="Run the job <%= job[:klass] %>? <%= t('AreYouSure') %>" />
2348
</form>

lib/sidekiq/belt/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Sidekiq
44
module Belt
5-
VERSION = "2.0.1"
5+
VERSION = "2.0.2"
66
end
77
end

0 commit comments

Comments
 (0)