cloudist 0.0.3 → 0.1.0

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.
data/README.md CHANGED
@@ -39,31 +39,56 @@ This will start and AMQP connection and EM loop then yield everything inside it.
39
39
 
40
40
  In your worker:
41
41
 
42
- Cloudist.worker {
43
- job('make.sandwich') {
44
- # Make sandwich here
45
-
46
- # Your worker has access to the data sent from the server in the 'data' attribute
47
- data # => {:bread => "white", :sauce => 'bbq'}
48
-
49
- # Fire the finished event
50
- finished!
42
+ Cloudist.start {
43
+ log.info("Started Worker")
44
+
45
+ worker {
46
+ job('make.sandwich') {
47
+ log.info("JOB (#{id}) Make sandwich with #{data[:bread]} bread")
48
+ log.debug(data.inspect)
49
+
50
+ # Do long running tasks here
51
+ EM.defer {
52
+ progress(0)
53
+ started!
54
+ progress(10)
55
+ sleep(1)
56
+ progress(20)
57
+ sleep(5)
58
+ progress(90)
59
+ sleep(1)
60
+ finished!
61
+ progress(100)
62
+ }
63
+ }
51
64
  }
52
65
  }
53
66
 
54
67
  In your application:
55
-
56
- job = Cloudist.enqueue('make.sandwich', :bread => "white", :sauce => 'bbq')
57
68
 
58
- Cloudist.listen(job) {
59
- event('finished') {
60
- # Called when we finish making a sandwich
69
+ Cloudist.start {
70
+ # Enqueue sandwich job
71
+ log.info("Dispatching sandwich making job...")
72
+ enqueue('make.sandwich', {:bread => 'white'})
73
+
74
+ # Listen to all sandwich jobs
75
+ listen('make.sandwich') {
76
+ progress {
77
+ Cloudist.log.info("Progress: #{data[:progress]}")
78
+ }
79
+
80
+ event('started') {
81
+ Cloudist.log.info("Started making sandwich at #{Time.now.to_s}")
82
+ }
83
+
84
+ event('finished'){
85
+ Cloudist.log.info("Finished making sandwich at #{Time.now.to_s}")
86
+ }
61
87
  }
88
+
62
89
  }
63
-
64
- You don't need to listen to responses immediately, if you store the job_id you can listen to responses at any time in the near future.
65
90
 
66
- You can also queue jobs outside an EventMachine loop using Cloudist.enqueue but this will be very slow as it has to connect to your message queue first.
91
+ If your application provides an AMQP.start loop already, you can skip the Cloudist.start
67
92
 
68
93
  Acknowledgements
69
94
  -------
@@ -73,8 +98,6 @@ Portions of this gem are based on code from the following projects:
73
98
  - Heroku's Droid gem
74
99
  - Lizzy
75
100
  - Minion
76
- - Nanite
77
- - Smith
78
101
 
79
102
  Contributing to Cloudist
80
103
  ------------------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cloudist}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ivan Vanderbyl"]
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "examples/sandwich_worker.rb",
32
32
  "lib/cloudist.rb",
33
33
  "lib/cloudist/basic_queue.rb",
34
+ "lib/cloudist/callback.rb",
34
35
  "lib/cloudist/core_ext/string.rb",
35
36
  "lib/cloudist/errors.rb",
36
37
  "lib/cloudist/job.rb",
@@ -13,8 +13,6 @@ $:.unshift File.dirname(__FILE__) + '/../lib'
13
13
  require "rubygems"
14
14
  require "cloudist"
15
15
 
16
- ENV['AMQP_URL'] = 'amqp://test_pilot:t35t_p1l0t!@ec2-50-16-8-137.compute-1.amazonaws.com:5672/ivan'
17
-
18
16
  Cloudist.signal_trap!
19
17
 
20
18
  Cloudist.start {
@@ -24,8 +22,17 @@ Cloudist.start {
24
22
 
25
23
  # Listen to all sandwich jobs
26
24
  listen('make.sandwich') {
27
- # Cloudist.log.info("Make sandwich event: #{data[:event]}")
28
- Cloudist.log.debug(queue_header.headers.inspect)
25
+ progress {
26
+ Cloudist.log.info("Progress: #{data[:progress]}")
27
+ }
28
+
29
+ event('started') {
30
+ Cloudist.log.info("Started making sandwich at #{Time.now.to_s}")
31
+ }
32
+
33
+ event('finished'){
34
+ Cloudist.log.info("Finished making sandwich at #{Time.now.to_s}")
35
+ }
29
36
  }
30
37
 
31
38
  }
@@ -14,8 +14,6 @@ $:.unshift File.dirname(__FILE__) + '/../lib'
14
14
  require "rubygems"
15
15
  require "cloudist"
16
16
 
17
- ENV['AMQP_URL'] = 'amqp://test_pilot:t35t_p1l0t!@ec2-50-16-8-137.compute-1.amazonaws.com:5672/ivan'
18
-
19
17
  Cloudist.signal_trap!
20
18
 
21
19
  Cloudist.start {
@@ -29,9 +27,15 @@ Cloudist.start {
29
27
  log.debug(data.inspect)
30
28
 
31
29
  EM.defer {
30
+ progress(0)
32
31
  started!
32
+ progress(10)
33
+ sleep(1)
34
+ progress(20)
33
35
  sleep(5)
36
+ progress(90)
34
37
  finished!
38
+ progress(100)
35
39
  }
36
40
  }
37
41
  }
@@ -18,6 +18,7 @@ require "cloudist/payload"
18
18
  require "cloudist/request"
19
19
  require "cloudist/worker"
20
20
  require "cloudist/listener"
21
+ require "cloudist/callback"
21
22
  require "cloudist/job"
22
23
 
23
24
  module Cloudist
@@ -0,0 +1,28 @@
1
+ module Cloudist
2
+ class Callback
3
+
4
+ attr_reader :payload, :source
5
+
6
+ def initialize(source)
7
+ @source = source
8
+ end
9
+
10
+ def call(payload)
11
+ @payload = payload
12
+ instance_eval(&source)
13
+ end
14
+
15
+ def data
16
+ payload.body
17
+ end
18
+
19
+ def headers
20
+ payload.headers
21
+ end
22
+
23
+ def runtime
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -1,9 +1,13 @@
1
1
  module Cloudist
2
2
  class Listener
3
3
 
4
- attr_reader :job_queue_name, :job_id
4
+ attr_reader :job_queue_name, :job_id, :callbacks
5
+
6
+ @@valid_callbacks = ["event", "progress", "reply", "update"]
5
7
 
6
8
  def initialize(job_or_queue_name)
9
+ @callbacks = {}
10
+
7
11
  if job_or_queue_name.is_a?(Cloudist::Job)
8
12
  @job_queue_name = Utils.reply_prefix(job_or_queue_name.payload.headers[:master_queue])
9
13
  @job_id = job_or_queue_name.id
@@ -19,9 +23,31 @@ module Cloudist
19
23
  reply_queue = Cloudist::ReplyQueue.new(job_queue_name)
20
24
  reply_queue.setup(job_id) if job_id
21
25
 
22
- reply_queue.subscribe do |request|
23
- # job = Job.new(request.payload)
24
- request.instance_eval(&block)
26
+ self.instance_eval(&block)
27
+
28
+ reply_queue.subscribe do |request|
29
+ payload = request.payload
30
+
31
+ key = [payload.message_type.to_s, payload.headers[:event]].compact.join(':')
32
+
33
+ if callbacks.has_key?(key)
34
+ callbacks_to_call = callbacks[key]
35
+ callbacks_to_call.each do |c|
36
+ c.call(payload)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def method_missing(meth, *args, &blk)
43
+ if @@valid_callbacks.include?(meth.to_s)
44
+
45
+ # callback should in format of "event:started" or "progress"
46
+ key = [meth.to_s, args.shift].compact.join(':')
47
+
48
+ (@callbacks[key] ||= []) << Callback.new(blk)
49
+ else
50
+ super
25
51
  end
26
52
  end
27
53
 
@@ -105,6 +105,10 @@ module Cloudist
105
105
  headers[:reply_to]
106
106
  end
107
107
 
108
+ def message_type
109
+ headers[:message_type]
110
+ end
111
+
108
112
  def event_hash
109
113
  @event_hash ||= headers[:event_hash] || body.delete('event_hash') || create_event_hash
110
114
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudist
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Vanderbyl
@@ -206,6 +206,7 @@ files:
206
206
  - examples/sandwich_worker.rb
207
207
  - lib/cloudist.rb
208
208
  - lib/cloudist/basic_queue.rb
209
+ - lib/cloudist/callback.rb
209
210
  - lib/cloudist/core_ext/string.rb
210
211
  - lib/cloudist/errors.rb
211
212
  - lib/cloudist/job.rb