message 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,6 +29,10 @@ module Message
29
29
  end
30
30
  end
31
31
 
32
+ def log(level, &block)
33
+ Message.logger.send(level) { "[Thread-#{Thread.current.object_id}] #{block.call}" }
34
+ end
35
+
32
36
  def logger
33
37
  @logger ||= Logger.new(STDOUT)
34
38
  end
@@ -7,11 +7,11 @@ module Message
7
7
  return filter.call(msg) unless action == :process
8
8
 
9
9
  ret = nil
10
- Message.logger.info { "#{job.name}: processing one message"}
10
+ Message.log(:info) { "#{job.name}: processing one message"}
11
11
  s = Benchmark.realtime do
12
12
  ret = filter.call(msg)
13
13
  end
14
- Message.logger.info { "#{job.name}: processed in #{(1000 * s).to_i}ms" }
14
+ Message.log(:info) { "#{job.name}: processed in #{(1000 * s).to_i}ms" }
15
15
  ret
16
16
  end
17
17
  end
@@ -9,7 +9,7 @@ module Message
9
9
  filter.call(msg)
10
10
  rescue => e
11
11
  job_name = job.name rescue 'unknown job(find job name failed)'
12
- Message.logger.error {"#{action} #{job_name} message failed, #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"}
12
+ Message.log(:error) {"#{action} #{job_name} message failed, #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"}
13
13
  if self.callback
14
14
  self.callback.call(e, msg, job, action)
15
15
  end
@@ -71,15 +71,16 @@ module Message
71
71
  delay = options[:delay] || 10 + rand(20)
72
72
  Thread.start do
73
73
  begin
74
- log(:info) { "start" }
74
+ Message.log(:info) { "[Worker] start in #{delay}" }
75
75
  sleep delay
76
76
  loop do
77
77
  process(size)
78
78
  sleep interval
79
79
  end
80
- log(:info) { "stopped" }
81
80
  rescue => e
82
- log(:error) { "crashed: #{e.message}\n#{e.backtrace.join("\n")}"}
81
+ Message.log(:error) { "[Worker] crashed: #{e.message}\n#{e.backtrace.join("\n")}"}
82
+ ensure
83
+ Message.log(:info) { "[Worker] stopped" }
83
84
  end
84
85
  end
85
86
  end
@@ -112,9 +113,5 @@ module Message
112
113
  obj, m, args = work
113
114
  obj.send(m, *args)
114
115
  end
115
-
116
- def log(level, &block)
117
- Message.logger.send(level) { "[Worker(#{Thread.current.object_id})] #{block.call}" }
118
- end
119
116
  end
120
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: