Sha256: 7d405414ec728d8c59f21760b6316f0b5db88804b76cbe59e5afea41dd662a54
Contents?: true
Size: 1.34 KB
Versions: 7
Compression:
Stored size: 1.34 KB
Contents
require "json" module Sad class Payload attr_accessor :klass, :args, :sad_args def initialize(klass, args = [], sad_args = {}) @klass = klass @args = args @sad_args = { 'retry' => 0, 'delay' => 0 }.update(sad_args) end def encode { 'klass' => @klass, 'args' => @args, 'sad_args' => @sad_args }.to_json end # 执行任务 # 当执行任务的perform出错时 # 重试1至::Sad::Config.max_retry次 # 每次重试时,延迟重试次数*::Sad::Config.interval的时长后,再enqueue def perform begin @klass.constantize.send :perform, *@args rescue Exception => e if self.sad_args['retry'] and (self.sad_args['retry'].to_i < ::Sad::Config.max_retry) self.sad_args['retry'] = self.sad_args['retry'].to_i + 1 self.sad_args['delay'] = ::Sad::Config.interval * self.sad_args['retry'] self.enqueue else ::Sad.logger.error "Payload perform error for #{self.sad_args['retry']} retrys:\n#{self.inspect}\nException:\n#{e.inspect}\nBacktrace:\n#{e.backtrace.inspect}" end end end def enqueue ::Sad::Config.redis.rpush(self.sad_args['queue'], self.encode) end def self.decode(json) h = JSON.parse(json) if h['sad_args'] or h['sad_args'] != '' self.new(h['klass'], h['args'], h['sad_args']) else self.new(h['klass'], h['args']) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems