iron_worker 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -126
- data/lib/iron_worker.rb +1 -0
- data/lib/iron_worker/client.rb +461 -0
- data/lib/iron_worker/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 810932e73c25acb5793479cc87e1a8a15c4a544a
|
4
|
+
data.tar.gz: a88f5046897d4b62de76c9e8e8621af28fd81230
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ebe5ec38e723ecd40ac2cc51c0f0a5d83e9cb5097117ba74a0e5cb51e894492556dbce799bcae355213450450d5ba51d8ee644d73e39de3f058dea451c50885
|
7
|
+
data.tar.gz: c17994e5fc634ff370a193fff2932d537dc989c37f183a6c1d61fc6f92fc32cd7d95c0cab073ed2a039163c39e413a61f18244e4ac222d910b835ca07a17d1ed
|
data/README.md
CHANGED
@@ -6,149 +6,41 @@ To run your code in cloud you need to do three things:
|
|
6
6
|
- **Upload code package**
|
7
7
|
- **Queue or schedule tasks** for execution
|
8
8
|
|
9
|
-
|
9
|
+
You can read how to create, package and upload your worker here: http://dev.iron.io/worker/getting_started/
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
This gem has two parts to it, one to access the [IronWorker API[(http://dev.iron.io/worker/reference/api) and
|
12
|
+
the other to help you with your Ruby IronWorker's.
|
13
13
|
|
14
14
|
# Preparing Your Environment
|
15
15
|
|
16
|
-
You'll need to register at http://iron.io/ and get your credentials to use IronWorker. Each account can have an unlimited number
|
16
|
+
You'll need to register at http://iron.io/ and get your credentials to use IronWorker. Each account can have an unlimited number
|
17
|
+
of projects, so take advantage of it by creating separate projects for development, testing and production.
|
18
|
+
Each project is identified by a unique project ID and requires your access token before it will perform any action,
|
19
|
+
like uploading or queuing workers.
|
17
20
|
|
18
|
-
|
21
|
+
Install using the following command.
|
19
22
|
|
20
23
|
```sh
|
21
24
|
gem install iron_worker
|
22
25
|
```
|
23
26
|
|
27
|
+
# IronWorker Helper Functions
|
24
28
|
|
25
|
-
|
29
|
+
These functions will help you read in worker payloads and things for when your worker is running. To use these functions
|
30
|
+
simple require this gem in your worker and then use the helper functions `IronWorker.payload`, `IronWorker.config` and
|
31
|
+
`IronWorker.id`. For example, this is a complete IronWorker script:
|
26
32
|
|
27
|
-
TODO: Drop this, should use new cli tool for queueing.
|
28
|
-
|
29
|
-
You can quicky queue up a task for your worker from the command line using:
|
30
|
-
|
31
|
-
iron_worker queue hello
|
32
|
-
|
33
|
-
Use the `-p` parameter to pass in a payload:
|
34
|
-
|
35
|
-
iron_worker queue hello -p "{\"hi\": \"world\"}"
|
36
|
-
|
37
|
-
Use the `--wait` parameter to queue a task, wait for it to complete and print the log.
|
38
|
-
|
39
|
-
iron_worker queue hello -p "{\"hi\": \"world\"}" --wait
|
40
|
-
|
41
|
-
### Queue up a task from code
|
42
|
-
|
43
|
-
Most commonly you'll be queuing up tasks from code though, so you can do this:
|
44
|
-
|
45
|
-
```ruby
|
46
|
-
require "iron_worker_ng"
|
47
|
-
client = IronWorker::Client.new
|
48
|
-
100.times do
|
49
|
-
client.tasks.create("hello", "foo"=>"bar")
|
50
|
-
end
|
51
|
-
```
|
52
|
-
|
53
|
-
### Setting Task Priority
|
54
|
-
|
55
|
-
TODO: Drop this, use new cli tool
|
56
|
-
|
57
|
-
You can specify priority of the task using `--priority` parameter:
|
58
|
-
|
59
|
-
```ruby
|
60
|
-
iron_worker queue hello --priority 0 # default value, lowest priority
|
61
|
-
iron_worker queue hello --priority 1 --label 'medium priority task' # medium priority
|
62
|
-
```
|
63
|
-
|
64
|
-
Value of priority parameter means the priority queue to run the task in. Valid values are 0, 1, and 2. 0 is the default.
|
65
|
-
|
66
|
-
From code you can set the priority like it done in snippet below:
|
67
|
-
|
68
|
-
```ruby
|
69
|
-
client.tasks.create("hello", some_params, priority: 2) # highest priority
|
70
|
-
```
|
71
|
-
|
72
|
-
### Setting additional Options
|
73
|
-
|
74
|
-
|
75
|
-
You can specify not only priority:
|
76
|
-
|
77
|
-
- **priority**: Setting the priority of your job. Valid values are 0, 1, and 2. The default is 0.
|
78
|
-
- **timeout**: The maximum runtime of your task in seconds. No task can exceed 3600 seconds (60 minutes). The default is 3600 but can be set to a shorter duration.
|
79
|
-
- **delay**: The number of seconds to delay before actually queuing the task. Default is 0.
|
80
|
-
- **label**: Optional text label for your task.
|
81
|
-
- **cluster**: cluster name ex: "high-mem" or "dedicated". If not set default is set to "default" which is the public IronWorker cluster.
|
82
|
-
|
83
|
-
## Get task status
|
84
|
-
|
85
|
-
|
86
|
-
TODO: Drop, use new cli
|
87
|
-
|
88
|
-
When you call `iron_worker queue X`, you'll see the task ID in the output which you can use to get the status.
|
89
|
-
|
90
|
-
iron_worker info task 5032f7360a4681382838e082
|
91
|
-
|
92
|
-
## Get task log
|
93
|
-
|
94
|
-
|
95
|
-
TODO: Drop, use new cli
|
96
|
-
|
97
|
-
Similar to getting status, get the task ID in the queue command output, then:
|
98
|
-
|
99
|
-
iron_worker log 5032f7360a4681382838e082 --wait
|
100
|
-
|
101
|
-
## Retry a Task
|
102
|
-
|
103
|
-
|
104
|
-
TODO: Drop, use new cli
|
105
|
-
|
106
|
-
You can retry task by id using same payload and options:
|
107
|
-
|
108
|
-
iron_worker retry 5032f7360a4681382838e082
|
109
|
-
|
110
|
-
or
|
111
33
|
```ruby
|
112
|
-
|
113
|
-
|
114
|
-
## Pause or Resume task processing
|
115
|
-
|
116
|
-
|
117
|
-
TODO: Drop, use new cli
|
34
|
+
require 'iron_worker'
|
118
35
|
|
119
|
-
|
120
|
-
|
121
|
-
iron_worker pause hello
|
122
|
-
|
123
|
-
iron_worker resume hello
|
124
|
-
|
125
|
-
or by code:
|
126
|
-
Pause or resume for the code package specified by `code_id`.
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
response = client.codes.pause_task_queue('1234567890')
|
130
|
-
response = client.codes.resume_task_queue('1234567890')
|
131
|
-
```
|
132
|
-
|
133
|
-
|
134
|
-
### Debugging
|
135
|
-
|
136
|
-
To get a bunch of extra output to debug things, turn it on using:
|
137
|
-
|
138
|
-
IronCore::Logger.logger.level = ::Logger::DEBUG
|
139
|
-
|
140
|
-
|
141
|
-
# Queue Up Tasks for Your Worker
|
142
|
-
|
143
|
-
Now that the code is uploaded, we can create/queue up tasks. You can call this over and over
|
144
|
-
for as many tasks as you want.
|
145
|
-
|
146
|
-
```ruby
|
147
|
-
client.tasks.create('MyWorker', {:client => 'Joe'})
|
36
|
+
puts "Here is the payload: #{IronWorker.payload}"
|
37
|
+
puts "Here is the config: #{IronWorker.config}"
|
148
38
|
```
|
149
39
|
|
40
|
+
# The IronWorker API
|
150
41
|
|
151
|
-
|
42
|
+
This client will enable you to use the IronWorker API in Ruby.
|
43
|
+
Full API documentation is here: http://dev.iron.io/worker/reference/api/
|
152
44
|
|
153
45
|
## IronWorker::Client
|
154
46
|
|
data/lib/iron_worker.rb
CHANGED
@@ -0,0 +1,461 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'base64'
|
3
|
+
require 'tmpdir'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
require 'iron_worker/api_client'
|
7
|
+
|
8
|
+
module IronWorker
|
9
|
+
class ClientProxyCaller
|
10
|
+
def initialize(client, prefix)
|
11
|
+
@client = client
|
12
|
+
@prefix = prefix
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(name, *args, &block)
|
16
|
+
full_name = @prefix.to_s + '_' + name.to_s
|
17
|
+
if @client.respond_to?(full_name)
|
18
|
+
@client.send(full_name, *args, &block)
|
19
|
+
else
|
20
|
+
super(name, *args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Client
|
26
|
+
attr_reader :api
|
27
|
+
|
28
|
+
def initialize(options = {}, &block)
|
29
|
+
@api = IronWorker::APIClient.new(options)
|
30
|
+
|
31
|
+
unless block.nil?
|
32
|
+
instance_eval(&block)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def options
|
37
|
+
@api.options
|
38
|
+
end
|
39
|
+
|
40
|
+
def token
|
41
|
+
@api.token
|
42
|
+
end
|
43
|
+
|
44
|
+
def project_id
|
45
|
+
@api.project_id
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(name, *args, &block)
|
49
|
+
if args.length == 0
|
50
|
+
IronWorker::ClientProxyCaller.new(self, name)
|
51
|
+
else
|
52
|
+
super(name, *args, &block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def stacks_list
|
57
|
+
@api.stacks_list
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def codes_list(options = {})
|
62
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.list with options='#{options.to_s}'"
|
63
|
+
|
64
|
+
all = options[:all] || options['all']
|
65
|
+
|
66
|
+
if all
|
67
|
+
result = []
|
68
|
+
|
69
|
+
page = options[:page] || options['page'] || 0
|
70
|
+
per_page = options[:per_page] || options['per_page'] || 100
|
71
|
+
|
72
|
+
while true
|
73
|
+
next_codes = codes_list(options.merge({:page => page}).delete_if { |name| name == :all || name == 'all' })
|
74
|
+
|
75
|
+
result += next_codes
|
76
|
+
|
77
|
+
break if next_codes.length != per_page
|
78
|
+
page += 1
|
79
|
+
end
|
80
|
+
|
81
|
+
result
|
82
|
+
else
|
83
|
+
@api.codes_list(options)['codes'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def codes_get(code_id)
|
88
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.get with code_id='#{code_id}'"
|
89
|
+
|
90
|
+
c = @api.codes_get(code_id)
|
91
|
+
c['_id'] = c['id']
|
92
|
+
OpenStruct.new(c)
|
93
|
+
end
|
94
|
+
|
95
|
+
def codes_create(code, options = {})
|
96
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.create with code='#{code.to_s}' and options='#{options.to_s}'"
|
97
|
+
|
98
|
+
if options[:config] && options[:config].is_a?(Hash)
|
99
|
+
options = options.dup
|
100
|
+
options[:config] = options[:config].to_json
|
101
|
+
end
|
102
|
+
|
103
|
+
options.merge!(stack:code.stack) if code.stack
|
104
|
+
|
105
|
+
container_file = code.create_container
|
106
|
+
|
107
|
+
if code.zip_package
|
108
|
+
res = nil
|
109
|
+
IronWorker::Fetcher.fetch_to_file(code.zip_package) do |file|
|
110
|
+
res = @api.codes_create(code.name, file, 'sh', '__runner__.sh', options)
|
111
|
+
end
|
112
|
+
elsif code.remote_build_command.nil? && (not code.full_remote_build)
|
113
|
+
res = @api.codes_create(code.name, container_file, 'sh', '__runner__.sh', options)
|
114
|
+
else
|
115
|
+
builder_code_name = code.name + (code.name[0 .. 0].upcase == code.name[0 .. 0] ? '::Builder' : '::builder')
|
116
|
+
|
117
|
+
@api.codes_create(builder_code_name, container_file, 'sh', '__runner__.sh', options)
|
118
|
+
|
119
|
+
builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api.options.to_json, :codes_create_options => options.to_json)
|
120
|
+
|
121
|
+
builder_task = tasks.wait_for(builder_task._id)
|
122
|
+
|
123
|
+
if builder_task.status != 'complete'
|
124
|
+
log = tasks.log(builder_task._id)
|
125
|
+
|
126
|
+
File.unlink(container_file)
|
127
|
+
|
128
|
+
IronCore::Logger.error 'IronWorker', "Error while remote building worker\n" + log, IronCore::Error
|
129
|
+
end
|
130
|
+
|
131
|
+
res = JSON.parse(builder_task.msg)
|
132
|
+
end
|
133
|
+
|
134
|
+
File.unlink(container_file) if code.zip_package.nil?
|
135
|
+
|
136
|
+
res['_id'] = res['id']
|
137
|
+
OpenStruct.new(res)
|
138
|
+
end
|
139
|
+
|
140
|
+
def codes_create_async(code, options = {})
|
141
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.create_async with code='#{code.to_s}' and options='#{options.to_s}'"
|
142
|
+
|
143
|
+
if options[:config] && options[:config].is_a?(Hash)
|
144
|
+
options = options.dup
|
145
|
+
options[:config] = options[:config].to_json
|
146
|
+
end
|
147
|
+
|
148
|
+
options.merge!(stack:code.stack) if code.stack
|
149
|
+
|
150
|
+
container_file = code.create_container
|
151
|
+
|
152
|
+
if code.remote_build_command.nil? && (not code.full_remote_build)
|
153
|
+
res = @api.codes_create(code.name, container_file, 'sh', '__runner__.sh', options)
|
154
|
+
else
|
155
|
+
builder_code_name = code.name + (code.name[0 .. 0].upcase == code.name[0 .. 0] ? '::Builder' : '::builder')
|
156
|
+
|
157
|
+
@api.codes_create(builder_code_name, container_file, 'sh', '__runner__.sh', options)
|
158
|
+
|
159
|
+
builder_task = tasks.create(builder_code_name, :code_name => code.name, :client_options => @api.options.to_json, :codes_create_options => options.to_json)
|
160
|
+
|
161
|
+
File.unlink(container_file)
|
162
|
+
|
163
|
+
return builder_task._id
|
164
|
+
end
|
165
|
+
|
166
|
+
File.unlink(container_file)
|
167
|
+
|
168
|
+
res['_id'] = res['id']
|
169
|
+
OpenStruct.new(res)
|
170
|
+
end
|
171
|
+
|
172
|
+
def codes_patch(name, options = {})
|
173
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.patch with name='#{name}' and options='#{options.to_s}'"
|
174
|
+
|
175
|
+
code = codes.list(per_page: 100).find { |c| c.name == name }
|
176
|
+
|
177
|
+
if code.nil?
|
178
|
+
IronCore::Logger.error 'IronWorker', "Can't find code with name='#{name}' to patch", IronCore::Error
|
179
|
+
end
|
180
|
+
|
181
|
+
patcher_code_name = name + (name[0 .. 0].upcase == name[0 .. 0] ? '::Patcher' : '::patcher')
|
182
|
+
|
183
|
+
exec_dir = ::Dir.tmpdir + '/' + ::Dir::Tmpname.make_tmpname('iron-worker-ng-', 'exec')
|
184
|
+
exec_file_name = exec_dir + '/patchcer.rb'
|
185
|
+
|
186
|
+
FileUtils.mkdir_p(exec_dir)
|
187
|
+
|
188
|
+
exec_file = File.open(exec_file_name, 'w')
|
189
|
+
exec_file.write <<EXEC_FILE
|
190
|
+
# #{IronWorker.full_version}
|
191
|
+
|
192
|
+
File.open('.gemrc', 'w') do |gemrc|
|
193
|
+
gemrc.puts('gem: --no-ri --no-rdoc')
|
194
|
+
end
|
195
|
+
|
196
|
+
`gem install iron_worker_ng`
|
197
|
+
|
198
|
+
require 'iron_worker_ng'
|
199
|
+
|
200
|
+
client = IronWorker::Client.new(JSON.parse(params[:client_options]))
|
201
|
+
|
202
|
+
original_code = client.codes.get(params[:code_id])
|
203
|
+
original_code_data = client.codes.download(params[:code_id])
|
204
|
+
|
205
|
+
`mkdir code`
|
206
|
+
original_code_zip = File.open('code/code.zip', 'w')
|
207
|
+
original_code_zip.write(original_code_data)
|
208
|
+
original_code_zip.close
|
209
|
+
`cd code && unzip code.zip && rm code.zip && cd ..`
|
210
|
+
|
211
|
+
patch_params = JSON.parse(params[:patch])
|
212
|
+
patch_params.each {|k, v| system("cat patch/\#{k} > code/\#{v}")}
|
213
|
+
|
214
|
+
code_container = IronWorker::Code::Container::Zip.new
|
215
|
+
|
216
|
+
Dir['code/*'].each do |entry|
|
217
|
+
code_container.add(entry[5 .. -1], entry)
|
218
|
+
end
|
219
|
+
|
220
|
+
code_container.close
|
221
|
+
|
222
|
+
res = client.api.codes_create(original_code.name, code_container.name, 'sh', '__runner__.sh', :config => original_code.config)
|
223
|
+
|
224
|
+
res['_id'] = res['id']
|
225
|
+
res = OpenStruct.new(res)
|
226
|
+
|
227
|
+
client.tasks.set_progress(iron_task_id, :msg => res.marshal_dump.to_json)
|
228
|
+
EXEC_FILE
|
229
|
+
exec_file.close
|
230
|
+
|
231
|
+
patcher_code = IronWorker::Code::Base.new
|
232
|
+
patcher_code.runtime = :ruby
|
233
|
+
patcher_code.name = patcher_code_name
|
234
|
+
patcher_code.exec(exec_file_name)
|
235
|
+
options[:patch].keys.each {|v| patcher_code.file(v, 'patch')}
|
236
|
+
patch_params = Hash[options[:patch].map {|k,v| [File.basename(k), v]}]
|
237
|
+
|
238
|
+
patcher_container_file = patcher_code.create_container
|
239
|
+
|
240
|
+
@api.codes_create(patcher_code_name, patcher_container_file, 'sh', '__runner__.sh', {})
|
241
|
+
|
242
|
+
FileUtils.rm_rf(exec_dir)
|
243
|
+
File.unlink(patcher_container_file)
|
244
|
+
|
245
|
+
patcher_task = tasks.create(patcher_code_name, :code_id => code._id, :client_options => @api.options.to_json, patch: patch_params.to_json)
|
246
|
+
patcher_task = tasks.wait_for(patcher_task._id)
|
247
|
+
|
248
|
+
if patcher_task.status != 'complete'
|
249
|
+
log = tasks.log(patcher_task._id)
|
250
|
+
IronCore::Logger.error 'IronWorker', "Error while patching worker\n" + log, IronCore::Error
|
251
|
+
end
|
252
|
+
|
253
|
+
res = JSON.parse(patcher_task.msg)
|
254
|
+
res['_id'] = res['id']
|
255
|
+
OpenStruct.new(res)
|
256
|
+
end
|
257
|
+
|
258
|
+
def codes_delete(code_id)
|
259
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.delete with code_id='#{code_id}'"
|
260
|
+
|
261
|
+
@api.codes_delete(code_id)
|
262
|
+
|
263
|
+
true
|
264
|
+
end
|
265
|
+
|
266
|
+
def codes_revisions(code_id, options = {})
|
267
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.revisions with code_id='#{code_id}' and options='#{options.to_s}'"
|
268
|
+
|
269
|
+
@api.codes_revisions(code_id, options)['revisions'].map { |c| OpenStruct.new(c.merge('_id' => c['id'])) }
|
270
|
+
end
|
271
|
+
|
272
|
+
def codes_download(code_id, options = {})
|
273
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.download with code_id='#{code_id}' and options='#{options.to_s}'"
|
274
|
+
|
275
|
+
@api.codes_download(code_id, options)
|
276
|
+
end
|
277
|
+
|
278
|
+
def codes_pause_task_queue(code_id, options = {})
|
279
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.pause_task_queue with code_id='#{code_id}' and options='#{options.to_s}'"
|
280
|
+
|
281
|
+
res = @api.codes_pause_task_queue(code_id, options)
|
282
|
+
OpenStruct.new(res)
|
283
|
+
end
|
284
|
+
|
285
|
+
def codes_resume_task_queue(code_id, options = {})
|
286
|
+
IronCore::Logger.debug 'IronWorker', "Calling codes.resume_task_queue with code_id='#{code_id}' and options='#{options.to_s}'"
|
287
|
+
|
288
|
+
res = @api.codes_resume_task_queue(code_id, options)
|
289
|
+
OpenStruct.new(res)
|
290
|
+
end
|
291
|
+
|
292
|
+
def tasks_list(options = {})
|
293
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.list with options='#{options.to_s}'"
|
294
|
+
|
295
|
+
@api.tasks_list(options)['tasks'].map { |t| OpenStruct.new(t.merge('_id' => t['id'])) }
|
296
|
+
end
|
297
|
+
|
298
|
+
def tasks_get(task_id)
|
299
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.get with task_id='#{task_id}'"
|
300
|
+
|
301
|
+
t = @api.tasks_get(task_id)
|
302
|
+
t['_id'] = t['id']
|
303
|
+
OpenStruct.new(t)
|
304
|
+
end
|
305
|
+
|
306
|
+
def tasks_create(code_name, params = {}, options = {})
|
307
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"
|
308
|
+
|
309
|
+
res = @api.tasks_create(code_name, params.is_a?(String) ? params : params.to_json, options)
|
310
|
+
|
311
|
+
t = res['tasks'][0]
|
312
|
+
t['_id'] = t['id']
|
313
|
+
OpenStruct.new(t)
|
314
|
+
end
|
315
|
+
|
316
|
+
def tasks_create_legacy(code_name, params = {}, options = {})
|
317
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"
|
318
|
+
|
319
|
+
res = @api.tasks_create(code_name, params_for_legacy(code_name, params), options)
|
320
|
+
|
321
|
+
t = res['tasks'][0]
|
322
|
+
t['_id'] = t['id']
|
323
|
+
OpenStruct.new(t)
|
324
|
+
end
|
325
|
+
|
326
|
+
def tasks_cancel(task_id)
|
327
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.cancel with task_id='#{task_id}'"
|
328
|
+
|
329
|
+
@api.tasks_cancel(task_id)
|
330
|
+
|
331
|
+
true
|
332
|
+
end
|
333
|
+
|
334
|
+
def tasks_cancel_all(code_id)
|
335
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.cancel_all with code_id='#{code_id}'"
|
336
|
+
|
337
|
+
@api.tasks_cancel_all(code_id)
|
338
|
+
|
339
|
+
true
|
340
|
+
end
|
341
|
+
|
342
|
+
def tasks_log(task_id)
|
343
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.log with task_id='#{task_id}'"
|
344
|
+
|
345
|
+
if block_given?
|
346
|
+
@api.tasks_log(task_id) { |chunk| yield(chunk) }
|
347
|
+
else
|
348
|
+
@api.tasks_log(task_id)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
def tasks_set_progress(task_id, options = {})
|
353
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.set_progress with task_id='#{task_id}' and options='#{options.to_s}'"
|
354
|
+
|
355
|
+
@api.tasks_set_progress(task_id, options)
|
356
|
+
|
357
|
+
true
|
358
|
+
end
|
359
|
+
|
360
|
+
def tasks_wait_for(task_id, options = {}, &block)
|
361
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.wait_for with task_id='#{task_id}' and options='#{options.to_s}'"
|
362
|
+
|
363
|
+
options[:sleep] ||= options['sleep'] || 5
|
364
|
+
|
365
|
+
task = tasks_get(task_id)
|
366
|
+
|
367
|
+
while task.status == 'queued' || task.status == 'running'
|
368
|
+
block.call(task) unless block.nil?
|
369
|
+
sleep options[:sleep]
|
370
|
+
task = tasks_get(task_id)
|
371
|
+
end
|
372
|
+
|
373
|
+
task
|
374
|
+
end
|
375
|
+
|
376
|
+
def tasks_retry(task_id, options = {})
|
377
|
+
IronCore::Logger.debug 'IronWorker', "Calling tasks.retry with task_id='#{task_id}' and options='#{options.to_s}'"
|
378
|
+
|
379
|
+
res = @api.tasks_retry(task_id, options)
|
380
|
+
|
381
|
+
t = res['tasks'][0]
|
382
|
+
t['_id'] = t['id']
|
383
|
+
OpenStruct.new(t)
|
384
|
+
end
|
385
|
+
|
386
|
+
def schedules_list(options = {})
|
387
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.list with options='#{options.to_s}'"
|
388
|
+
|
389
|
+
@api.schedules_list(options)['schedules'].map { |s| OpenStruct.new(s.merge('_id' => s['id'])) }
|
390
|
+
end
|
391
|
+
|
392
|
+
def schedules_get(schedule_id)
|
393
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.get with schedule_id='#{schedule_id}"
|
394
|
+
|
395
|
+
s = @api.schedules_get(schedule_id)
|
396
|
+
s['_id'] = s['id']
|
397
|
+
OpenStruct.new(s)
|
398
|
+
end
|
399
|
+
|
400
|
+
def schedules_create(code_name, params = {}, options = {})
|
401
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.create with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"
|
402
|
+
|
403
|
+
res = @api.schedules_create(code_name, params.is_a?(String) ? params : params.to_json, options)
|
404
|
+
|
405
|
+
s = res['schedules'][0]
|
406
|
+
s['_id'] = s['id']
|
407
|
+
OpenStruct.new(s)
|
408
|
+
end
|
409
|
+
|
410
|
+
def schedules_update(id, options = {})
|
411
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.update with id='#{id}', options='#{options.to_s}'"
|
412
|
+
|
413
|
+
res = @api.schedules_update(id, options)
|
414
|
+
|
415
|
+
OpenStruct.new(res)
|
416
|
+
end
|
417
|
+
|
418
|
+
def schedules_create_legacy(code_name, params = {}, options = {})
|
419
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"
|
420
|
+
|
421
|
+
res = @api.schedules_create(code_name, params_for_legacy(code_name, params), options)
|
422
|
+
|
423
|
+
s = res['schedules'][0]
|
424
|
+
s['_id'] = s['id']
|
425
|
+
OpenStruct.new(s)
|
426
|
+
end
|
427
|
+
|
428
|
+
def schedules_cancel(schedule_id)
|
429
|
+
IronCore::Logger.debug 'IronWorker', "Calling schedules.cancel with schedule_id='#{schedule_id}"
|
430
|
+
|
431
|
+
@api.schedules_cancel(schedule_id)
|
432
|
+
|
433
|
+
true
|
434
|
+
end
|
435
|
+
|
436
|
+
def projects_get
|
437
|
+
IronCore::Logger.debug 'IronWorker', "Calling projects.get"
|
438
|
+
|
439
|
+
res = @api.projects_get
|
440
|
+
|
441
|
+
res['_id'] = res['id']
|
442
|
+
OpenStruct.new(res)
|
443
|
+
end
|
444
|
+
|
445
|
+
def params_for_legacy(code_name, params)
|
446
|
+
if params.is_a?(String)
|
447
|
+
params = JSON.parse(params)
|
448
|
+
end
|
449
|
+
|
450
|
+
attrs = {}
|
451
|
+
|
452
|
+
params.keys.each do |k|
|
453
|
+
attrs['@' + k.to_s] = params[k]
|
454
|
+
end
|
455
|
+
|
456
|
+
attrs = attrs.to_json
|
457
|
+
|
458
|
+
{:class_name => code_name, :attr_encoded => Base64.encode64(attrs), :sw_config => {:project_id => project_id, :token => token}}.to_json
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|
data/lib/iron_worker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iron_worker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Reeder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: iron_core
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- iron_worker.gemspec
|
53
53
|
- lib/iron_worker.rb
|
54
54
|
- lib/iron_worker/api_client.rb
|
55
|
+
- lib/iron_worker/client.rb
|
55
56
|
- lib/iron_worker/version.rb
|
56
57
|
- lib/iron_worker/worker_helper.rb
|
57
58
|
homepage: https://github.com/iron-io/iron_worker_ruby
|