Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to search the Args to find jobs #714

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion lib/resque/scheduler/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ def delayed_jobs_klass
end

def delayed_search
@jobs = find_job(params[:search])
@jobs = if params[:search_args]=='checked'
find_jobs_with_expr(params[:search])
else
find_job(params[:search])
end
erb scheduler_template('search')
end

Expand Down Expand Up @@ -151,6 +155,11 @@ def queue_from_class_name(class_name)
)
end

def find_jobs_with_expr(search_expr)
results = working_jobs_for_expr(search_expr)
results + delayed_jobs_for_search_expr(search_expr)
end

def find_job(worker)
worker = worker.downcase
results = working_jobs_for_worker(worker)
Expand Down Expand Up @@ -243,6 +252,33 @@ def working_jobs_for_worker(worker)
end
end

def working_jobs_for_expr(expr)
[].tap do |results|
working = [*Resque.working]
work = working.select do |w|
w.job && w.job['payload'].inspect.match(/#{expr}/)
end
work.each do |w|
results += [
w.job['payload'].merge(
'queue' => w.job['queue'], 'where_at' => 'working'
)
]
end
end
end

def delayed_jobs_for_search_expr(expr)
schedule_size = Resque.delayed_queue_schedule_size
[].tap do |dels|
Resque.delayed_queue_peek(0, schedule_size).map do |d|
Resque.delayed_timestamp_peek(d, 0, Resque.delayed_timestamp_size(d)).select do |j|
dels << j.merge!('timestamp' => d, 'where_at' => 'delayed') if j.inspect.match(/#{expr}/i)
end
end
end
end

def delayed_jobs_for_worker(_worker)
[].tap do |dels|
schedule_size = Resque.delayed_queue_schedule_size
Expand Down
2 changes: 1 addition & 1 deletion lib/resque/scheduler/server/views/delayed.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td><%= h(show_job_arguments(job['args'])) if job && delayed_timestamp_size == 1 %></td>
<td>
<% if job %>
<a href="<%=u URI("/delayed/jobs/#{URI.escape(job['class'])}?args=" + URI.encode(job['args'].to_json)) %>">All schedules</a>
<a href="<%=u URI("/delayed/jobs/#{URI::Parser.new.escape(job['class'])}?args=" + URI.encode_www_form_component(job['args'].to_json)) %>">All schedules</a>
<% end %>
</td>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions lib/resque/scheduler/server/views/search_form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<form method="POST" action="<%= u 'delayed/search' %>">
<input type='checkbox' id='search_args' name='search_args' value="checked" <%= 'checked' if params[:search_args]=='checked' %>/>
<label for="search_args"> Search Args?</label><br>
<input type='input' name='search' value="<%= h params[:search] %>"/>
<input type='submit' value='Search'/>
</form>
Expand Down
6 changes: 3 additions & 3 deletions test/resque-web_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
test "contains link to all schedules for class #{job['class']}" do
Resque.enqueue_at(Time.now + job['t'], job['class'], *job['args'])
get '/delayed'
assert !(last_response.body =~ %r{/delayed/jobs/#{URI.escape(job['class'].to_s)}}).nil?
assert !(last_response.body =~ %r{/delayed/jobs/#{URI::Parser.new.escape(job['class'].to_s)}}).nil?
end
end
end
Expand All @@ -112,7 +112,7 @@
Resque.enqueue_at(@t, SomeIvarJob, 'foo', 'bar')
get(
URI('/delayed/jobs/SomeIvarJob?args=' <<
URI.encode(%w(foo bar).to_json)).to_s
URI.encode_www_form_component(%w(foo bar).to_json)).to_s
)
end

Expand All @@ -135,7 +135,7 @@ def self.queue
Resque.enqueue_at(@t, Foo::Bar, 'foo', 'bar')
get(
URI('/delayed/jobs/Foo::Bar?args=' <<
URI.encode(%w(foo bar).to_json)).to_s
URI.encode_www_form_component(%w(foo bar).to_json)).to_s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed on a PR merged into master

)
end

Expand Down