quirkey / resque-status

resque-status is an extension to the resque queue system that provides simple trackable jobs.
MIT License
514 stars 169 forks source link

Attach custom behavior to 'at' as callback #110

Closed chriskilding closed 9 years ago

chriskilding commented 10 years ago

When calling 'at' with a status message, it would sometimes be convenient to have a hook into the 'at' execution so that custom behaviour can be triggered after the main body of it has run. For example, relaying the status message to a Webhook status callback URL, or relaying the message to a broadcast server like Pusher or Faye. Does this plugin already have this capability? If not, and you would be willing to extend resque-status to allow attaching such behaviour, which of the following approaches (or others) would be best:

chriskilding commented 10 years ago

With option 2, I am thinking of an instance method available on the Worker class like the following. Being an instance method it will have access to the 'options' variable, making useful operations possible. The after_at :callback thing is just modelled on ActiveRecord callbacks, just because it would allow easy attachment of multiple callback methods rather than just one:

class SomeWorker
    include Resque::Plugins::Status

    def queue
        :some_queue
    end

    after_at :send_msg

    def send_msg msg
        # Perform Webhook callback, or whatever behaviour you like
        if options['callback_url']
            HTTParty.post options['callback_url'], msg
        end
    end

    def perform
        x = options['x']
        at 0, 2, 'starting a thing'
        ...
    end
end
chriskilding commented 10 years ago

I added a better implementation for this, for each type of callback, in pull request #117