NAME

IO::Iron::IronMQ::Client - IronMQ (Message Queue) Client.

VERSION

version 0.08

SYNOPSIS

require IO::Iron::IronMQ::Client;

my $iron_mq_client = IO::Iron::IronMQ::Client->new();
my $iron_mq_queue = $iron_mq_client->create_queue('name' => 'My_Message_Queue');
# Or get an existing queue.
my $iron_mq_queue = $iron_mq_client->get_queue('name' => 'My_Message_Queue');
my $queue_info = $iron_mq_client->get_info_about_queue('name' => 'My_Message_Queue');
my $iron_mq_msg_send = IO::Iron::IronMQ::Message->new(
    'body' => "My message",
    );
my $msg_send_id = $iron_mq_queue->push('messages' => [ $iron_mq_msg_send ]);
my $iron_mq_msg_peek = $iron_mq_queue->peek();
my @iron_mq_msgs_pull = $iron_mq_queue->pull( n => 1 );
my $pulled_msg_body = $iron_mq_msgs_pull[0]->body();
my $delete_ret = $iron_mq_queue->delete( 'ids' => [ $iron_mq_msgs_pull[0]->id() ]);
my $cleared = $iron_mq_queue->clear();
my $queue_deleted = $iron_mq_client->delete_queue('name' => 'My_Message_Queue');

REQUIREMENTS

Requires the following packages:

Log::Any, v. 0.15
File::Slurp, v. 9999.19
JSON, v. 2.53
Carp::Assert::More, v. 1.12
REST::Client, v. 88
File::HomeDir, v. 1.00,
Exception::Class, v. 1.37
Try::Tiny, v. 0.18
Scalar::Util, v. 1.27

Requires IronMQ account. Three configuration items must be set (others available) before using the functions: 'project_id', 'token' and 'host'. These can be set in a json file, as environmental variables or as parameters when creating the object.

project_id, the identification string, from IronMQ.
token, an OAuth authentication token string, from IronMQ.
host, the cloud in which you want to operate: e.g. 'mq-aws-us-east-1.iron.io/1' or 'mq-aws-eu-west-1.iron.io' for AWS (Amazon) or 'mq-rackspace-ord.iron.io/1' or 'mq-rackspace-lon.iron.io' for Rackspace.

Please see http://dev.iron.io/mq/reference/api/#base_url for available hosts.

DESCRIPTION

IO::Iron::IronMQ is a client for the IronMQ message queue at http://www.iron.io/. IronMQ is a cloud based message queue with a REST API. IO::Iron::IronMQ creates a Perl object for interacting with IronMQ. All IronMQ functions are available.

The class IO::Iron::IronMQ::Client instantiates the 'project', IronMQ access configuration.

IronMQ Message Queue

http://www.iron.io/

