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

First Version #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

First Version #1

wants to merge 6 commits into from

Conversation

alejanderl
Copy link
Contributor

No description provided.

@alejanderl alejanderl changed the title First commit First Version May 29, 2024
lib/hanikamu/services/async.rb Show resolved Hide resolved
lib/hanikamu/services/async.rb Show resolved Hide resolved

def retry_on(error, wait: 5, attempts: 5)
self::Async.retry_on error, wait: wait.seconds, attempts: attempts
end
Copy link

Choose a reason for hiding this comment

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

Suggested change
end
# Use this method in your service class to set sidekiq options.
def sidekiq_options(options)
self::Async.sidekiq_options(options)
end
end

Copy link

Choose a reason for hiding this comment

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

def sidekiq_options(...)

lib/hanikamu/services/async.rb Outdated Show resolved Hide resolved
def call(args = {})
perform_later(args: args, bang: false)
end
end
Copy link

Choose a reason for hiding this comment

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

Suggested change
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

Copy link

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 :)

Copy link
Contributor Author

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

Copy link

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

Copy link

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)

Copy link

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:)
Copy link

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?

Copy link
Contributor Author

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
Copy link

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

end

def perform(args:, bang:)
parent_name_space.public_send(bang ? :call! : :call, args)
Copy link

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

@casiodk
Copy link

casiodk commented Jun 5, 2024

maybe add this as well to get unique functionality https://github.com/veeqo/activejob-uniqueness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants