GearmanClient::doBackground

(PECL gearman >= 0.5.0)

GearmanClient::doBackgroundExécute une tâche en arrière-plan

Description

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

Exécute une tâche en arrière-plan, retourne le gestionnaire de travaux qui pourra être utilisé pour récupérer le statut de la tâche en cours.

Liste de paramètres

function

Une fonction enregistrée que le travailleur va exécuter

workload

Données sérialisées à analyser

unique

Un identifiant unique utilisé pour identifier une tâche particulière

Valeurs de retour

Le gestionnaire de travaux pour la tâche soumise.

Exemples

Exemple #1 Soumet et surveille un travail en arrière-plan

L'agent de cet exemple a un délai artificiel introduit pour simuler un travail dont l'exécution prend beaucoup de temps. Le script du client vérifie périodiquement le statut du travail en cours.

<?php

/* Crée un client */
$gmclient= new GearmanClient();

/* Ajoute un serveur par défaut */
$gmclient->addServer();

/* Exécute le client */
$job_handle = $gmclient->doBackground("reverse", "this is a test");

if (
$gmclient->returnCode() != GEARMAN_SUCCESS)
{
echo
"Code retour erroné\n";
exit;
}

$done = false;
do
{
sleep(3);
$stat = $gmclient->jobStatus($job_handle);
if (!
$stat[0]) // the job is known so it is not done
$done = true;
echo
"Exécution : " . ($stat[1] ? "true" : "false") . ", numérateur : " . $stat[2] . ", dénominateur : " . $stat[3] . "\n";
}
while(!
$done);

echo
"fait !\n";

?>

Résultat de l'exemple ci-dessus est similaire à :

Exécution : true, numérateur : 3, dénominateur : 14
Exécution : true, numérateur : 6, dénominateur : 14
Exécution : true, numérateur : 9, dénominateur : 14
Exécution : true, numérateur : 12, dénominateur : 14
Exécution : false, numérateur : 0, dénominateur : 0
fait !

Voir aussi

add a note add a note

User Contributed Notes 1 note

up
6
felix at passcod dot name
6 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