NAME
IO::Iron::IronMQ::Client - IronMQ (Online Message Queue) Client.
VERSION
version 0.13
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_queue_info('name' => 'My_Message_Queue');
my $iron_mq_msg_send = IO::Iron::IronMQ::Message->new(
'body' => "My message",
);
my $msg_send_id = $iron_mq_queue->post_messages('messages' => [ $iron_mq_msg_send ]);
my $iron_mq_msg_peek = $iron_mq_queue->peek();
my @iron_mq_msgs_pull = $iron_mq_queue->reserve_messages( 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');
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
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.
- 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_and_get_queue(
'name' => 'My_Message_Queue',
);
# 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 undef)
$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();
# Get info about the queue
# (Return a hash containing items name, id, size, project, etc.).
my $queue_info = $iron_mq_client->get_queue_info( '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. If you want to put an object or a hash here, it needs to be serialized first; use e.g. JSON, Storable or YAML to stringify it. Then give the resulting string here.
- - delay, The item will not be available on the queue until this many seconds have passed.
- - push_headersn, Headers for push queues.
- - id, Message id from IronMQ (available after message has been pulled/peeked).
- - reserved_count, Not yet implemented. (available after message has been pulled/peeked).
- - reservation_id, Reservation id string from the queue.
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,
'delay' => $msg_delay, # The item will not be available on the queue until this many seconds have passed.
);
# 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 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->post_messages( 'messages' => [ $iron_mq_msg_send_01 ] );
my @msg_send_ids = $iron_mq_queue->post_messages( 'messages' => [ $iron_mq_msg_send_01, $iron_mq_msg_send_02 ] );
my $number_of_msgs_sent = $iron_mq_queue->post_messages( '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->reserve_messages( 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_message( '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_messages();
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_statuses( '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_statuses( '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.
}
};
REQUIREMENTS
See IO::Iron for requirements.
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>)
get_queues
Return a IO::Iron::IronMQ::Queue objects representing message queues. Not paginated; you get all the queues! Heavy because every queue must separately be queried in IronMQ v3.
create_and_get_queue
Return a IO::Iron::IronMQ::Queue object. Throws if fails. Combines create_queue() and get_queue() (which hides get_queue_info()).
URL format for subscribers to Iron.io systems: [ironmq_or_ironworker]://[project_id:token]@[host]/queue_or_worker_name
- Params: name, message_timeout, message_expiration, type, push { subscribers, retries, retries_delay, error_queue }, dead_letter { queue_name, max_reservations }.
- Return: IO::Iron::IronMQ::Queue object.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
create_queue
Return nothing. Throws if fails.
URL format for subscribers to Iron.io systems: [ironmq_or_ironworker]://[project_id:token]@[host]/queue_or_worker_name
- Params: name, message_timeout, message_expiration, type, push { subscribers, retries, retries_delay, error_queue }, dead_letter { queue_name, max_reservations }.
- Return: undefined.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
get_queue_info
update_queue
Update a queue. Return nothing. Throw if fails.
- Params: name, subscribers, push_type, retries, retries_delay, error_queue.
- Return: undefined.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
delete_queue
Delete an IronMQ queue. Return undefined. Throw if fails.
- Params: queue name. Queue must exist. If not, fails with an exception.
- Return: undefined.
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
list_queues
Return a list of queue names.
Paginated with per_page and previous (this is the last queue on the previous page, it will start from the next one. If queue with specified name does not exist result will contain first per_page queues that lexicographically greater than previous).
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.
- Returns 1 if succcessful
- 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.
- Returns 1 if successful
- Exception: IronHTTPCallException if fails. (IronHTTPCallException: status_code=<HTTP status code> response_message=<response_message>)
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) 2017 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.