Class: Sad::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/sad/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, args = [], sad_args = {}) ⇒ Payload

Returns a new instance of Payload.



7
8
9
10
11
12
13
14
# File 'lib/sad/payload.rb', line 7

def initialize(klass, args = [], sad_args = {})
	@klass = klass
	@args = args
	@sad_args = {
		'retry' => 0,
		'delay' => 0
	}.update(sad_args)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/sad/payload.rb', line 5

def args
  @args
end

#klassObject

Returns the value of attribute klass.



5
6
7
# File 'lib/sad/payload.rb', line 5

def klass
  @klass
end

#redisObject

Returns the value of attribute redis.



5
6
7
# File 'lib/sad/payload.rb', line 5

def redis
  @redis
end

#sad_argsObject

Returns the value of attribute sad_args.



5
6
7
# File 'lib/sad/payload.rb', line 5

def sad_args
  @sad_args
end

Class Method Details

.decode(json) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/sad/payload.rb', line 67

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

Instance Method Details

#encodeObject



20
21
22
23
24
25
26
# File 'lib/sad/payload.rb', line 20

def encode
	{
		'klass' => @klass,
		'args' => @args,
		'sad_args' => @sad_args
	}.to_json
end

#enqueue(&blk) ⇒ Object



47
48
49
50
51
# File 'lib/sad/payload.rb', line 47

def enqueue(&blk)
	self.wrap_redis_rpush(self.sad_args['queue'], self.encode) do |value|
		blk.call(value) if blk
	end
end

#performObject

执行任务 当执行任务的perform出错时 重试1至::Sad::Config.max_retry次 每次重试时,延迟重试次数*::Sad::Config.interval的时长后,再enqueue



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sad/payload.rb', line 32

def perform
	begin
		@klass.constantize.send :perform, *@args
	rescue Exception => e
		::Sad.logger.error "Payload perform error:\n#{e.to_s}\n#{e.backtrace.join($/)}"
		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

#wrap_redis_rpush(queue, data, &blk) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sad/payload.rb', line 53

def wrap_redis_rpush(queue, data, &blk)
	case self.redis.class.to_s
	when "EM::Hiredis::Client", "EventMachine::Hiredis::Client"
		self.redis.rpush(queue, data) do |value|
			blk.call(value)
		end
	when "Redis", "Redis::Namespace"
		self.redis.rpush(queue, data)
		blk.call(data)
	else
		raise RuntimeError, "No redis client support!\nself.redis => #{self.redis.to_s}\n#{self.inspect}"
	end
end