-
Notifications
You must be signed in to change notification settings - Fork 109
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 sync
option to job configuration to run job synchronously / immediately
#112
Conversation
…mmediately There might be a case when you want some jobs to complete immediately after a call to `Workflow#start!`. For example, some jobs might complete very quickly and it can be helpful to show the results of those jobs in the response to some web request that started the workflow. Any job with the `sync: true` option is not sent to the queuing adapter but directly executed by blocking the execution of others until it’s finished.
Hi, @noahfpf!
I think instead of introducing this slippery slope of sync jobs one could simply do required setup inside |
The only functional change is that
If a sync job has dependencies, it'll enqueue them just like an async job would.
If a sync job is a dependency, it'll be run in the same ActiveJob worker instance as its parent.
For my organization, the utility to this concept is that a But I certainly see that this feature has limited utility to others and might not be worth merging into Gush. Maybe an interesting alternate approach would be to allow users of Gush to swap out the |
Thank you for detailed explanation!
I agree, this would be a more flexible way for users to tweak the behaviour without the need to add a passthrough for every possible ActiveJob parameter. Maybe a simple overrideable method for enqueuing that we can document ? |
I think the synchronous execution idea has some great use cases! I agree with @pokonski, your last suggestions about using something to 'swap out' the Worker calls could have some nice outcomes. An overridable method seems very reasonable, or maybe a proc/lambda to encapsulate what ends up needing to run when we call |
@pokonski and @natemontgomery, here's a suggestion for how to allow Gush users to have more control over the use of
diff --git a/lib/gush/client.rb b/lib/gush/client.rb
index c8e36d6..d866cd7 100644
--- a/lib/gush/client.rb
+++ b/lib/gush/client.rb
@@ -155,14 +155,9 @@ module Gush
def enqueue_job(workflow_id, job)
job.enqueue!
persist_job(workflow_id, job)
- queue = job.queue || configuration.namespace
- wait = job.wait
- if wait.present?
- Gush::Worker.set(queue: queue, wait: wait).perform_later(*[workflow_id, job.name])
- else
- Gush::Worker.set(queue: queue).perform_later(*[workflow_id, job.name])
- end
+ options = { queue: configuration.namespace }.merge(job.worker_options)
+ job.enqueue_worker!(**options)
end
private
diff --git a/lib/gush/job.rb b/lib/gush/job.rb
index e432648..669f0df 100644
--- a/lib/gush/job.rb
+++ b/lib/gush/job.rb
@@ -59,6 +59,14 @@ module Gush
@failed_at = nil
end
+ def enqueue_worker!(**options)
+ Gush::Worker.set(**options).perform_later(workflow_id, name)
+ end
+
+ def worker_options
+ { queue: queue, wait: wait }.compact
+ end
+
def finish!
@finished_at = current_timestamp
end What do you think of this approach? If it seems right, I'll submit a new PR (with specs). The alternative approach I considered is:
|
I think the diff you showed looks great, super simple and easy to override per-job! Bonus points for getting rid of the unnecessary conditional 😁 |
Thanks for the quick feedback! I've opened #117, and I'm closing this PR. |
There might be a case when you want some jobs to complete immediately after a call to
Workflow#start!
. For example, some jobs might complete very quickly and it can be helpful to show the results of those jobs in the response to some web request that started the workflow. Any job with thesync: true
option is not sent to the queuing adapter but directly executed by blocking the execution of others until it’s finished.