0

I need to modularise gearman workers in Perl, so that I can just register different worker function name and their implementation and reuse all the other code.

use strict;
use warnings;

use Getopt::Std;
use JSON qw( decode_json );

use FindBin qw($Bin);
use lib ("$Bin/../blib/lib", "$Bin/../blib/arch");

use Gearman::XS qw(:constants);
use Gearman::XS::Worker;

my %opts;
if (!getopts('h:p:t:', \%opts))
{
    usage();
    exit(1);
}

if(!defined($opts{h})) {
    usage();
    exit(1);
}

my $host    = $opts{h} || '';
my $port    = $opts{p} || 4730;
my $timeout = $opts{t} || -1;

my $worker= new Gearman::XS::Worker;

if ($timeout >= 0)
{
    $worker->set_timeout($timeout);
}

my $ret= $worker->add_server($host, $port);
if ($ret != GEARMAN_SUCCESS)
{
    printf(STDERR "%s\n", $worker->error());
    exit(1);
}

#----------------------------------------------------------------------
# Register function
#----------------------------------------------------------------------

$ret= $worker->add_function("do_work", 0, \&do_work, ''); # <-- 1. This function name can be changed
if ($ret != GEARMAN_SUCCESS)
{
    printf(STDERR "%s\n", $worker->error());
}

#----------------------------------------------------------------------
# Wait for job
#----------------------------------------------------------------------

while (1)
{
    my $ret = $worker->work();
    if ($ret != GEARMAN_SUCCESS)
    {
        printf(STDERR "%s\n", $worker->error());
    }
}

#----------------------------------------------------------------------
# Worker Function
#----------------------------------------------------------------------

sub do_work {   # 2. And then this implementation can be changed
    my $job         = shift;

    my $result      = '';
    my $workload    = $job->workload();
    my $params      = decode_json $workload;

    my $input_file  = $params->{'inFile'};
    my $output_file = $params->{'outFile'};
    my $options     = $params->{'options'};
    my $timeout     = $params->{'timeout'};

    # Worker code

    my $ret= $job->send_status(1, 1);
    if ($ret != GEARMAN_SUCCESS)
    {
        return '';
    }

    return $result;
}

#----------------------------------------------------------------------
# Usage
#----------------------------------------------------------------------

sub usage {
    printf("\nusage: %s [-h <host>] [-p <port>]\n", $0);
    printf("\t-h <host>    - job server host\n");
    printf("\t-p <port>    - job server port\n");
    printf("\t-t <timeout> - timeout in milliseconds\n");
}

exit;

I want to reuse all the functionality using Perl modules, other than I can register different function names and their implementations in a different file, something like:

ModuleG.php

# All the gearman connection and common code goes here

WorkerFunction.php

Use ModuleG

$SomeVAR = $worker->add_function("do_new_work", 0, \&do_new_work, '');
do_new_work() {
    # code goes here
}

Can you please help me, how do I modularise this code.

Imran Imtiaz
  • 65
  • 1
  • 7

1 Answers1

2

It is very similar to the php.

  1. Split the german connection code into a .pm file

  2. Add package ModuleG; to the top and 1; to the bottom of the file

  3. Create a function in ModuleG.pm to access to worker Sub worker { return $worker; } as it is considered bad practise to expose a variable from a module

  4. Call the worker sub as ModuleG->worker()->add_function("do_new_work", 0, \&do_new_work, ''); and define the sub as normal.

user1937198
  • 4,987
  • 4
  • 20
  • 31