cloudq_client 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Tom Wilson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/lib/cloudq.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ require 'cloudq/connection'
5
+ require 'cloudq/base'
6
+ require 'cloudq/publish'
7
+ require 'cloudq/consume'
8
+
@@ -0,0 +1,9 @@
1
+ module Cloudq
2
+ class Base
3
+ attr_accessor :queue
4
+
5
+ def initialize(queue = nil)
6
+ @queue = queue
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Cloudq
2
+ module Connection
3
+ extend self
4
+
5
+ attr_accessor :url
6
+ end
7
+ end
8
+
@@ -0,0 +1,40 @@
1
+ module Cloudq
2
+ class Consume < Base
3
+ def job
4
+ get do |a_job|
5
+ perform a_job
6
+ delete a_job["id"]
7
+ end
8
+ end
9
+
10
+ private
11
+ def perform(a_job)
12
+ klass = Object.const_get(a_job["klass"])
13
+ klass.perform(a_job["args"])
14
+ end
15
+
16
+ def get(&block)
17
+ RestClient.get url do |response|
18
+ if response.code == 200
19
+ result = JSON.parse(response)
20
+ return nil if result['status'] == 'empty'
21
+ yield result
22
+ result
23
+ end
24
+ end
25
+ end
26
+
27
+ def delete(job_id)
28
+ RestClient.delete [url, job_id].join('/')
29
+ end
30
+
31
+ def url
32
+ [Cloudq::Connection.url, @queue].join('/')
33
+ end
34
+
35
+
36
+
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,20 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ module Cloudq
5
+ class Publish < Base
6
+ def job(klass, *args)
7
+ post(:job => { :klass => klass, :args => args} )
8
+ end
9
+
10
+ private
11
+ def post(job)
12
+ headers = {:content_type => :json, :accept => :json}
13
+ RestClient.post [Cloudq::Connection.url, @queue].join('/'), job, headers do |response|
14
+ JSON.parse(response)['status'] == 'success'
15
+ end
16
+ end
17
+
18
+
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ module Cloudq
2
+ VERSION = '0.0.3'
3
+ end
4
+
data/readme.md ADDED
@@ -0,0 +1,56 @@
1
+ # Cloudq Client
2
+
3
+ This is the Cloudq Client Gem, it has both the consumer and publisher
4
+ modules.
5
+
6
+ ## The Job
7
+
8
+ The Job is the JSON message that is published to the queue server to be
9
+ consumed by a worker. The queue server can have one to many queues and
10
+ you can create one to many workers. With Cloudq you can have these
11
+ workers all over the internet. The Cloudq server is a rack application
12
+ so you can do awesome things with rack middleware to add authentication,
13
+ encryption, logging, etc. All using Rack Middleware.
14
+
15
+ The job message is simple: (It is a json object )
16
+
17
+ { 'job': { 'klass': 'Archive', 'args': [1] }}
18
+
19
+ Args can be a hash or array.
20
+
21
+ ## The Consumer
22
+
23
+ The consumer will reserve a job from the queue then perform the job and
24
+ delete it from the queue.
25
+
26
+ ### Sample Consumer Worker
27
+
28
+ require 'cloudq/consume'
29
+ # You must require the files that have the Job you need to perform
30
+
31
+ require 'donut/job'
32
+
33
+ Cloudq::Connection.url = 'http://donuts.com'
34
+
35
+ loop do
36
+ Cloudq::Consume.new(:make_the_donuts).job
37
+ sleep 5
38
+ end
39
+
40
+ ## The Publisher
41
+
42
+ The publisher will create a job on the queue, the great thing about the
43
+ publisher, is that it does not have to know the klass or the job that
44
+ you want your worker to perform. It just published the job on the
45
+ queue.
46
+
47
+ ### Sample Publisher
48
+
49
+ require 'cloudq/publish'
50
+
51
+ Cloudq::Connection.url = 'http://donuts.com'
52
+
53
+ Cloudq::Publish.new(:make_donuts).job(:make_donuts, 'Bake', :type =>
54
+ 'glazed')
55
+
56
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudq_client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Tom Wilson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-18 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.5.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 1.6.1
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: "The Cloudq will "
39
+ email:
40
+ - tom@jackhq.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - lib/cloudq/base.rb
49
+ - lib/cloudq/connection.rb
50
+ - lib/cloudq/consume.rb
51
+ - lib/cloudq/publish.rb
52
+ - lib/cloudq/version.rb
53
+ - lib/cloudq.rb
54
+ - LICENSE
55
+ - readme.md
56
+ has_rdoc: true
57
+ homepage: http://github.com/twilson63/cloudq_client
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.3.6
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.6.2
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A Ruby Interface to the Cloudq
84
+ test_files: []
85
+