-
Notifications
You must be signed in to change notification settings - Fork 0
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
First Version #1
base: main
Are you sure you want to change the base?
Conversation
|
||
def retry_on(error, wait: 5, attempts: 5) | ||
self::Async.retry_on error, wait: wait.seconds, attempts: attempts | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end | |
# Use this method in your service class to set sidekiq options. | |
def sidekiq_options(options) | |
self::Async.sidekiq_options(options) | |
end | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def sidekiq_options(...)
def call(args = {}) | ||
perform_later(args: args, bang: false) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end | |
# Schedules the service to be run later with the bang method | |
def call_later!(wait:, args: {}) | |
set(wait:).perform_later(args: args, bang: true) | |
end | |
# Schedules the service to be run later without the bang method | |
def call_later(wait:, args: {}) | |
set(wait:).perform_later(args: args, bang: false) | |
end | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to schedule my services on a later time, so this is what i came up with :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not using set
?
I mean, for this edge-case you could use
MyService::Async.set(wait: 3.weeks).perform_later(args: my_args, bang: true)
Overloading the interface feels a bit unnecessary since it's pretty straight forward to rely on the original implementation of active_job for edge cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just feels weird to have to add , bang: true to the call args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is better
def call_in(duration, **kwargs)
set(wait: duration).perform_later(args: kwargs, bang: false)
end
then you can do
SomeOperation.call!(user_id: 3)
SomeOperation::Async.call!(user_id: 3)
SomeOperation::Async.call_in(3.minutes, user_id: 3)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not to have to think about active job interface since i am using this a lot
end | ||
end | ||
|
||
def perform(args:, bang:) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just have
def perform(args:, bang:)
if bang
self.class.module_parent.call!(args)
else
self.class.module_parent.call(args)
end
end
But maybe this is better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying a one-liner
@@ -0,0 +1,47 @@ | |||
# frozen_string_literal: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my specs, maybe you can use it?
class AsyncServiceTestErrorCollector
class << self
def call
"42"
end
end
end
class AsyncServiceRescueFromBlockTest < BaseService
include ::AsyncService
rescue_from(GuardError) do
AsyncServiceTestErrorCollector.call
end
def execute
raise GuardError, "this is a test with rescue_from"
end
end
class AsyncServiceRescueFromTest < BaseService
include ::AsyncService
rescue_from(GuardError)
def execute
raise GuardError, "this is a test with rescue_from"
end
end
class AsyncServiceNoRescueFromTest < BaseService
include ::AsyncService
def execute
raise GuardError, "this is a test with no rescue_from"
end
end
describe AsyncService do
before do
allow(AsyncServiceTestErrorCollector).to receive(:call).and_call_original
end
context "with service class that rescues from error with a block" do
subject { AsyncServiceRescueFromBlockTest::Async.new(args: {}, bang: true).perform_now }
it "performs the rescue block" do
expect { subject }.not_to raise_error
expect(AsyncServiceTestErrorCollector).to have_received(:call)
end
end
context "with service class that rescues from error" do
subject { AsyncServiceRescueFromTest::Async.new(args: {}, bang: true).perform_now }
it "performs the rescue without a block" do
expect { subject }.not_to raise_error
expect(AsyncServiceTestErrorCollector).not_to have_received(:call)
end
end
context "with service class that does not rescue from error" do
subject { AsyncServiceNoRescueFromTest::Async.new(args: {}, bang: true).perform_now }
before do
allow(AsyncServiceTestErrorCollector).to receive(:call).and_call_original
end
it "raises an error" do
expect { subject }.to raise_error(GuardError, "this is a test with no rescue_from")
expect(AsyncServiceTestErrorCollector).not_to have_received(:call)
end
end
end
Co-authored-by: Nicolai Seerup <[email protected]>
end | ||
|
||
def perform(args:, bang:) | ||
parent_name_space.public_send(bang ? :call! : :call, args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are missing the args in the call! method
maybe add this as well to get unique functionality https://github.com/veeqo/activejob-uniqueness |
No description provided.