message 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +20 -0
  2. data/lib/message/worker.rb +15 -5
  3. metadata +2 -2
data/README.md CHANGED
@@ -20,6 +20,26 @@ and you can easily swap in other queues later.
20
20
 
21
21
  ## How to use
22
22
 
23
+ ### Initialization
24
+
25
+ Add an initializer config/initializers/message.rb
26
+
27
+ Message.logger = Rails.logger
28
+ # Setup default job queue name, default: "message-worker-default"
29
+ Message.worker.default_job = "app-name-#{Rails.env}-message-worker-default"
30
+ # Bypass queue system and process job synchronously when you called .async
31
+ # default: false
32
+ Message.worker.sync = !Rails.env.production?
33
+ # Change to sqs adapter for asynchronous calls, default is a thread-safe in memory queue named :in_memory
34
+ Message.queue.adapter = :sqs
35
+
36
+ # Start a worker thread in production
37
+ if Rails.env.production? && $0 !~ /rake$/
38
+ # Sleep 15 seconds for processed every 10 jobs or less
39
+ # default: size = 10, interval = 5
40
+ Message.worker.start(:size => 10, :interval => 15)
41
+ end
42
+
23
43
  ### Queuing jobs
24
44
 
25
45
 
@@ -3,6 +3,8 @@ require 'yaml'
3
3
  module Message
4
4
  class Worker
5
5
  DEFAULT_JOB_NAME = 'message-worker-default'
6
+ DEFAULT_PROCESS_SIZE = 10
7
+ DEFAULT_PROCESS_INTERVAL = 5
6
8
 
7
9
  module Enqueue
8
10
  class Enq
@@ -63,7 +65,9 @@ module Message
63
65
  @job_name = job_name
64
66
  end
65
67
 
66
- def start(size=10, interval=1)
68
+ def start(options={})
69
+ size = options[:size] || DEFAULT_PROCESS_SIZE
70
+ interval = options[:interval] || DEFAULT_PROCESS_INTERVAL
67
71
  Thread.start do
68
72
  begin
69
73
  log(:info) { "start" }
@@ -83,8 +87,10 @@ module Message
83
87
  end
84
88
 
85
89
  def enq(work)
86
- job.enq(YAML.dump(work)).tap do
87
- process if self.class.sync
90
+ if self.class.sync
91
+ process_work(work)
92
+ else
93
+ job.enq(YAML.dump(work))
88
94
  end
89
95
  end
90
96
  alias :<< :enq
@@ -96,11 +102,15 @@ module Message
96
102
 
97
103
  def job_processor
98
104
  lambda do |msg|
99
- obj, m, args = YAML.load(msg)
100
- obj.send(m, *args)
105
+ process_work(YAML.load(msg))
101
106
  end
102
107
  end
103
108
 
109
+ def process_work(work)
110
+ obj, m, args = work
111
+ obj.send(m, *args)
112
+ end
113
+
104
114
  def log(level, &block)
105
115
  Message.logger.send(level) { "[Worker(#{Thread.current.object_id})] #{block.call}" }
106
116
  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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-13 00:00:00.000000000 Z
12
+ date: 2014-09-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Message provides flexible & reliable background/asynchronous job processing
15
15
  mechanism on top of simple queue interface.