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

idea: way to make it easy to invoke a method on an object #22

Open
jjb opened this issue Aug 13, 2014 · 2 comments
Open

idea: way to make it easy to invoke a method on an object #22

jjb opened this issue Aug 13, 2014 · 2 comments

Comments

@jjb
Copy link

jjb commented Aug 13, 2014

My most common use of sidekiq is

foo.delay.my_method(1,2,3)

This is pretty safe for most things, but it would be more ideal and efficient in some ways to have the worker pull out the object again and then invoke.

class FooMyMethodDoer
  include Sidekiq::Worker

  def perform(id,a,b,c)
    foo = Foo.find(id)
    foo.my_method(a,b,c)
  end
end

So, one could make some sort of generic worker for this

class MethodInvoker
  include Sidekiq::Worker

  def perform(class_name, id, method_name, *params)
    object = class_name.constantize.find(id)
    object.method(method_name.to_sym).run(params)
  end
end

(I haven't tried that, I may have gotten something slightly off)

Would be nice to have some syntactic sugar for that, so instead of instantiating a worker in one's code all the time, one could just do:

foo.delay_awesomely(:my_method, 1,2,3)

Thoughts?

@seuros
Copy link
Member

seuros commented Aug 13, 2014

I think delaying methods is bad practice, the state of an record could have changed by the time it is invoked.

@yelled3
Copy link
Contributor

yelled3 commented Aug 13, 2014

@jjb you can do this easily with TaskWorker:
https://github.com/sidekiq-orm/sidekiq-activerecord/wiki/Task-Worker

class MethodInvokerTaskWorker < Sidekiq::ActiveRecord::TaskWorker

  sidekiq_task_model :user # or User

  def perform_on_model(options)
    user.my_method(options[:a], options[:b])
  end
end

and then call it by doing

MethodInvokerTaskWorker.perform_async(user.id, {:a => true, :b => 8})

@seuros

I think delaying methods is bad practice, the state of an record could have changed by the time it is invoked.

agreed 👍

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

No branches or pull requests

3 participants