From b4e740094f1b08aeb6ab8313c5343a0d0cb72107 Mon Sep 17 00:00:00 2001 From: Stephenitis Date: Fri, 30 Jan 2015 15:23:13 -0800 Subject: [PATCH 1/2] added bulk create method for posting 1+ N tasks in single api call --- lib/iron_worker_ng/api_client.rb | 13 +++++++++++++ lib/iron_worker_ng/client.rb | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/iron_worker_ng/api_client.rb b/lib/iron_worker_ng/api_client.rb index ab5e750..a83aa62 100644 --- a/lib/iron_worker_ng/api_client.rb +++ b/lib/iron_worker_ng/api_client.rb @@ -82,9 +82,22 @@ def tasks_get(id) end def tasks_create(code_name, payload, options = {}) + # creates single task + # could not make tasks_create(code_name, payload, options = {}) backwards compatible for batch tasks an array payload value could potentially be a legitimate payload rather than an array of tasks @stephenitis 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) + # batch post tasks + 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")) diff --git a/lib/iron_worker_ng/client.rb b/lib/iron_worker_ng/client.rb index 1d6a9b5..21cb5f1 100644 --- a/lib/iron_worker_ng/client.rb +++ b/lib/iron_worker_ng/client.rb @@ -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.is_a?(Array) ? params : params.to_json, options) + + OpenStruct.new(res) + end + def tasks_create_legacy(code_name, params = {}, options = {}) IronCore::Logger.debug 'IronWorkerNG', "Calling tasks.create_legacy with code_name='#{code_name}', params='#{params.to_s}' and options='#{options.to_s}'" From 05d99512301c95bb32ec2a95122524d5ae948118 Mon Sep 17 00:00:00 2001 From: Stephenitis Date: Fri, 30 Jan 2015 15:41:13 -0800 Subject: [PATCH 2/2] documentation on bulk_create method --- README.md | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0ece488..df41bdc 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,7 @@ Specific methods for binary (freeform) runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.sh' @@ -312,7 +312,7 @@ Specific methods for go runtime. It'll run provided exec via 'go run'. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.go' @@ -347,7 +347,7 @@ Specific methods for mono (.net) runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.exe' @@ -360,7 +360,7 @@ Specific methods for node runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.js' @@ -373,7 +373,7 @@ Specific methods for perl runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.pl' @@ -386,7 +386,7 @@ Specific methods for PHP runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.php' @@ -399,7 +399,7 @@ Specific methods for python runtime. ### merge_exec(path) ### exec(path) -Merge the exec located at `path`. +Merge the exec located at `path`. ```ruby code.merge_exec 'my_worker.py' @@ -559,8 +559,24 @@ Queue a new task for the code package specified by `code_name`, passing the `par ```ruby task = client.tasks.create('MyWorker', {:client => 'Joe'}, {:delay => 180}) puts task.id +# => # ``` +```ruby +task = client.tasks.create('MyWorker', {:client => 'Joe'}, {:delay => 180}) +puts task.id +``` + +### tasks.bulk_create(code_name, array_of_params = [], options = {}) + +```ruby +task_ids = client.tasks.bulk_create('hello_ruby', [{:hello => "world"}, {:hello => "world"}, {:hello => "world"}], {:cluster => "mem1"} ) +puts tasks_ids +# => #"54cc11b8855dc73d9209ce0d"}, {"id"=>"54cc11b8855dc73d9209ce0e"}, {"id"=>"54cc11b8855dc73d9209ce0f"}}], msg="Queued up"> +``` + +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. + ### tasks.cancel(task_id) Cancel the task specified by `task_id`.