IronMQ is a message queue as a service available to Internet connecting applications via its REST interface. Built with distributed cloud applications in mind, it provides on-demand message queuing with HTTPS transport, one-time FIFO delivery, message persistence, and cloud-optimized performance. [see http://www.iron.io/]

Using the IronMQ Client Library

IO::Iron::IronMQ::Client is a normal Perl package meant to be used as an object.

require IO::Iron::IronMQ::Client;
my $iron_mq_client = IO::Iron::IronMQ::Client->new( { } );

The following parameters can be given to new() as items in the first parameter which is a hash.

project_id, The ID of the project to use for requests.
token, The OAuth token that is used to authenticate requests.
host, The domain name the API can be located at. E.g. 'mq-aws-us-east-1.iron.io/1'.
protocol, The protocol that will be used to communicate with the API. Defaults to "https".
port, The port to connect to the API through. Defaults to 443.
api_version, The version of the API to connect through. Defaults to the version supported by the client.
host_path_prefix, Path prefix to the RESTful url. Defaults to '/1'. Used with non-standard clouds/emergency service back up addresses.
timeout, REST client timeout (for REST calls accessing IronMQ.)
config, Config filename with path if required.

You can also give the parameters in the config file '.iron.json' (in home dir) or 'iron.json' (in current dir) or as environmental variables. Please read http://dev.iron.io/mq/reference/configuration/ for further details.

After creating the client, the client can create a new message queue, get, modify or delete an old one or get all the existing message queues within the same project.

The client has all the methods which interact with queues; the queue (object of IO::Iron::IronMQ::Queue) has methods which involve messages.

If failed, the methods cause exception. After successfull REST API call, the HTTP return code can be retrieved with method last_http_status_code().

    # Create a new queue. (Parameter queue name;
    # return an IO::Iron::IronMQ::Queue object)
    my $iron_mq_queue = $iron_mq_client->create_queue(name => 'My_Message_Queue',
			'subscribers' => [
				{ "url" => "ironmq:///Other_queue_name" },
			],
			'push_type' => 'unicast',
			'retries' => 0,
			'retries_delay' => 3,
			'error_queue' => "Error_queue_name",
    );

    # Get an existing queue. (Parameter queue name;
    # return an IO::Iron::IronMQ::Queue object)
    my $iron_mq_queue = $iron_mq_client->get_queue( 'name' => 'My_Message_Queue');

    # Delete an existing queue. (Parameter queue name;
    # return 1)
    my $success = $iron_mq_client->delete_queue( 'name' => 'My_Message_Queue');

    # Get all the queues. 
    # Return a list of IO::Iron::IronMQ::Queue objects.
    my @iron_mq_queues = $iron_mq_client->get_queues();

    # Update a queue. Return a IO::Iron::IronMQ::Queue object. 
    # Queue properties: set queue type (pull, multicast, unicast), set subscribers.
	my $push_from_queue = $iron_mq_client->update_queue(
			'name' => 'My_Message_Queue',
			'subscribers' => [
				{ "url" => "ironmq:///Other_queue_name" },
			],
			'push_type' => 'unicast',
			'retries' => 0,
			'retries_delay' => 3,
			'error_queue' => "Error_queue_name",
		);

    # Get info about the queue
    # (Return a hash containing items name, id, size, project, etc.).
    my $queue_info = $iron_mq_client->get_info_about_queue( 'name' => 'My_Message_Queue');

A IO::Iron::IronMQ::Queue object gives access to a single message queue. With it you can do all the normal things one would with a message queue.

Messages are objects of the class IO::Iron::IronMQ::Message. It contains the following attributes:

- body, Free text. Will be JSONized. If you need an object serialized, don't use JSON. Use e.g. YAML. Then give the resulting string here.
- timeout, When reading from queue, after timeout (in seconds), item will be placed back onto queue.
- delay, The item will not be available on the queue until this many seconds have passed.
- expires_in, How long in seconds to keep the item on the queue before it is deleted.
- id, Message id from IronMQ (available after message has been pulled/peeked).
- reserved_count, Not yet implemented. (available after message has been pulled/peeked).
- push_status, Not yet implemented. (available after message has been pulled/peeked).
my $iron_mq_msg_send_01 = IO::Iron::IronMQ::Message->new(
		'body' => "My message",
		);
# Or
use YAML::Tiny;
%msg_body_hash_02 = (msg_body_text => 'My message 2', msg_body_item => {sub_item => 'Sub text'});
my $yaml = YAML::Tiny->new(); $yaml->[0] = \%msg_body_hash_02;
my $msg_body = $yaml->write_string();
my $iron_mq_msg_send_02 = IO::Iron::IronMQ::Message->new(
		'body' => $msg_body,
		'timeout' => $msg_timeout, # When reading from queue, after timeout (in seconds), item will be placed back onto queue.
		'delay' => $msg_delay,	 # The item will not be available on the queue until this many seconds have passed.
		'expires_in' => $msg_expires_in, # How long in seconds to keep the item on the queue before it is deleted.
		);
# Return YAML serialized structure:
my $yaml_de = YAML::Tiny->new(); $yaml_de = $yaml_de->read_string($iron_mq_msg_send_02->body());

IO::Iron::IronMQ::Queue objects are created by the client IO::Iron::IronMQ::Client. With an IO::Iron::IronMQ::Queue object you can push messages to the queue, or pull messages from it. The names push and pull are used because the queue is likened to a pipe. The queue is like a FIFO pipe (first in, first out).

Get queue id.

my $queue_id = $iron_mq_queue->id();

Get queue name.

my $queue_name = $iron_mq_queue->name();

Add one or more messages to the queue. Returns the ids of the messages sent or the number of sent messages.

my $msg_send_id = $iron_mq_queue->push( 'messages' => [ $iron_mq_msg_send_01 ] );
my @msg_send_ids = $iron_mq_queue->push( 'messages' => [ $iron_mq_msg_send_01, $iron_mq_msg_send_02 ] );
my $number_of_msgs_sent = $iron_mq_queue->push( 'messages' => [ $iron_mq_msg_send_01, $iron_mq_msg_send_02 ] );

Read one or more messages from the queue and reserve them so another process cannot access them. Parameters: n (number of messages you want, default 1, maximum 100; if there is less, all available messages will be returned), if no messages, an empty list will be returned, timeout (After timeout (in seconds), item will be placed back onto queue, default is 60 seconds, minimum is 30 seconds, and maximum is 86,400 seconds (24 hours)).

my @iron_mq_msg_pulls = $iron_mq_queue->pull( n => 10, timeout => 120 );

Read one or more messages from the queue but don't reserve them. Parameters: n (number of messages you want, default 1, maximum 100; if there is less, all available messages will be returned), if no messages, an empty list will be returned.

my @iron_mq_msg_peeks = $iron_mq_queue->peek( n => 10 );

Delete one or more messages from the queue. Call this when you have processed the messages. Returns the ids of the messages deleted or the number of deleted messages.

my $deleted_msg_id = $iron_mq_queue->delete( 'ids' => [ $msg_id_01 ] );
my @deleted_msg_ids = $iron_mq_queue->delete( 'ids' => [ $msg_id_01, $msg_id_02 ] );
my $number_of_msgs_deleted = $iron_mq_queue->delete( 'ids' => [ $msg_id_01, $msg_id_02 ] );

Release one or more messages back to the queue. Releasing a reserved message unreserves the message and puts it back on the queue as if the message had timed out. Delay: The item will not be available on the queue until this many seconds have passed. Default is 0 seconds. Maximum is 604,800 seconds (7 days).

Returns 1.

my $released_msg = $iron_mq_queue->release( 'id' => $msg_id_01, 'delay' => $delay );

Touch one or more messages in the queue. Touching a reserved message extends its timeout to the duration specified when the message was created. Default is 60 seconds. Returns 1.

my $touched_msg = $iron_mq_queue->touch( 'id' => $msg_id_01 );

Clear all messages from the queue: delete all messages, whether they are reserved or not.

my $cleared = $iron_mq_queue->clear();

Get queue size.

my $size = $iron_mq_queue->size();

Push Queue Commands

Get push status for a message. Retrieve the push status for a particular message which will let you know which subscribers have received the message, which have failed, how many times it's tried to be delivered and the status code returned from the endpoint.

my $info = $iron_mq_queue->get_push_status( 'id' => $msg_id );
my @subscribers = (@{info->{'subscribers'}});

Acknowledge / Delete Push Message for a Subscriber. This is only for use with long running processes that have previously returned a 202.

my $info = $iron_mq_queue->get_push_status( 'id' => $msg_id );
my @subscribers = (@{info->{'subscribers'}});
my $push_acknowledged = $iron_mq_queue->delete_push_message(
	'id' => $msg_id, 'subscriber' => $subscribers[0]->{'id'}
	);

Add Subscribers to a Queue.

my $add_ret_val = $iron_mq_queue->add_subscribers(
		'name' => $queue_name,
		'subscribers' => [
			{ 'url' => "ironmq://project_id:token\@host/queue_name" },
			{ 'url' => "ironmq:///$queue_name_02" },
		],
	);

Remove Subscribers from a Queue

my $del_ret_val = $iron_mq_client->delete_subscribers(
		'name' => $queue_name,
		'subscribers' => [
			{ 'url' => "ironmq:///$queue_name" },
		],
	);

Queue Alerts

Add Alerts to a Queue. This is for Pull Queue only.

my $alert_added = $iron_mq_client->add_alerts(
	'name' => $normal_queue->name(),
	'alerts' => [
			{
				'type' => 'fixed',
				'queue' => $alert_queue->name(),
				'trigger' => 1,
				'direction' => 'asc',
				'snooze' => 0,
			}
		],
	);

Replace alerts. Change the existing alerts to the given ones.

my $alert_replaced = $iron_mq_client->replace_alerts(
	'name' => $normal_queue->name(),
	'alerts' => [
			{
				'type' => 'fixed',
				'queue' => $alert_queue->name(),
				'trigger' => 2,
				'direction' => 'desc',
				'snooze' => 0,
			},
			{
				'type' => 'fixed',
				'queue' => $alert_queue->name(),
				'trigger' => 5,
				'direction' => 'desc',
				'snooze' => 5,
			},
		],
	);

Delete alerts. You can either give an array containing items (hashes) which contain key 'id', or delete only one alert with the parameter 'id'.

my $alert_deleted = $iron_mq_client->delete_alerts(
	'name' => $normal_queue->name(),
	'alerts' => [
			{ 'id' => $alert_id, },
		],
	);

Or:

$alert_deleted = $iron_mq_client->delete_alerts(
	'name' => $normal_queue->name(),
	'id' => $alert_id,
	);

Exceptions

A REST call to IronMQ 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.

get_queue

Return a IO::Iron::IronMQ::Queue object representing a particular message queue.

Params: queue name. Queue must exist. If not, fails with an exception.
Return: IO::Iron::IronMQ::Queue object.
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

create_queue

Return a IO::Iron::IronMQ::Queue object representing a particular message queue.

URL format for subscribers to Iron.io systems: [ironmq_or_ironworker]://[project_id:token]@[host]/queue_or_worker_name

Params: name, subscribers, push_type, retries, retries_delay, error_queue.
Return: IO::Iron::IronMQ::Queue object.
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

update_queue

Return a IO::Iron::IronMQ::Queue object representing a particular message queue.

Params: name, subscribers, push_type, retries, retries_delay, error_queue.
Return: IO::Iron::IronMQ::Queue object.
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

add_subscribers

Add subscribers to a push queue.

Params: name, subscribers.
Returns 1 if succcessful
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

delete_subscribers

Delete the given subscribers from the queue.

Params: name, subscribers.
1
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

add_alerts

Add alerts to a queue.

Params: name, alerts.
Returns 1 if succcessful
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

replace_alerts

Replace alerts to a queue. Replace the old alerts (if any) with these new ones.

Params: name, alerts.
Returns 1 if succcessful
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

delete_alerts

Delete the given alerts from the queue.

Params: name, alerts.
1
Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)

