amitree / delayed_job_recurring

Extends delayed_job to support recurring jobs
MIT License
84 stars 33 forks source link

Undefined method 'perform' #8

Closed berfarah closed 9 years ago

berfarah commented 9 years ago

Hi! I'm trying to use your on-the-fly scheduler like this:

Delayed::Task.schedule(options) do
  start_instance
  run script
  finish_instance
end

Where I access the database before and after running to log whether an instance is running and send it to another class in between to run a bash script.

When I deserialize the class manually, I get:

#<Object:0x007fbf852d9d98
 @schedule_options=
  {:run_at=>[2015-06-02 16:18:19 -0700],
   :timezone=>nil,
   :run_interval=>{:value=>120, :parts=>[[:seconds, 120]]},
   :priority=>nil,
   :queue=>"dfasdfasdfa36"}>

What am I missing?

afn commented 9 years ago

You're not missing anything; I was clearly smoking crack when I wrote this. :)

You can't serialize a block[1], so I'm not sure what I was thinking when I added the ability to schedule a block that's not attached to a class. The Delayed::Task.schedule method does take an optional name, in which case it defines a class with that name on-the-fly, but the implementation is broken, and it's problematic anyway, because this class is only defined in the Ruby environment where you call schedule. If you run your delayed jobs on a separate host, or in a separate Ruby process, it still won't be able to deserialize the job.

I'm going to get rid of Delayed::Task. Use this instead:

class BerfarahsJob
  include Delayed::RecurringJob
  def perform
    start_instance
    run script
    finish_instance
  end
end

BerfarahsJob.schedule(options)

Thanks for reporting the issue, and sorry for the inconvenience!

[1] That's not 100% true; one could take a block and decompile it, e.g. see http://stackoverflow.com/questions/199603/how-do-you-stringize-serialize-ruby-code — but this is probably outside the scope of this gem.

berfarah commented 9 years ago

Haha awesome, thanks @afn. I figured there was some Black Magic™ with memory caching or something since I couldn't figure out how it worked.

For my purposes, I suppose I'll be overwriting #jobs to only look for jobs in a matching queue, since the reason I wanted to use dynamic classes was to keep track of individual instances.