Module: Insque
- Defined in:
- lib/insque.rb,
lib/insque/railtie.rb,
lib/insque/version.rb,
lib/generators/insque/initializer_generator.rb
Defined Under Namespace
Modules: Generators
Classes: Railtie
Constant Summary
collapse
- VERSION =
"0.5.2"
Class Method Summary
collapse
Class Method Details
.broadcast(message, params = nil, recipient = :any) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/insque.rb', line 40
def self.broadcast message, params = nil, recipient = :any
keys = []
case recipient
when :any
keys = @redis.smembers '{insque}inboxes'
when :self
keys = [@inbox]
when :slow
keys = [@slow_inbox]
else
keys = recipient.is_a?(Array) ? recipient : [recipient]
end
value = { :message => "#{@sender}_#{message}", :params => params, :broadcasted_at => Time.now.utc }
log "SENDING: #{value.to_json} TO #{keys.to_json}" if @debug
@redis.multi do |r|
keys.each {|k| r.lpush k, value.to_json}
end
end
|
.create_redis_connection ⇒ Object
130
131
132
|
# File 'lib/insque.rb', line 130
def self.create_redis_connection
(@redis_class || Redis).new @redis_config
end
|
.create_send_later_handler ⇒ Object
139
140
141
142
143
|
# File 'lib/insque.rb', line 139
def self.create_send_later_handler
define_singleton_method("#{@sender}_send_later") do |msg|
Kernel.const_get(msg['params']['class']).unscoped.find(msg['params']['id']).send(msg['params']['method'], *msg['params']['args'])
end
end
|
.debug=(debug) ⇒ Object
6
7
8
|
# File 'lib/insque.rb', line 6
def self.debug= debug
@debug = debug
end
|
.do_listen(inbox, processing, redis, worker_name) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/insque.rb', line 78
def self.do_listen inbox, processing, redis, worker_name
log "#{worker_name} START LISTENING #{inbox}"
loop do
message = redis.brpoplpush(inbox, processing, 0)
log "#{worker_name} RECEIVING: #{message}" if @debug
begin
parsed_message = JSON.parse message
send(parsed_message['message'], parsed_message) if self.respond_to? parsed_message['message']
rescue => e
log "#{worker_name} ========== BROKEN_MESSAGE: #{message} =========="
log e.inspect
log e.backtrace
end
redis.lrem processing, 0, message
end
end
|
.janitor(redis = nil) ⇒ Object
69
70
71
|
# File 'lib/insque.rb', line 69
def self.janitor redis=nil
real_janitor @inbox, @processing, (redis || create_redis_connection)
end
|
.listen(worker_name = '', redis = nil) ⇒ Object
59
60
61
62
63
|
# File 'lib/insque.rb', line 59
def self.listen worker_name='', redis=nil
redis ||= create_redis_connection
redis.sadd '{insque}inboxes', @inbox
do_listen @inbox, @processing, redis, worker_name
end
|
.log(message) ⇒ Object
134
135
136
137
|
# File 'lib/insque.rb', line 134
def self.log message
print "#{Time.now.utc} #{message}\n"
STDOUT.flush if @debug
end
|
.real_janitor(inbox, processing, redis) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/insque.rb', line 95
def self.real_janitor inbox, processing, redis
loop do
redis.watch processing
errors = []
restart = []
delete = []
redis.lrange(processing, 0, -1).each do |m|
begin
parsed_message = JSON.parse(m)
if parsed_message['restarted_at'] && DateTime.parse(parsed_message['restarted_at']) < 1.hour.ago.utc
errors << parsed_message
delete << m
elsif DateTime.parse(parsed_message['broadcasted_at']) < 1.hour.ago.utc
restart << parsed_message.merge(:restarted_at => Time.now.utc)
delete << m
end
rescue
log "========== JANITOR_BROKEN_MESSAGE: #{m} =========="
end
end
result = redis.multi do |r|
restart.each {|m| r.lpush inbox, m.to_json }
delete.each {|m| r.lrem processing, 0, m }
end
if result
errors.each {|m| log "ERROR: #{m.to_json}" }
restart.each {|m| log "RESTART: #{m.to_json}" }
log "CLEANING SUCCESSFULL"
else
log "CLEANING FAILED"
end
sleep(Random.rand * 300)
end
end
|
.redis ⇒ Object
14
15
16
|
# File 'lib/insque.rb', line 14
def self.redis
@redis
end
|
.redis=(redis) ⇒ Object
10
11
12
|
# File 'lib/insque.rb', line 10
def self.redis= redis
@redis = redis
end
|
.redis_class=(klass) ⇒ Object
18
19
20
|
# File 'lib/insque.rb', line 18
def self.redis_class= klass
@redis_class = klass
end
|
.redis_config ⇒ Object
22
23
24
|
# File 'lib/insque.rb', line 22
def self.redis_config
@redis_config
end
|
.redis_config=(redis) ⇒ Object
26
27
28
29
|
# File 'lib/insque.rb', line 26
def self.redis_config= redis
@redis_config = redis
@redis = self.create_redis_connection
end
|
.sender=(sender) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/insque.rb', line 31
def self.sender= sender
@sender = sender
@inbox = "{insque}inbox_#{sender}"
@processing = "{insque}processing_#{sender}"
@slow_inbox = "{insque}slow_inbox_#{sender}"
@slow_processing = "{insque}slow_processing_#{sender}"
create_send_later_handler
end
|
.slow_janitor(redis = nil) ⇒ Object
73
74
75
|
# File 'lib/insque.rb', line 73
def self.slow_janitor redis=nil
real_janitor @slow_inbox, @slow_processing, (redis || create_redis_connection)
end
|
.slow_listen(worker_name = '', redis = nil) ⇒ Object
65
66
67
|
# File 'lib/insque.rb', line 65
def self.slow_listen worker_name='', redis=nil
do_listen @slow_inbox, @slow_processing, (redis || create_redis_connection), worker_name
end
|