[![Build Status](https://travis-ci.org/berkmancenter/p5-MediaCloud-JobManager.svg?branch=develop)](https://travis-ci.org/berkmancenter/p5-MediaCloud-JobManager) [![Coverage Status](https://coveralls.io/repos/github/berkmancenter/p5-MediaCloud-JobManager/badge.svg?branch=develop)](https://coveralls.io/github/berkmancenter/p5-MediaCloud-JobManager)

# MediaCloud::JobManager

(Yet another) Perl worker / client library for running jobs asynchronously.

## Features

* Job brokers supported:
    * [Gearman](http://gearman.org/)
* Automattic worker logging via [Log::Log4perl](http://search.cpan.org/~mschilli/Log-Log4perl/)
* Restarting failed jobs
* Progress reporting

## Sample

### Worker

```perl
package AddTwoNumbers;

use strict;
use warnings;

use Moose;
with 'MediaCloud::JobManager::Job';

sub run($;$)
{
    my ( $self, $args ) = @_;

    my $number_1 = $args->{ number_1 };
    my $number_2 = $args->{ number_2 };

    # Write the sum to the database here
    say STDERR "Sum: " . ( $number_1 + $number_2 );

    return 1;
}

no Moose;    # gets rid of scaffolding

# Return package name instead of 1 or otherwise worker.pl won't know the
# name of the package it's loading
__PACKAGE__;
```

### Client

```perl
use AddTwoNumbers;

my $args = { number_1 => 3, number_2 => 4 };

# Run locally, like any other function
AddTwoNumbers->run_locally( $args );

# Run remotely, wait for the function to finish
AddTwoNumbers->run_remotely( $args );

# Add to broker's queue and return instantly, don't wait for the function to finish
AddTwoNumbers->add_to_queue( $args );
```


# Development Notes

## Code formatting

Install `perltidy` Git hook to automatically fix script formatting:

    cpanm --installdeps .
    githook-perltidy install