quirkey/resque-status

Attach custom behavior to 'at' as callback

Closed this issue · 2 comments

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:

  • Pass optional block to each 'at' call
  • Pass optional block to the worker upon initialisation, so that the context is set when 'perform' is invoked each time (e.g. each API client has their own callback URL, but this won't change throughout 'perform')
  • Accept optional block in a config/initializers file, i.e. take in custom behaviour once at boot time (e.g. pubsub server pushing logic + connection credentials)

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

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