Skip to content

Commit

Permalink
Merge pull request #40 from sunloverz/task_bulk_create
Browse files Browse the repository at this point in the history
Add task bulk create.
  • Loading branch information
treeder committed Apr 2, 2015
2 parents 40a766c + 8128862 commit 2495c45
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -133,6 +133,17 @@ task = client.tasks.create('MyWorker', {:client => 'Joe'}, {:delay => 180})
puts task.id
```

### tasks.bulk_create(code_name, array_of_params = [], options = {})

Queue more than 1 tasks in a single api call for the code package specified by `code_name`, passing an array of params/payloads and returning a tasks object with the ids of each task queued.
Visit http://dev.iron.io/worker/reference/api/#queue_a_task for more information about the available options.

```ruby
task_ids = client.tasks.bulk_create('hello_ruby', [{:hello => "world"}, {:hello => "world"}, {:hello => "world"}], {:cluster => "mem1"} )
puts tasks_ids
# => #<OpenStruct tasks=[{"id"=>"54cc11b8855dc73d9209ce0d"}, {"id"=>"54cc11b8855dc73d9209ce0e"}, {"id"=>"54cc11b8855dc73d9209ce0f"}}], msg="Queued up">
```

### tasks.cancel(task_id)

Cancel the task specified by `task_id`.
Expand Down
10 changes: 10 additions & 0 deletions lib/iron_worker/api_client.rb
Expand Up @@ -88,6 +88,16 @@ def tasks_create(code_name, payload, options = {})
parse_response(post("projects/#{@project_id}/tasks", {:tasks => [{:code_name => code_name, :payload => payload}.merge(options)]}))
end

def tasks_bulk_create(code_name, array_of_payloads, options)
array_of_tasks = array_of_payloads.map! do |payload|
{
:code_name => code_name,
:payload => payload.is_a?(String) ? payload : payload.to_json
}.merge(options)
end
parse_response(post("projects/#{@project_id}/tasks", {:tasks => array_of_tasks}))
end

def tasks_cancel(id)
check_id(id)
parse_response(post("projects/#{@project_id}/tasks/#{id}/cancel"))
Expand Down
8 changes: 8 additions & 0 deletions lib/iron_worker/client.rb
Expand Up @@ -313,6 +313,14 @@ def tasks_create(code_name, params = {}, options = {})
OpenStruct.new(t)
end

def tasks_bulk_create(code_name, params = [], options = {})
IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.bulk_create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

res = @api.tasks_bulk_create(code_name, params, options)

OpenStruct.new(res)
end

def tasks_create_legacy(code_name, params = {}, options = {})
IronCore::Logger.debug 'IronWorker', "Calling tasks.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'"

Expand Down

0 comments on commit 2495c45

Please sign in to comment.