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

Added enqueue_with_response #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions lib/stalker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def enqueue(job, args={}, opts={})
failed_connection(e)
end

def enqueue_with_response(job, args={}, opts={})
job_id = enqueue(job, {:beanstalk_respond_to => beanstalk_url}.merge(args), opts)
response_queue = "response.#{job}.#{job_id}"
beanstalk.watch response_queue
response = beanstalk.reserve(opts[:timeout])
beanstalk.ignore response_queue
response.delete
JSON.parse(response.body).last
end

def job(j, &block)
@@handlers ||= {}
@@handlers[j] = block
Expand Down Expand Up @@ -69,6 +79,7 @@ class JobTimeout < RuntimeError; end
def work_one_job
job = beanstalk.reserve
name, args = JSON.parse job.body
beanstalk_respond_to = args.delete('beanstalk_respond_to')
log_job_begin(name, args)
handler = @@handlers[name]
raise(NoSuchJob, name) unless handler
Expand All @@ -80,12 +91,17 @@ def work_one_job
block.call(name)
end
end
handler.call(args)
response = handler.call(args)
unless beanstalk_respond_to.nil?
response_queue = "response.#{name}.#{job.id}"
beanstalk.use response_queue
beanstalk.put [name, response].to_json
beanstalk.ignore response_queue
end
end
rescue Timeout::Error
raise JobTimeout, "#{name} hit #{job.ttr-1}s timeout"
end

job.delete
log_job_end(name)
rescue Beanstalk::NotConnected => e
Expand Down