Push Job - nebulab/cangaroo GitHub Wiki
Jobs are where the payload is pushed to the configured connection.
To allow a job to be executed add it to the Rails.configuration.cangaroo.jobs
configuration, for example in an initializer:
# config/initializers/cangaroo.rb
Rails.configuration.cangaroo.jobs = [Cangaroo::AddOrderJob, Cangaroo::UpdateShipmentJob]
The Cangaroo::Job class inherits from ActiveJob::Base, so you can use
any 3rd-party queuing library supported by ActiveJob.
When the job is performed Cangaroo makes a POST request to the connection with
the configured path and build the json body with the result of the #transform
instance method merged with this attributes:
request_id- is thejob_idcoming fromActiveJob::Baseparameters- are the parameters configured by theparametersclass method
You can use the following Cangaroo::Job class methods to configure the job's
behaivor:
- connection - is the connection name (see connection for more info)
- path - this path will be appended to your
connection.url - parameters - these parameters will be merged with
connection.parameters, they will be added to the json body.
it also has a #perform? instance method that must be implemented. This method
must return true or false as Cangaroo will use it to understand if the job
must be performed. Inside the #perform? method you'll be able to access the
source_connection, type and payload instance attributes.
The #transform instance method can be overridden to customize the json body
request, it will have the source_connection, type and payload variables
(like the #perform? method) and must return an Hash.
The following is an example of a Cangaroo::Job:
module Cangaroo
class ShipmentJob < Cangaroo::Job
connection :mystore
path '/update_shipment'
parameters({ timestamp: Time.now })
def transform
payload = super
payload['shipment']['updated_at'] = Time.now
payload
end
def perform?
type == 'shipments' &&
payload['status'] == 'shipped'
end
end
end
Suppose that the mystore connection has a url set to "http://mystore.com"
an the payload is something like:
{ "id": "S123", "status": "shipped" }
It will do a POST request to http://mystore.com/update_shipment with
this json body:
{
"request_id": "088e29b0ab0079560dea5d3e5aeb2f7868af661e",
"parameters": {
"timestamp": "2015-11-04 14:14:30 +0100"
},
"shipment": {
"id": "S123",
"status": "shipped"
}
}