Alex
Alex

Reputation: 41

Gearman PHP, sendComplete has no effect

Have successfully connected Gearman to an existing PHP project. Using supervisord to ensure that the workers are running, it has produced pretty good results!

I have a critical issue, however, in that the "setCompleteCallback" is not working at all.

Split up somewhat like this:

Client

$client = new GearmanClient();
$client->addServer();
$client->setCompleteCallback( 
    array( 'LDPE_Service_AWSConnect_Transfer_Target', 'transferComplete' ) );

// push core to S3 bucket
$target = new LDPE_Service_AWSConnect_Transfer_Target( $transaction->id,
    "/usr/local/include/LDP/", LDPE_Service_S3::BUCKET_CORE );

// push S3 bucket to instances
foreach( $aws_target_list as $dns )
{
    $target->addChildRequest( 
        new LDPE_Service_AWSConnect_Transfer_Target( $transaction->id, 
                null, LDPE_Service_S3::BUCKET_CORE, $dns )
    );
}


$client->addTaskBackground( 'transferStart', serialize( $target ) );        
$client->runTasks();

Worker

(basically bootstraps a Zend Framework environment, and loads the exec functions)

include 'bootstrap.php';    

ini_set('memory_limit', -1);


$worker = new GearmanWorker();
$worker->addServer();

$worker->addFunction( 'transferStart', array( 
        'LDPE_Service_AWSConnect_Transfer_Target', 'transferStart' ) );

while ($worker->work())
{
    switch( $worker->returnCode() )
    {
        case GEARMAN_SUCCESS:
        break;

        default:
            echo "ERROR RET: " . $worker->returnCode() . "\n";
            exit;   
    }
}

Finally, here's the LDPE_Service_AWSConnect_Transfer_Target class that contains all of the heavy lifting. I've pruned out all of the logic, and it doesn't fire at all.

Implementation Methods

class LDPE_Service_AWSConnect_Transfer_Target {

    public static function transferStart( GearmanJob $job )
    {

        $workload   = $job->workload();
        $target     = unserialize( $workload );

        echo "transferStart/begin [ " . 
                $target->getShortRepresentation() . " ]\n";
        // perform a series of actions

        echo "transferStart/complete [ " . 
                $target->getShortRepresentation() . " ]\n"; 
        return serialize( $target );
    }


    public static function transferComplete( GearmanTask $task )
    {
        echo "transferComplete/begin\n";

        $workload       = $task->data();
        $parent_target  = unserialize( $workload );


        echo "transferComplete/complete\n";
    }
}

To be clear then, the "transferStart/begin" and "transferStart/complete" strings are correctly printed to logs, however, transferComplete/begin is never fired. What's going on?

Thanks! Alex


Seems as though the callbacks don't fire when run in background mode..

Upvotes: 3

Views: 1895

Answers (2)

Alex
Alex

Reputation: 41

I had tried that, it really boiled down to having the client run as a Gearman task itself. The client was being invoked as a part of a browser-invoked page. Seems that the callback wasn't being honored under this context. The solution was to move the client that schedules the callbacks into a Gearman-run method. I added a "scheduleXXXX" function to the work, which pretty much called the flow above. This function received the "normal" function's input, serialized.

Upvotes: 0

Yarek T
Yarek T

Reputation: 9925

Try setting the callback after your call to the process function

$client->addTaskBackground('my_task', 'payload');
$client->setCompleteCallback('complete');
$client->runTasks();

Upvotes: 1

Related Questions