delete_queue

Delete an IronMQ queue.

Params: queue name. Queue 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>)

get_queues

Return a IO::Iron::IronMQ::Queue objects representing message queues.

Params: [None]
Return: List of IO::Iron::IronMQ::Queue objects.

get_info_about_queue

Params: queue name.
Return: a hash containing info about queue..

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::IronMQ::Client

You can also look for information at:

ACKNOWLEDGMENTS

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

TODO

  • Some get_*s (e.g. get messages) commands not yet implemented.

  • The IronMQ client needs to control the queues, perhaps using semafores.

  • A buffer mechanism to keep the messages while the IronMQ REST service is unavailable. IO::Iron::IronMQ::ASyncPush?

  • Mock IronMQ for testing.

  • Rethink the using of REST:Client. Since message queues often involve a lot of traffic but always to the same address, REST:Client might not be the best solution.

  • Handle message size issues: max 64KB; Includes the entire request (delay, timeout, expiration).

  • Handle message delay, timeout and expiration min-max values.

    Message Var Default Maximum Notes Message Size -- 64KB Includes the entire request (delay, timeout, expiration). Delay 0sec 604,800sec Message is made available on queue after the delay expires. Timeout 60sec 86,400sec Message goes back on queue after timeout unless deleted. Expiration 604,800sec 2,592,000sec Equates to 7 days and 30 days, respectively. Messages per Get 1 100 One or more messages can be handled at a time.

  • Better logging, consistent log and error messages.

  • Option to delete queue when IO::Iron::IronMQ::Queue object goes to garbage collection.

  • Verify the client is connected when created by calling queues!

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.

3 POD Errors

The following errors were encountered while parsing the POD:

Around line 91:

alternative text 'http://dev.iron.io/mq/reference/api/#base_url' contains non-escaped | or /

Around line 742:

Expected text after =item, not a number

Around line 897:

Expected text after =item, not a number