celery_client 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzdiZDVkMjIxMTcxOWFiYmJhMDE3YTFiMmExZDRhYjU5MzcwYTJmOA==
5
+ data.tar.gz: !binary |-
6
+ ODZkMjIxMjUxNmJkYmNkZjQ3N2E3MTRmNmYyMzk3ZGFkNmU2Yzg1Nw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDVlMmUwY2YzZDIyNzMyODY4YzdjNjUzZDE2N2Y5MzQyMjczMGQ5ZmMzZDNj
10
+ ZTA5NGI4ZmE3NTNiNjViYTA0OTA3NTFjODgzNzg4ZDAxMzU4MzY1M2E0NmZk
11
+ MTk2NjMxZjVhYTA1ZWRmNzg3NTZhYmQ1MjZlMzYzZTg2ZmUyNWE=
12
+ data.tar.gz: !binary |-
13
+ OTY5MmQzZTc5OGRjYmZmZDAzZjI5YmNlYmE1M2IzZjZmZTRkZDVlOWM2OGZm
14
+ YjNmNTdkNTk2N2ViY2FlYzliMThmMjc4ZjQ4MzBlZTRmZTgxNGNlYWQ1OTYx
15
+ OGEyZTliOTk5ZDBiZWE4OWRiMTRlYWU5YzE4YjU2YTYwYmY1Y2Q=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in celery_client.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ celery_client (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rake (10.1.0)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.5)
16
+ rspec-expectations (2.14.3)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.3)
25
+ celery_client!
26
+ rake
27
+ rspec (~> 2.6)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jestin Woods
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # CeleryClient
2
+
3
+ This gem provides methods to execute celery tasks that are proxied by a HTTP Gateway. See an example of this here: https://github.com/celery/celery/tree/master/examples/celery_http_gateway/.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'celery_client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install celery_client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'celery_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "celery_client"
8
+ spec.version = CeleryClient::VERSION
9
+ spec.authors = ["Jestin Woods"]
10
+ spec.email = ["jestin.woods@jivesoftware.com"]
11
+ spec.description = %q{Execute celery tasks}
12
+ spec.summary = %q{Execute celery tasks}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.6"
24
+ end
@@ -0,0 +1,17 @@
1
+ require "pathname"
2
+
3
+
4
+ require "celery_client/version"
5
+ require "celery_client/http"
6
+ require "celery_client/task_manager"
7
+
8
+
9
+ module CeleryClient
10
+ lib_path = Pathname.new(File.expand_path("../celery_client", __FILE__))
11
+ autoload :HTTP, lib_path.join("http")
12
+ autoload :HTTP, lib_path.join("task_manager")
13
+
14
+ def self.source_root
15
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
16
+ end
17
+ end
@@ -0,0 +1,42 @@
1
+ require 'net/http'
2
+
3
+
4
+ module CeleryClient
5
+ class HTTP
6
+ def initialize(config)
7
+ @url = config.fetch('url')
8
+ @username = config.fetch('username')
9
+ @password = config.fetch('password')
10
+ end
11
+
12
+ def get(path, params={})
13
+ uri = URI("#{@url}#{path}")
14
+ uri.query = URI.encode_www_form(params)
15
+ request = Net::HTTP::Get.new(uri.request_uri)
16
+ make_request(request, uri)
17
+ end
18
+
19
+ def post(path, params={})
20
+ uri = URI("#{@url}#{path}")
21
+ request = Net::HTTP::Post.new(uri.request_uri)
22
+ request.set_form_data(params)
23
+ make_request(request, uri)
24
+ end
25
+
26
+ private
27
+
28
+ def make_request(request, uri)
29
+ request.basic_auth @username, @password
30
+ response = Net::HTTP.start(uri.host, uri.port,
31
+ :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
32
+ http.request(request)
33
+ end
34
+ case response
35
+ when Net::HTTPSuccess, Net::HTTPRedirection
36
+ response.body
37
+ else
38
+ response.value
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,146 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+
5
+ module CeleryClient
6
+ class TaskManager
7
+ def initialize(connection, wait=true, timeout=60, verbose=false)
8
+ @connection = connection
9
+ @timeout = Integer(timeout)
10
+ @tasks = {}
11
+ @wait = wait
12
+ if @wait.is_a?(String)
13
+ @wait = to_boolean(@wait)
14
+ end
15
+ @verbose = verbose
16
+ if @verbose.is_a?(String)
17
+ @verbose = to_boolean(@verbose)
18
+ end
19
+ end
20
+
21
+ def run(context, task_name, params)
22
+ if @verbose
23
+ puts "Executing #{context}/#{task_name} with payload #{params}"
24
+ end
25
+ result = @connection.post(
26
+ get_run_path(context, task_name), params)
27
+ task_id = get_task_id(result)
28
+ save_task(task_id, context, task_name, params)
29
+ task_id
30
+ end
31
+
32
+ def status(task_id)
33
+ check_task(task_id)
34
+ check_for_status(task_id)
35
+ end
36
+
37
+ def result(task_id)
38
+ check_task(task_id)
39
+ check_for_result(task_id)
40
+ end
41
+
42
+ private
43
+
44
+ def to_boolean(string)
45
+ !!(string =~ /^(true|t|yes|y|1)$/i)
46
+ end
47
+
48
+ def check_task(task_id)
49
+ counter = 0
50
+ while true
51
+ response = @connection.get(
52
+ get_status_path(get_context(task_id), task_id))
53
+ status = get_task_status(response)
54
+ set_status(task_id, status)
55
+ set_result(task_id, get_task_result(response))
56
+ if not @wait or counter >= @timeout or status != 'PENDING'
57
+ break
58
+ end
59
+ if @verbose
60
+ puts "Current status is #{status}"
61
+ end
62
+ sleep 1
63
+ counter += 1
64
+ end
65
+ end
66
+
67
+ def get_run_path(context, task_name)
68
+ "/#{context}/manage/apply/#{task_name}"
69
+ end
70
+
71
+ def get_status_path(context, task_id)
72
+ "/#{context}/manage/status/#{task_id}"
73
+ end
74
+
75
+ def get_task_id(result)
76
+ get_task_key(result, 'task_id')
77
+ end
78
+
79
+ def get_task_result(result)
80
+ get_task_key(result, 'result')
81
+ end
82
+
83
+ def get_task_status(result)
84
+ get_task_key(result, 'status')
85
+ end
86
+
87
+ def get_task_key(result, key)
88
+ results = JSON.parse(result)
89
+ begin
90
+ value = results.fetch('task').fetch(key)
91
+ rescue KeyError
92
+ begin
93
+ value = results.fetch(key)
94
+ rescue KeyError
95
+ abort("Unable to get #{key} from task. Got this instead: #{result}.")
96
+ end
97
+ end
98
+ value
99
+ end
100
+
101
+ def get_context(task_id)
102
+ begin
103
+ context = check_for_task(task_id).fetch(:context)
104
+ rescue KeyError
105
+ abort("No context for task: #{task_id}")
106
+ end
107
+ context
108
+ end
109
+
110
+ def set_status(task_id, status)
111
+ @tasks[task_id][:data][:status] = status
112
+ end
113
+
114
+ def set_result(task_id, result)
115
+ @tasks[task_id][:data][:result] = result
116
+ end
117
+
118
+ def save_task(task_id, context, task_name, params)
119
+ @tasks[task_id] = {:context => context, :task_name => task_name, :params => params, :data => {}}
120
+ end
121
+
122
+ def check_for_task(task_id)
123
+ begin
124
+ return @tasks.fetch(task_id)
125
+ rescue KeyError
126
+ abort("No task: #{task_id}")
127
+ end
128
+ end
129
+
130
+ def check_for_status(task_id)
131
+ begin
132
+ return check_for_task(task_id).fetch(:data).fetch(:status)
133
+ rescue KeyError
134
+ abort("No status for task: #{task_id}")
135
+ end
136
+ end
137
+
138
+ def check_for_result(task_id)
139
+ begin
140
+ return @tasks.fetch(task_id).fetch(:data).fetch(:result)
141
+ rescue KeyError
142
+ abort("No result for task: #{task_id}")
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,3 @@
1
+ module CeleryClient
2
+ VERSION = "0.0.1"
3
+ end
Binary file
@@ -0,0 +1,20 @@
1
+ require 'celery_client'
2
+
3
+
4
+ describe CeleryClient::HTTP do
5
+ c = CeleryClient::HTTP.new({'url' => 'http://tools.dev.jivehosted.com', 'username' => 'admin', 'password' => 'admin'})
6
+ it "test get request" do
7
+ c.get('/health/status').class.should eql(String)
8
+ end
9
+
10
+ t = CeleryClient::TaskManager.new(c)
11
+ task_id = t.run('zenoss', 'Zenoss.tasks.sync_installation', {'installationname' => 'devonp-puppet-test'})
12
+ it "test sync installation" do
13
+ t.run('zenoss', 'Zenoss.tasks.sync_installation', {'installationname' => 'devonp-puppet-test'}).class.should eql(String)
14
+ end
15
+
16
+ it "test get task result" do
17
+ t.status(task_id).class.should eql(String)
18
+ end
19
+ end
20
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: celery_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jestin Woods
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ description: Execute celery tasks
56
+ email:
57
+ - jestin.woods@jivesoftware.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - celery_client.gemspec
68
+ - lib/celery_client.rb
69
+ - lib/celery_client/http.rb
70
+ - lib/celery_client/task_manager.rb
71
+ - lib/celery_client/version.rb
72
+ - pkg/celery_client-0.0.1.gem
73
+ - spec/celery_client_http_spec.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Execute celery tasks
98
+ test_files:
99
+ - spec/celery_client_http_spec.rb