| 1 | package Job::Machine; | ||
| 2 | $Job::Machine::VERSION = '0.25'; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | 1; | ||
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =encoding UTF-8 | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Job::Machine - Job queue handling using PostgreSQL. | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | version 0.25 | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | =head2 The Client | ||
| 22 | |||
| 23 | my $client = Job::Machine::Client->new(queue => 'job.task'); | ||
| 24 | my $id = $client->send({foo => 'bar'}); | ||
| 25 | |||
| 26 | =head2 The Worker | ||
| 27 | |||
| 28 | The Worker is a subclass | ||
| 29 | |||
| 30 | use base 'Job::Machine::Worker'; | ||
| 31 | |||
| 32 | sub process { | ||
| 33 | my ($self, $data) = @_; | ||
| 34 | $self->reply({baz => 'Yeah!'}) if $data->{foo} eq 'bar'; | ||
| 35 | }; | ||
| 36 | |||
| 37 | and then use the worker | ||
| 38 | |||
| 39 | my $worker = Worker->new(queue => 'job.task'); | ||
| 40 | $worker->receive; | ||
| 41 | |||
| 42 | Back at the Client: | ||
| 43 | |||
| 44 | if ($client->check('reply')) { | ||
| 45 | print $client->receive->{baz}; | ||
| 46 | } | ||
| 47 | |||
| 48 | =head2 Moose | ||
| 49 | |||
| 50 | Job::Machine doesn't use Moose itself, so this is the way to subclass it in a Moose environment: | ||
| 51 | |||
| 52 | use MooseX::NonMoose; | ||
| 53 | extends 'Job::Machine::Worker'; | ||
| 54 | |||
| 55 | This makes sure that Moose doesn't step on the precious new method. | ||
| 56 | |||
| 57 | =head2 Database Connection | ||
| 58 | |||
| 59 | Both client and worker accepts a Database Handle (dbh), or a Data Source Name (dsn). | ||
| 60 | |||
| 61 | From scratch: | ||
| 62 | |||
| 63 | my $client = Job::Machine::Client->new( | ||
| 64 | dsn => 'dbi:Pg:dbname=jobqueue', | ||
| 65 | queue => 'my.queue', | ||
| 66 | ); | ||
| 67 | |||
| 68 | Hot Handle: | ||
| 69 | |||
| 70 | my $dbh = $self->existing_dbh; | ||
| 71 | my $client = Job::Machine::Client->new( | ||
| 72 | dbh => $dbh, | ||
| 73 | queue => 'my.queue', | ||
| 74 | ); | ||
| 75 | |||
| 76 | =head2 Queue | ||
| 77 | |||
| 78 | Normally the queue name is passed as a parameter to new, but it can be overriden | ||
| 79 | for any method call. | ||
| 80 | |||
| 81 | The queue can be named anything PostgreSQL accepts. A good idea is to maintain a | ||
| 82 | hierarchical structure. e.g. I<gl.accounting> or I<message.email>. | ||
| 83 | |||
| 84 | =head2 Extra Parameters | ||
| 85 | |||
| 86 | You might have some already initialized data you want to pass to your worker | ||
| 87 | instance. Job::Machine just pushes any extra parameter you send it into the | ||
| 88 | object, so you can always access it from your process method. | ||
| 89 | |||
| 90 | There's no reason to repeat your configuration process in the worker if you already | ||
| 91 | have it when the worker starts: | ||
| 92 | |||
| 93 | my $config = C<some lenghty process> | ||
| 94 | |||
| 95 | my $worker = SMSio::Worker::CPA->new( | ||
| 96 | ... | ||
| 97 | config => $config, | ||
| 98 | ); | ||
| 99 | $worker->receive; | ||
| 100 | |||
| 101 | You can access $self->{config} e.g. in your worker's startup and process methods. | ||
| 102 | |||
| 103 | =head1 DESCRIPTION | ||
| 104 | |||
| 105 | Job queue handling using PostgreSQL. | ||
| 106 | |||
| 107 | A small, but versatile system for sending jobs to a message queue and, if necessary, | ||
| 108 | communicating answers back to the sender. | ||
| 109 | |||
| 110 | Job::Machine uses LISTEN / NOTIFY from PostgreSQL to send signals between | ||
| 111 | clients and workers. This ensures very efficient message passing, giving any | ||
| 112 | worker that is awake the chance to start working immediately. | ||
| 113 | |||
| 114 | The Database: | ||
| 115 | |||
| 116 | The Database Schema of Job::Machine is in sql/create_tables.sql. Just install it into your | ||
| 117 | database. It is environmental friendly (will not pollute your namespace). By default | ||
| 118 | it installs in a new jobmachine schema (Database schema, NOT DBIC schema; Job::Machine doesn't use DBIx::Class). | ||
| 119 | |||
| 120 | =head2 NB! | ||
| 121 | |||
| 122 | Starting with version 0.18, Job::Machine needs at least PostgreSQL 9.0. | ||
| 123 | |||
| 124 | Using pg_notify means we need PostgreSQL >= 9.0 | ||
| 125 | |||
| 126 | =head1 NAME | ||
| 127 | |||
| 128 | Job::Machine | ||
| 129 | |||
| 130 | =head1 SUPPORT | ||
| 131 | |||
| 132 | Report tickets to http://rt.cpan.org/Job-Machine/ | ||
| 133 | |||
| 134 | =head1 AUTHOR | ||
| 135 | |||
| 136 | Kaare Rasmussen <kaare@cpan.org>. | ||
| 137 | |||
| 138 | =head1 COPYRIGHT | ||
| 139 | |||
| 140 | Copyright (C) 2009,2015, Kaare Rasmussen | ||
| 141 | |||
| 142 | This module is free software; you can redistribute it or modify it | ||
| 143 | under the same terms as Perl itself. | ||
| 144 | |||
| 145 | =head1 AUTHOR | ||
| 146 | |||
| 147 | Kaare Rasmussen <kaare at cpan dot net> | ||
| 148 | |||
| 149 | =head1 COPYRIGHT AND LICENSE | ||
| 150 | |||
| 151 | This software is copyright (c) 2015 by Kaare Rasmussen. | ||
| 152 | |||
| 153 | This is free software; you can redistribute it and/or modify it under | ||
| 154 | the same terms as the Perl 5 programming language system itself. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | __END__ |