Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:56:07+00:00 2026-06-16T18:56:07+00:00

I need to modularise gearman workers in Perl, so that I can just register

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:56:08+00:00Added an answer on June 16, 2026 at 6:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need a map reduce function by mongo in php This my mongo structure [_id]
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need just a push in the right direction with this. I'm building a multi-language
Need help with a query that I wrote: I have three tables Company id
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation
Background: I work in a suite of ASP.NET applications that have several different modules.
I would need to have two different projects, let's say internal and external, which
Need some advice on creating a floating menu that behaves very similar to what
Need help with Min Function in SQL I have a table as shown below.
Need some help. I have a table with some columns..like name , phone etc...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.