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 7962301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:18:09+00:00 2026-06-04T05:18:09+00:00

I have the problem that in a PHP application Gearman jobs sometimes are passed

  • 0

I have the problem that in a PHP application Gearman jobs sometimes are passed to more than one worker. I could reduce a code to reproduce it into one file. Now I am not sure if this is a bug in Gearman or a bug in the pecl library or maybe in my code.

Here is the code to reproduce the error:

#!/usr/bin/php
<?php

// Try 'standard', 'exception' or 'exception-sleep'.
$sWorker = 'exception';



// Detect run mode "client" or "worker".
if (!isset($argv[1]))
    $sMode = 'client';
else
    $sMode = 'worker-' . $sWorker;

$sLogFilePath = __DIR__ . '/log.txt';

switch ($sMode) {

    case 'client':

        // Remove all queued test jobs and quit if there are test workers running.
        prepare();

        // Init the greaman client.
        $Client= new GearmanClient;
        $Client->addServer();

        // Empty the log file.
        file_put_contents($sLogFilePath, '');

        // Start some worker processes.
        $aPids = array();       
        for ($i = 0; $i < 100; $i++)
            $aPids[] = exec('php ' . __FILE__ . ' worker > /dev/null 2>&1 & echo $!');

        // Start some jobs. Also try doHigh(), doBackground() and
        // doBackgroundHigh();
        for ($i = 0; $i < 50; $i++)
            $Client->doNormal('test', $i);

        // Wait a second (when running jobs in background).
        // sleep(1);

        // Prepare the log file entries.
        $aJobs = array();
        $aLines = file($sLogFilePath);
        foreach ($aLines as $sLine) {
            list($sTime, $sPid, $sHandle, $sWorkload) = $aAttributes = explode("\t", $sLine);
            $sWorkload = trim($sWorkload);
            if (!isset($aJobs[$sWorkload]))
                $aJobs[$sWorkload] = array();
            $aJobs[$sWorkload][] = $aAttributes;
        }

        // Remove all jobs that have been passed to only one worker as expected.
        foreach ($aJobs as $sWorkload => $aJob) {
            if (count($aJob) === 1)
                unset($aJobs[$sWorkload]);
        }

        echo "\n\n";

        if (empty($aJobs))
            echo "No job has been passed to more than one worker.";
        else {
            echo "Those jobs has been passed more than one times to a worker:\n";
            foreach ($aJobs as $sWorload => $aJob) {

                echo "\nJob #" . $sWorload . ":\n";
                foreach ($aJob as $aAttributes)
                    echo "  $aAttributes[2] (Worker PID: $aAttributes[1])\n";
            }
        }

        echo "\n\n";

        // Kill all started workers.
        foreach ($aPids as $sPid)
            exec('kill ' . $sPid . ' > /dev/null 2>&1');

    break;

    case 'worker-standard':
        $Worker = new GearmanWorker;
        $Worker->addServer();
        $Worker->addFunction('test', 'logJob');
                    $bShutdown = false;
        while ($Worker->work())
            if ($bShutdown)
                continue;
        break;

    case 'worker-exception':
        try {
            $Worker = new GearmanWorker;
            $Worker->addServer();
            $Worker->addFunction('test', 'logJob');
            $bShutdown = false;
            while ($Worker->work())
                if ($bShutdown)
                    throw new \Exception;

        } catch (\Exception $E) {
        }
    break;

    case 'worker-exception-sleep':
        try {
            $Worker = new GearmanWorker;
            $Worker->addServer();
            $Worker->addFunction('test', 'logJob');
            $bShutdown = false;
            while ($Worker->work())
            {
                if ($bShutdown) {
                    sleep(1);
                    throw new \Exception;
                }
            }
        } catch (\Exception $E) {
        }
    break;
}

function logJob(\GearmanJob $Job)
{
    global $bShutdown, $sLogFilePath;
    $sLine = microtime(true) . "\t" . getmypid() . "\t" . $Job->handle() . "\t" . $Job->workload() . "\n";
    file_put_contents($sLogFilePath, $sLine, FILE_APPEND);
    $bShutdown = true;
}


function prepare()
{
    $rGearman = fsockopen('127.0.0.1', '4730', $iErrno, $sErrstr, 3);
    $aBuffer = array();
    fputs ($rGearman, 'STATUS' . PHP_EOL);
    stream_set_timeout($rGearman, 1);
    while (!feof($rGearman))
        if ('.' . PHP_EOL !== $sLine = fgets($rGearman, 128))
            $aBuffer[] = $sLine;
        else
            break;
    fclose($rGearman);

    $bJobsInQueue = false;
    $bWorkersRunning = false;
    foreach ($aBuffer as $sFunctionLine) {
        list($sFunctionName, $iQueuedJobs, $iRunningJobs, $iWorkers) = explode("\t", $sFunctionLine);
        if ('test' === $sFunctionName) {
            if (0 != $iQueuedJobs)
                $bJobsInQueue = true;
            if (0 != $iWorkers)
                $bWorkersRunning = true;;
        }
    }

    // Exit if there are workers running.
    if ($bWorkersRunning)
        die("There are some Gearman workers running that have registered a 'test' function. Please stop these workers and run again.\n\n");

    // If there are test jobs in the queue start a worker that eat up the jobs.
    if ($bJobsInQueue) {
        $sPid = exec('gearman -n -w -f test > /dev/null 2>&1 & echo $!');
        sleep(1);
        exec ("kill $sPid > /dev/null 2>&1");
        // Repeat this method to make sure all jobs are removed.
        prepare();
    }
}

When you run this code on the command line it should output "No job
has been passed to more than one worker."
but insted it alway outputs a list of some jobs that have been passed to more than one worker. The error doesn’t appear if you set $sWorker = 'standard'; or 'exception-sleep'.

It would help me a lot if you could run the code and tell me if you are able to reproduce the error of if I have a bug in the 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-04T05:18:10+00:00Added an answer on June 4, 2026 at 5:18 am

    Had exactly the same issue with Gearman 0.24, PECL lib 1.0.2. Was able to reproduce the error with your script every time.

    An older version of Gearman (0.14 I think) used to work fine.

    Upgrading Gearman to 0.33 fixed the issue.

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

Sidebar

Related Questions

I have problem with session in cakephp.I have one file chat.php that is in
A have a problem with PHP application, that I'm working on. Main page which
I have the problem that when I try to copy some files from php
I'm having a strange problem in that I have php inserting text into a
I have a Problem developing in PHP. First I have to say that I'm
My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have Mamp Pro and have a PHP application that sends an email password
hello i have a problem that's maybe difficult to descripe, i have an application,
I have a PHP application that pulls order information based on a scanned/entered order
I have a strange problem using the Cosine formula in my PHP application.. function

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.