Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bulk create method for posting 1+ N tasks in single api call #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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
# => #<OpenStruct id="54cc1638855dc73d920a0870", _id="54cc1638855dc73d920a0870">
```

```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
# => #<OpenStruct tasks=[{"id"=>"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`.
Expand Down
13 changes: 13 additions & 0 deletions lib/iron_worker_ng/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
8 changes: 8 additions & 0 deletions lib/iron_worker_ng/client.rb
Original file line number Diff line number Diff line change
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.is_a?(Array) ? params : params.to_json, options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. How about passing just params without checking params.is_a?(Array), if you pass string it will raise en error NoMethodError: undefined method `map!' for String


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}'"

Expand Down