NAME
IO::Iron::IronWorker::Client - IronWorker (Online Worker Platform) Client.
VERSION
version 0.14
SYNOPSIS
require IO::Iron::IronWorker::Client;
my $ironworker_client = IO::Iron::IronWorker::Client->new();
# or
use IO::Iron qw(ironworker);
my $iron_worker_client = ironworker();
my $unique_code_package_name = 'HelloWorldCode';
my $worker_as_zip; # Zipped Perl script and dependencies.
my $unique_code_executable_file_name = 'HelloWorldCode.pl';
my $uploaded = $iron_worker_client->update_code_package(
'name' => $unique_code_package_name,
'file' => $worker_as_zip,
'file_name' => $unique_code_executable_file_name,
'runtime' => 'perl',
);
my $code_package_id;
my @code_packages = $iron_worker_client->list_code_packages();
foreach (@code_packages) {
if($_->{'name'} eq $unique_code_package_name) {
$code_package_id = $_->{'id'};
last;
}
}
my $code_package = $iron_worker_client->get_info_about_code_package(
'id' => $code_package_id
);
my @code_package_revisions = $iron_worker_client->
list_code_package_revisions( 'id' => $code_package_id );
my ($downloaded, $file_name) = $iron_worker_client->download_code_package(
'id' => $code_package_id,
'revision' => 1,
);
my $delete_rval = $iron_worker_client->delete( 'id' => $code_package_id );
# Tasks
my $task_payload = 'Task payload (can be JSONized)';
my $task = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
#
# additional parameters for a task:
# 'priority', # The priority queue to run the task in.
# Valid values are 0, 1, and 2. 0 is the default.
# 'timeout', # The maximum runtime of your task in seconds.
# 'delay', # The number of seconds to delay before actually
# queuing the task. Default is 0.
# For scheduled task:
# 'priority', # The priority queue to run the task in.
# Valid values are 0, 1, and 2. 0 is the default.
# 'run_every', # The amount of time, in seconds, between runs
# 'end_at', # The time tasks will stop being queued.
# Should be a time or datetime.
# 'run_times', # The number of times a task will run.
# 'start_at', # The time the scheduled task should first be run.
# 'name', # Name of task or scheduled task.
);
# When queuing, the task object is updated with returned id.
my $task_id = $iron_worker_client->queue( 'tasks' => $task );
# Or:
my $task1 = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
);
my $task2 = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
);
my @task_ids = $iron_worker_client->queue( 'tasks' => [ $task1, $task2 ] );
# Or:
my $number_of_tasks_queued = $iron_worker_client->queue(
'tasks' => [ $task1, $task2 ]
);
#
$task_id = $task->id();
my $task_info = $iron_worker_client->get_info_about_task( 'id' => $task_id );
until ($task_info->{'status'} =~ /(complete|error|killed|timeout)/) {
sleep 3;
$task_info = $iron_worker_client->get_info_about_task( 'id' => $task_id );
}
# $task->status() updates the task's information.
my $task_duration = $task->duration();
my $task_end_time = $task->end_time();
my $task_updated_at = $task->updated_at();
my $task_log = $task->log(); # Log is text/plain
my $cancelled = $task->cancel();
my $progress_set = $task->progress( {
'percent' => 25,
'msg' => 'Not even halfway through!',
} );
my $retried = $task->retry();
$task_id = $task->id(); # New task id after retry().
$task_info = $iron_worker_client->get_info_about_task( 'id' => $task_id );
# Schedule task.
my $schedule_task = $iron_worker_client->create_task(
$unique_code_package_name,
$task_payload,
'priority' => 0,
'run_every' => 120, # Every two minutes.
);
$schedule_task->run_times(5);
use DateTime;
use DateTime::Format::ISO8601;
my $dt = DateTime->now;
$dt->add( hours => 1 );
my $start_dt = DateTime::Format::ISO8601->format_datetime( $dt );
$dt->add( hours => 3 );
my $end_dt = DateTime::Format::ISO8601->format_datetime( $dt );
$schedule_task->start_at( $start_dt );
$schedule_task->end_at( $end_dt );
#
# When scheduling, the task object is updated with returned id.
$schedule_task = $iron_worker_client->schedule( 'tasks' => $schedule_task);
# Or:
my $schedule_task1 = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
'start_at' => $start_dt,
);
my $schedule_task2 = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
'start_at' => $start_dt,
);
my @scheduled_tasks = $iron_worker_client->schedule(
'tasks' => [$schedule_task1, $schedule_task2]
);
# Or:
my $number_of_scheduled_tasks = $iron_worker_client->schedule(
'tasks' => [$schedule_task1, $schedule_task2]
);
#
my $scheduled_task_info = $iron_worker_client->
get_info_about_scheduled_task( 'id' => $task_id );
my $from_time = time - (24*60*60);
my $to_time = time - (1*60*60);
my @tasks = $iron_worker_client->tasks(
'code_name' => $unique_code_package_name, # Mandatory
'status' => qw{queued running complete error cancelled killed timeout},
'from_time' => $from_time, # Number of seconds since the Unix epoc
'to_time' => $to_time, # Number of seconds since the Unix epoc
);
@scheduled_tasks = $iron_worker_client->scheduled_tasks();
DESCRIPTION
IO::Iron::IronWorker is a client for the IronWorker remote worker system at http://www.iron.io/. IronWorker is a cloud based parallel multi-language worker platform. with a REST API. IO::Iron::IronWorker creates a Perl object for interacting with IronWorker. All IronWorker functions are available.
The class IO::Iron::IronWorker::Client instantiates the 'project', IronWorker access configuration.
IronWorker Cloud Parallel Workers
IronWorker is a parallel worker platform delivered as a service to Internet connecting applications via its REST interface. Built with distributed cloud applications in mind, it provides on-demand scalability for workers, controls with HTTPS transport and cloud-optimized performance. [see http://www.iron.io/]
Using the IronWorker Client Library
IO::Iron::IronWorker::Client is a normal Perl package meant to be used as an object.
require IO::Iron::IronWorker::Client;
my $ironworker_client = IO::Iron::IronWorker::Client->new();
Please see IO::Iron for further parameters and general usage.
Commands
After creating the client three sets of commands is available:
- Commands for operating code packages:
-
- IO::Iron::IronWorker::Client::list_code_packages()
- IO::Iron::IronWorker::Client::update_code_package(params)
- IO::Iron::IronWorker::Client::get_info_about_code_package('id' => code_package_id)
- IO::Iron::IronWorker::Client::delete_code_package('id' => code_package_id)
- IO::Iron::IronWorker::Client::download_code_package('id' => code_package_id, params)
- IO::Iron::IronWorker::Client::list_code_package_revisions('id' => code_package_id)
- Commands for operating tasks:
-
- IO::Iron::IronWorker::Client::create_task('code_name' => $name, 'payload' => $payload, ...)
- IO::Iron::IronWorker::Client::tasks('code_name' => $code_name, ...)
- IO::Iron::IronWorker::Client::queue(tasks => $task | [@tasks] )
- IO::Iron::IronWorker::Client::get_info_about_task('id => $id)
- IO::Iron::IronWorker::Task::log()
- IO::Iron::IronWorker::Task::cancel()
- IO::Iron::IronWorker::Task::set_progress('percent' => $number, 'msg' => $text)
- retry_task()
- Commands for operating scheduled tasks:
- Commands for stacks:
Operating code packages
A code package is simply a script program packed into Zip archive together with its dependency files (other libraries, configuration files, etc.).
After creating the zip file and reading it into a perl variable, upload it. In the following example, the worker contains only one file and we create the archive in the program - as opposed to creating it before and simply reading it from a file before uploading it.
require IO::Iron::IronWorker::Client;
use IO::Compress::Zip;
$iron_worker_client = IO::Iron::IronWorker::Client->new(
'config' => 'iron_worker.json'
);
my $worker_as_string_ = <<EOF;
print qq{Hello, World!\n};
EOF
my $worker_as_zip;
my $worker_
IO::Compress::Zip::zip(\$worker_as_string => \$worker_as_zip);
my $code_package_return_id = $iron_worker_client->update_code_package(
'name' => 'HelloWorld_code_package',
'file' => $worker_as_string,
'file_name' => 'helloworld.pl',
'runtime' => 'perl',
);
With method list_code_packages() you can retrieve information about all the uploaded code packages. The method get_info_about_code_package() will return information about only the requested code package.
my @code_packages = $iron_worker_client->list_code_packages();
foreach (@code_packages) {
if($_->{'name'} eq 'HelloWorld_code_package) {
$code_package_id = $_->{'id'};
last;
}
}
my $code_package = $iron_worker_client->get_info_about_code_package(
'id' => $code_package_id,
);
Method delete_code_package() removes the code package from IronWorker service.
my $deleted = $iron_worker_client->delete_code_package(
'id' => $code_package_id,
);
The uploaded code package can be retrieved with method download_code_package(). The downloaded file is a zip archive.
my ($downloaded, $file_name) = $iron_worker_client->download_code_package(
'id' => $code_package_id, 'revision' => 1,
);
The code packages get revision numbers according to their upload order. The first upload of a code package gets revision number 1. Any subsequent upload of the same code package (same name) will get one higher revision number so the different uploads can be recognized.
my @code_package_revisions = $iron_worker_client->list_code_package_revisions(
'id' => $code_package_id,
);
Operating tasks
Every task needs two parameters: the name of the code package on whose code they will run and a payload. The payload is passed to the code package as a file. Payload is mandatory so if your code doesn't need it, just insert an empty string. Payload can be any string, or stringified object, normally JSON.
my $task_payload = 'Task payload (could be JSONized object)';
my $task = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
'priority' => 0,
);
my $task_code_package_name = $task->code_package_name();
Queue the task, i.e. put it to the queue for immediate execution. "Immediate" doesn't mean that IronWorker will execute it right away, just ASAP according to priority and delay parameters. When queuing, the task object is updated with returned id.
my $task_id = $iron_worker_client->queue('task' => $task);
# Or:
my @task_ids = $iron_worker_client->queue('task' => [$task1, $task2, ]);
# Or:
my $number_of_tasks_queued = $iron_worker_client->queue(
'task' => [$task1, $task2],
);
Read the STDOUT log of the task.
my $task_log = $task->log(); # Log is mime type text/plain
Cancel task if it's still in queue or currently being executed.
my $cancelled = $task->cancel();
Change the "progress display" of the task.
my $progress_set = $task->progress(
'percent' => 25,
'msg' => 'Not even halfway through!',
);
Retry a failed task. You cannot change the payload. If the payload is faulty, then you need to create a new task.
my $new_task_id = $task->retry();
# New task id after retry().
Get info about a task. Info is a hash structure.
my $task_info = $iron_worker_client->get_info_about_task( 'task' => $task_id );
Operating scheduled tasks
Create a new task for scheduling.
my $schedule_task = $iron_worker_client->create_task(
'code_name' => $unique_code_package_name,
'payload' => $task_payload,
'priority' => 0,
'run_every' => 120, # Every two minutes.
);
Schedule the task or tasks. When scheduling, the task object is updated with returned id.
$schedule_task = $iron_worker_client->schedule('task' => $schedule_task);
# Or:
my @scheduled_tasks = $iron_worker_client->schedule(
'task' => [$schedule_task1, $schedule_task2]
);
# Or:
my $number_of_scheduled_tasks = $iron_worker_client->schedule(
'task' => [$schedule_task1, $schedule_task2]
);
Get information about the scheduled task.
my $scheduled_task_info = $iron_worker_client->get_info_about_scheduled_task(
'id' => $task_id
);
Get all scheduled tasks as IO::Iron::IronWorker::Task objects.
my @scheduled_tasks = $iron_worker_client->scheduled_tasks();
Stacks
Get a list of all available stacks.
my @stacks = $iron_worker_client->list_available_stacks();
Exceptions
A REST call to IronWorker server may fail for several reason. All failures generate an exception using the Exception::Class package. Class IronHTTPCallException contains the field status_code, response_message and error. Error is formatted as such: IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>.
use Try::Tiny;
use Scalar::Util qw{blessed};
try {
my $queried_iron_mq_queue_01 = $iron_mq_client->get_queue($unique_queue_name_01);
}
catch {
die $_ unless blessed $_ && $_->can('rethrow');
if ( $_->isa('IronHTTPCallException') ) {
if ($_->status_code == 404) {
print "Bad things! Can not just find the catch in this!\n";
}
}
else {
$_->rethrow; # Push the error upwards.
}
};
REQUIREMENTS
See IO::Iron for requirements.
SUBROUTINES/METHODS
new
Creator function.
list_code_packages
Return a list of hashes containing information about every code package in IronWorker.
See "get_info_about_code_package" for an example of the returned hashes.
update_code_package
Upload an IronWorker code package or update an existing code package.
- Params:
-
- name, code package name, mandatory.
- file, the zip archive as a string buffer, optional if only updating other parameters.
- file_name, the zip archive name, required if parameter 'file' is present.
- runtime, the runtime type, e.g. sh, perl or ruby, required if parameter 'file' is present.
- config, an (configuration) file for the code package, optional.
- max_concurrency, number of concurrent runs, optional.
- retries, number of retries, optional.
- retries_delay, delay between retries, optional.
- Return: if successful, a new code package id.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
get_info_about_code_package
- Params: code package id.
- Return: a hash containing info about code package. Exception if code packages does not exist.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
Sample response (in JSON format):
{
"id": "4eb1b241cddb13606500000b",
"project_id": "4eb1b240cddb13606500000a",
"name": "MyWorker",
"runtime": "ruby",
"latest_checksum": "a0702e9e9a84b758850d19ddd997cf4a",
"rev": 1,
"latest_history_id": "4eb1b241cddb13606500000c",
"latest_change": 1328737460598000000
}
delete_code_package
Delete an IronWorker code package.
- Params: code package id. Code package must exist. If not, fails with an exception.
- Return: 1 == success.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
download_code_package
Download an IronWorker code package.
- Params: code package id. Code package must exist. If not, fails with an exception. subparam: revision.
- Return: (list) the code package zipped (as it was uploaded), code package file name (with "_[1|later].zip" suffix).
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
list_code_package_revisions
Return a list of hashes containing information about one code package revisions.
- Params: code package id. Code package must exist. If not, fails with an exception.
- Return: List of hashes.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
create_task
This method does not access the IronWorker service.
tasks
Return a list of objects of class IO::Iron::IronWorker::Task, every task in this IronWorker project.
- Params: code package name, params hash (status: queued|running|complete|error|cancelled|killed|timeout, from_time, to_time)
- Return: List of objects.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
queue
Queue a new task or tasks for an IronWorker code package to execute.
- Params: one or more IO::Iron::IronWorker::Task objects.
- Return: task id(s) returned from IronWorker (if in list context), or number of tasks.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
get_info_about_task
- Params: task id.
- Return: a hash containing info about a task. Exception if the task does not exist.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
Sample response (in JSON format):
{
"id": "4eb1b471cddb136065000010",
"project_id": "4eb1b46fcddb13606500000d",
"code_id": "4eb1b46fcddb13606500000e",
"code_history_id": "4eb1b46fcddb13606500000f",
"status": "complete",
"code_name": "MyWorker",
"code_rev": "1",
"start_time": 1320268924000000000,
"end_time": 1320268924000000000,
"duration": 43,
"timeout": 3600,
"payload": "{\"foo\":\"bar\"}",
"updated_at": "2012-11-10T18:31:08.064Z",
"created_at": "2012-11-10T18:30:43.089Z"
}
scheduled_tasks
Return a list of objects of class IO::Iron::IronWorker::Task, every task in this IronWorker project.
- Params: code package name, params hash (status: queued|running|complete|error|cancelled|killed|timeout, from_time, to_time)
- Return: List of objects.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
schedule
Schedule a new task or tasks for an IronWorker code package to execute.
- Params: one or more IO::Iron::IronWorker::Task objects.
- Return: scheduled task id(s) returned from IronWorker (if in list context), or number of scheduled tasks.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
get_info_about_scheduled_task
- Params: task id.
- Return: a hash containing info about a task. Exception if the task does not exist.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
Sample response (in JSON format):
{
"id": "4eb1b490cddb136065000011",
"created_at": "2011-11-02T21:22:51Z",
"updated_at": "2011-11-02T21:22:51Z",
"project_id": "4eb1b46fcddb13606500000d",
"msg": "Ran max times.",
"status": "complete",
"code_name": "MyWorker",
"delay": 10,
"start_at": "2011-11-02T21:22:34Z",
"end_at": "2262-04-11T23:47:16Z",
"next_start": "2011-11-02T21:22:34Z",
"last_run_time": "2011-11-02T21:22:51Z",
"run_times": 1,
"run_count": 1
}
list_available_stacks
Return a list of stacks available for running IronWorker code packages in.
AUTHOR
Mikko Koivunalho <mikko.koivunalho@iki.fi>
BUGS
Please report any bugs or feature requests to bug-io-iron@rt.cpan.org or through the web interface at: http://rt.cpan.org/Public/Dist/Display.html?Name=IO-Iron
COPYRIGHT AND LICENSE
This software is copyright (c) 2023 by Mikko Koivunalho.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
The full text of the license can be found in the LICENSE file included with this distribution.