GearmanClient::doBackground

(PECL gearman >= 0.5.0)

GearmanClient::doBackgroundExecuta uma tarefa em segundo plano

Descrição

public GearmanClient::doBackground(string $function, string $workload, ?string $unique = null): string

Executa uma tarefa em segundo plano, retornando um identificador de tarefa que pode ser usado para obter o status da tarefa em execução.

Parâmetros

function

Uma função registrada que o trabalhador deve executar

workload

Dados serializados a serem processados

unique

Um identificador único usado para identificar uma tarefa específica

Valor Retornado

O identificador da tarefa enviada.

Exemplos

Exemplo #1 Envia e monitora uma tarefa em segundo plano

O trabalhador neste exemplo tem um atraso artificial introduzido para imitar uma tarefa de longa execução. O script do cliente verifica periodicamente o status da tarefa em execução.

<?php

/* Cria o objeto */
$gmclient= new GearmanClient();

/* Adiciona o servidor padrão */
$gmclient->addServer();

/* Executa o cliente reverso */
$job_handle = $gmclient->doBackground("reverse", "this is a test");

if (
$gmclient->returnCode() != GEARMAN_SUCCESS)
{
echo
"Código de retorno incorreto\n";
exit;
}

$done = false;
do
{
sleep(3);
$stat = $gmclient->jobStatus($job_handle);
if (!
$stat[0]) // a tarefa é conhecida, então não é executada
$done = true;
echo
"Executando: " . ($stat[1] ? "true" : "false") . ", numerador: " . $stat[2] . ", denominador: " . $stat[3] . "\n";
}
while(!
$done);

echo
"Pronto!\n";

?>

O exemplo acima produzirá algo semelhante a:

Executando: true, numerador: 3, denominador: 14
Executando: true, numerador: 6, denominador: 14
Executando: true, numerador: 9, denominador: 14
Executando: true, numerador: 12, denominador: 14
Executando: false, numerador: 0, denominador: 0
Pronto!

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 1 note

up
8
felix at passcod dot name
7 years ago
Behaviour of the unique ID argument:

If it's not provided, it defaults to a UUIDv1 (timestamp + mac address).

Otherwise, if a job with the same unique ID exists (i.e. is either queued or currently executing), then that job is used, not the one you're submitting. This does not apply to past (completed) jobs, and works across the entire job server pool, assuming no partitioning.

Or with code (the values in [brackets] are the server-assigned job identifiers):

<?php

$gearman
->doBackground('sleep', '3', '123'); // [H:host:1] Starts sleep(3)
$gearman->doBackground('sleep', '5', '456'); // [H:host:2] Queues sleep(5)
$gearman->doBackground('sleep', '3', '123'); // [H:host:1] Does nothing
$gearman->doBackground('sleep', '1', '123'); // [H:host:1] Also does nothing (diff args don't trigger new jobs)

sleep (3);
// Job 123 [sleep(3)] has finished by now

$gearman->doBackground('sleep', '3', '123'); // [H:host:3] Starts a new job

?>
To Top