message 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +21 -2
  2. data/lib/message/worker.rb +45 -16
  3. data/lib/message.rb +6 -1
  4. metadata +1 -1
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Message
2
2
 
3
+ https://travis-ci.org/xli/message.svg?branch=master
4
+
3
5
  Message provides flexible & reliable background/asynchronous job processing mechanism on top of simple queue interface.
4
6
 
5
7
  Any developer can create queue adapter for Message to hook up different messaging/queue system.
@@ -11,24 +13,41 @@ and you can easily swap in other queues later.
11
13
  ## Installation
12
14
 
13
15
 
16
+ ### Use AWS SQS as back-end queue system
17
+
18
+ gem 'message-sqs'
14
19
 
15
20
  ## How to use
16
21
 
17
22
  ### Queuing jobs
18
23
 
19
24
 
20
- Inspired by delayed_job API, call .enq.method(params) on any object and it will be processed in the background.
25
+ Inspired by delayed_job API, call .async.method(params) on any object and it will be processed in the background.
21
26
 
22
27
  # without message
23
28
  @img.resize(36)
24
29
 
25
30
  # with message
26
- @img.enq.resize(36)
31
+ @img.async.resize(36)
27
32
 
28
33
  ### Start worker to process jobs
29
34
 
30
35
  Message.worker.start
31
36
 
37
+ ### Change to synchronize mode
38
+
39
+ Message.worker.synch = true
40
+
41
+ This is designed for test environment or Rails development environment.
42
+ After set the synch option to true, the async jobs will be processed immediately when .async.method(params) is called.
43
+ The default value is false.
44
+
45
+ ### Change default worker job name
46
+
47
+ For some environment or queue system (e.g. AWS SQS), you will need set an application specific job name, so that you can share same account for multiple applications using Message.
48
+
49
+ Message.worker.default_job = "app-name-#{Rails.env}-message-default"
50
+
32
51
  ## Job interface specification
33
52
 
34
53
  Job = a queue + message processor
@@ -18,38 +18,49 @@ module Message
18
18
  unless @obj.respond_to?(m)
19
19
  raise NoMethodError, "undefined method `#{m}' for #{@obj.inspect}"
20
20
  end
21
- Worker.enq(@job, [@obj, m, args])
21
+ Message.worker(@job) << [@obj, m, args]
22
22
  end
23
23
  end
24
24
 
25
- def enq(job=DEFAULT_JOB_NAME)
26
- Enq.new(self, job)
25
+ def async(job=nil)
26
+ Enq.new(self, job || Message.worker.default_job)
27
27
  end
28
28
  end
29
29
 
30
30
  class << self
31
- def jobs
32
- @jobs ||= RUBY_PLATFORM =~ /java/ ? java.util.concurrent.ConcurrentHashMap.new : {}
31
+ attr_accessor :default_job, :synch
32
+
33
+ def default_job
34
+ @default_job ||= DEFAULT_JOB_NAME
33
35
  end
34
36
 
35
- def job(name)
36
- jobs[name] ||= Message.job(name, &job_processor)
37
+ def default
38
+ new(default_job)
37
39
  end
38
40
 
39
- def enq(name, work)
40
- job(name).enq(YAML.dump(work))
41
+ def process(*args)
42
+ default.process(*args)
41
43
  end
42
44
 
43
- def job_processor
44
- lambda do |msg|
45
- obj, m, args = YAML.load(msg)
46
- obj.send(m, *args)
47
- end
45
+ def start(*args)
46
+ default.start(*args)
47
+ end
48
+
49
+ def jobs
50
+ @jobs ||= RUBY_PLATFORM =~ /java/ ? java.util.concurrent.ConcurrentHashMap.new : {}
51
+ end
52
+
53
+ def reset
54
+ @default_job = nil
55
+ @synch = nil
56
+ @jobs = nil
48
57
  end
49
58
  end
50
59
 
60
+ attr_reader :job_name
61
+
51
62
  def initialize(job_name)
52
- @job_name = job_name || DEFAULT_JOB_NAME
63
+ @job_name = job_name
53
64
  end
54
65
 
55
66
  def start(size=10, interval=1)
@@ -68,10 +79,28 @@ module Message
68
79
  end
69
80
 
70
81
  def process(size=1)
71
- Worker.job(@job_name).process(size)
82
+ job.process(size)
72
83
  end
73
84
 
85
+ def enq(work)
86
+ job.enq(YAML.dump(work)).tap do
87
+ process if self.class.synch
88
+ end
89
+ end
90
+ alias :<< :enq
91
+
74
92
  private
93
+ def job
94
+ self.class.jobs[@job_name] ||= Message.job(@job_name, &job_processor)
95
+ end
96
+
97
+ def job_processor
98
+ lambda do |msg|
99
+ obj, m, args = YAML.load(msg)
100
+ obj.send(m, *args)
101
+ end
102
+ end
103
+
75
104
  def log(level, &block)
76
105
  Message.logger.send(level) { "[Worker(#{Thread.current.object_id})] #{block.call}" }
77
106
  end
data/lib/message.rb CHANGED
@@ -22,7 +22,11 @@ module Message
22
22
  end
23
23
 
24
24
  def worker(job=nil)
25
- Worker.new(job)
25
+ if job
26
+ Worker.new(job)
27
+ else
28
+ Worker
29
+ end
26
30
  end
27
31
 
28
32
  def logger
@@ -36,6 +40,7 @@ module Message
36
40
  def reset
37
41
  Message.queue.reset
38
42
  Message.job.reset
43
+ Message.worker.reset
39
44
  end
40
45
 
41
46
  reset
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: