مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2026-03-03 22:34:09 +00:00
47 أسطر
1011 B
Ruby
47 أسطر
1011 B
Ruby
require 'nifty/utils/random_string'
|
|
|
|
module Postal
|
|
class Job
|
|
def initialize(id, params = {})
|
|
@id = id
|
|
@params = params.with_indifferent_access
|
|
on_initialize
|
|
end
|
|
|
|
def id
|
|
@id
|
|
end
|
|
|
|
def params
|
|
@params || {}
|
|
end
|
|
|
|
def on_initialize
|
|
# Called whenever the class is initialized. Can be overriden.
|
|
end
|
|
|
|
def on_error(exception)
|
|
# Called if there's an exception while processing the perform block.
|
|
# Receives the exception.
|
|
end
|
|
|
|
def perform
|
|
end
|
|
|
|
def log(text)
|
|
Worker.logger.info "[#{@id}] #{text}"
|
|
end
|
|
|
|
def self.queue(queue, params = {})
|
|
job_id = Nifty::Utils::RandomString.generate(:length => 10).upcase
|
|
job_payload = {'params' => params, 'class_name' => self.name, 'id' => job_id, 'queue' => queue}
|
|
Postal::Worker.job_queue(queue).publish(job_payload.to_json, :persistent => false)
|
|
job_id
|
|
end
|
|
|
|
def self.perform(params = {})
|
|
new(nil, params).perform
|
|
end
|
|
end
|
|
end
|