Mikko Koivunalho > IO-Iron > IO::Iron::IronWorker::Client

Download:
IO-Iron-0.10.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  2
View/Report Bugs
Module Version: 0.10   Source  

NAME ^

IO::Iron::IronWorker::Client - IronWorker (Online Worker Platform) Client.

VERSION ^

version 0.10

SYNOPSIS ^

        require IO::Iron::IronWorker::Client;

        my $ironworker_client = IO::Iron::IronWorker::Client->new();
        # or
        use IO::Iron qw(get_ironworker);
        my $iron_worker_client = get_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 = $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 @task_ids = $iron_worker_client->queue( 'tasks' => [ $task1, $task2 ] );
        # Or: 
        my $number_of_tasks_queued = $iron_worker_client->queue(
                'tasks' => [ $task1, $task2 ]
                );
        #
        my $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().

        my $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);
        my $end_dt = DateTime::...
        $schedule_task->end_at($end_dt);
        $schedule_task->start_at($end_dt);
        # When scheduling, the task object is updated with returned id.
        $schedule_task = $iron_worker_client->schedule( 'tasks' => $schedule_task);
        # Or:
        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
        );
        
        my @scheduled_tasks = $iron_worker_client->scheduled_tasks();

REQUIREMENTS ^

See IO::Iron for requirements.

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

http://www.iron.io/

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:
IO::Iron::IronWorker::Client::scheduled_tasks('code_name' => $code_name, ...)
IO::Iron::IronWorker::Client::schedule(tasks => $task | [@tasks] )
IO::Iron::IronWorker::Client::get_info_about_scheduled_task('id' => $id)
IO::Iron::IronWorker::Task::cancel_scheduled()

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 = $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();

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.
                }
        };

SUBROUTINES/METHODS ^

new

Creator function.

list_code_packages

Return a list of hashes containing information about every code package in IronWorker.

Params: [None]
Return: List of hashes.

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.
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

Params: code package name.
Return: an object of class IO::Iron::IronWorker::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
        }

AUTHOR ^

Mikko Koivunalho, <mikko.koivunalho at iki.fi>

BUGS ^

Please report any bugs or feature requests to bug-io-iron at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Iron. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc IO::Iron::IronWorker::Client

You can also look for information at:

ACKNOWLEDGMENTS ^

Cool idea, "workers in the cloud": http://www.iron.io/.

LICENSE AND COPYRIGHT ^

Copyright 2013 Mikko Koivunalho.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax highlighting: