message 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +8 -1
  3. data/lib/message/worker.rb +33 -13
  4. metadata +12 -20
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee19d8a22b05ea1f808a9fa3bbdf502d11c48d1b
4
+ data.tar.gz: f2c79a4328536efc675c732beb8ad0d97a69450b
5
+ SHA512:
6
+ metadata.gz: 45e79a88708e35ad52016f2af849c36365a9e2763617752bb6c696934f496abda8b16f4ce23d3653b60fa824c6eb11177c3f8d0444c220458831fd1a710ed904
7
+ data.tar.gz: 1a5263521182fd8d51f0d65b3dc6c828b2d3a7b9df48ad0568b7b08a1d929d81dcf4f8c3e21913e7bb21e3ecad8a8fd98cdb5542f6a7299e61957502af946b86
data/README.md CHANGED
@@ -50,6 +50,14 @@ Add a rake task for start worker process
50
50
  end
51
51
  end
52
52
 
53
+ To monitor worker thread events:
54
+
55
+ Message.worker.callbacks[:start] << lambda {|job_name, options| "start event with options" }
56
+ Message.worker.callbacks[:crash] << lambda {|job_name, e| "crash event with error" }
57
+ Message.worker.callbacks[:stop] << lambda {|job_name| "stop event" }
58
+
59
+ By default, there are event log callbacks installed.
60
+
53
61
  ### Queuing jobs
54
62
 
55
63
 
@@ -96,4 +104,3 @@ For some environment or queue system (e.g. AWS SQS), you will need set an applic
96
104
  By change queue adapter:
97
105
 
98
106
  Message.queue.adapter = :sqs
99
-
@@ -52,6 +52,10 @@ module Message
52
52
  @jobs ||= RUBY_PLATFORM =~ /java/ ? java.util.concurrent.ConcurrentHashMap.new : {}
53
53
  end
54
54
 
55
+ def callbacks
56
+ @callbacks ||= {:start => [], :crash => [], :stop => []}
57
+ end
58
+
55
59
  def reset
56
60
  @default_job = nil
57
61
  @sync = nil
@@ -59,6 +63,10 @@ module Message
59
63
  end
60
64
  end
61
65
 
66
+ callbacks[:start] << lambda {|job_name, options| Message.log(:info) { "[Worker] start in #{delay}" } }
67
+ callbacks[:crash] << lambda {|job_name, e| Message.log(:error) { "[Worker] crashed: #{e.message}\n#{e.backtrace.join("\n")}"} }
68
+ callbacks[:stop] << lambda {|job_name| Message.log(:info) { "[Worker] stopped" } }
69
+
62
70
  attr_reader :job_name
63
71
 
64
72
  def initialize(job_name)
@@ -66,22 +74,28 @@ module Message
66
74
  end
67
75
 
68
76
  def start(options={})
77
+ Thread.start do
78
+ self.work_in_thread(options) do |size|
79
+ process(size)
80
+ end
81
+ end
82
+ end
83
+
84
+ def work_in_thread(options, &block)
69
85
  size = options[:size] || DEFAULT_PROCESS_SIZE
70
86
  interval = options[:interval] || DEFAULT_PROCESS_INTERVAL
71
87
  delay = options[:delay] || 10 + rand(20)
72
- Thread.start do
73
- begin
74
- Message.log(:info) { "[Worker] start in #{delay}" }
75
- sleep delay
76
- loop do
77
- process(size)
78
- sleep interval
79
- end
80
- rescue => e
81
- Message.log(:error) { "[Worker] crashed: #{e.message}\n#{e.backtrace.join("\n")}"}
82
- ensure
83
- Message.log(:info) { "[Worker] stopped" }
88
+ begin
89
+ callback(:start, options)
90
+ sleep delay if delay > 0
91
+ loop do
92
+ yield(size)
93
+ sleep interval
84
94
  end
95
+ rescue => e
96
+ callback(:crash, e)
97
+ ensure
98
+ callback(:stop)
85
99
  end
86
100
  end
87
101
 
@@ -99,6 +113,12 @@ module Message
99
113
  alias :<< :enq
100
114
 
101
115
  private
116
+ def callback(name, *args)
117
+ self.class.callbacks[name].each do |c|
118
+ c.call(job_name, *args)
119
+ end
120
+ end
121
+
102
122
  def job
103
123
  self.class.jobs[@job_name] ||= Message.job(@job_name, &job_processor)
104
124
  end
@@ -114,4 +134,4 @@ module Message
114
134
  obj.send(m, *args)
115
135
  end
116
136
  end
117
- end
137
+ end
metadata CHANGED
@@ -1,29 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Xiao Li
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ! 'Message provides flexible & reliable background/asynchronous job processing
15
- mechanism on top of simple queue interface.
16
-
17
-
18
- Any developer can create queue adapter for Message to hook up different messaging/queue
19
- system.
13
+ description: |
14
+ Message provides flexible & reliable background/asynchronous job processing mechanism on top of simple queue interface.
20
15
 
16
+ Any developer can create queue adapter for Message to hook up different messaging/queue system.
21
17
 
22
18
  One in-memory queue is included with Message for you to start development and test,
23
-
24
19
  and you can easily swap in other queues later.
25
-
26
- '
27
20
  email:
28
21
  - swing1979@gmail.com
29
22
  executables: []
@@ -31,38 +24,37 @@ extensions: []
31
24
  extra_rdoc_files: []
32
25
  files:
33
26
  - README.md
27
+ - lib/message.rb
28
+ - lib/message/filters.rb
34
29
  - lib/message/filters/benchmarking.rb
35
30
  - lib/message/filters/error_handling.rb
36
31
  - lib/message/filters/retry_on_error.rb
37
- - lib/message/filters.rb
38
32
  - lib/message/in_memory_queue.rb
39
33
  - lib/message/job.rb
40
34
  - lib/message/q.rb
41
35
  - lib/message/worker.rb
42
- - lib/message.rb
43
36
  homepage: https://github.com/xli/message
44
37
  licenses:
45
38
  - MIT
39
+ metadata: {}
46
40
  post_install_message:
47
41
  rdoc_options: []
48
42
  require_paths:
49
43
  - lib
50
44
  required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
45
  requirements:
53
- - - ! '>='
46
+ - - ">="
54
47
  - !ruby/object:Gem::Version
55
48
  version: '0'
56
49
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
50
  requirements:
59
- - - ! '>='
51
+ - - ">="
60
52
  - !ruby/object:Gem::Version
61
53
  version: '0'
62
54
  requirements: []
63
55
  rubyforge_project:
64
- rubygems_version: 1.8.23
56
+ rubygems_version: 2.2.2
65
57
  signing_key:
66
- specification_version: 3
58
+ specification_version: 4
67
59
  summary: Simplify Ruby messaging/queue/async/job processing.
68
60
  test_files: []