GearmanClient::doNormal

(No version information available, might only be in Git)

GearmanClient::doNormalExécute une tâche et retourne le résultat

Description

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

Exécute une tâche et retourne une chaîne de caractères représentant le résultat. Il appartient aux classes GearmanClient et GearmanWorker d'accepter le format du résultat.

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

Une chaîne de caractères représentant le résultat de la tâche exécutée.

Exemples

Exemple #1 Soumission d'une tâche avec un retour immédiat

<?php

?>
<?php

# Code client

echo "Début\n";

# Création de notre objet client.
$gmclient= new GearmanClient();

# Ajout du serveur par défaut (localhost).
$gmclient->addServer();

echo
"Envoi de la tâche\n";

$result = $gmclient->doNormal("reverse", "Hello!");

echo
"Succès : $result\n";

?>
<?php

echo "Début\n";

# Création de l'objet worker.
$gmworker= new GearmanWorker();

# Ajout du serveur par défaut (localhost).
$gmworker->addServer();

# Enregistre la fonction "reverse" avec le serveur. Modifie la fonction worker
# en "reverse_fn_fast" pour un worker plus rapide avec aucune sortie.
$gmworker->addFunction("reverse", "reverse_fn");

print
"Attente d'une tâche...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"return_code : " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
return
strrev($job->workload());
}

?>

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

Début
Envoi de la tâche
Succès : !olleH

Exemple #2 Soumission d'une tâche et récupération incrémentale du statut

Une tâche est soumise et le script boucle pour récupérer les informations de statut. Le worker a un délai artificiel qui le transforme en une tâche longue et envoie le statut et les données lorsque l'exécution survient. Chaque sous-appel à la fonction GearmanClient::doNormal() produit des informations de statut sur la tâche en cours.

<?php

# Code client

# Création de notre objet client.
$gmclient= new GearmanClient();

# Ajout du srveur par défaut (localhost).
$gmclient->addServer();

echo
"Envoi de la tâche\n";

# Envoi de la tâche reverse
do
{
$result = $gmclient->doNormal("reverse", "Hello!");
# Vérifie les paquets retournés ainsi que les erreurs.

switch($gmclient->returnCode())
{
case
GEARMAN_WORK_DATA:
echo
"Données : $result\n";
break;
case
GEARMAN_WORK_STATUS:
list(
$numerator, $denominator)= $gmclient->doStatus();
echo
"Statut : $numerator/$denominator terminé\n";
break;
case
GEARMAN_WORK_FAIL:
echo
"Échec\n";
exit;
case
GEARMAN_SUCCESS:
break;
default:
echo
"RET : " . $gmclient->returnCode() . "\n";
echo
"Erreur : " . $gmclient->error() . "\n";
echo
"Errno : " . $gmclient->getErrno() . "\n";
exit;
}
}
while(
$gmclient->returnCode() != GEARMAN_SUCCESS);

echo
"Success: $result\n";

?>
<?php

# Code du worker

echo "Début\n";

# Création de notre objet worker.
$gmworker= new GearmanWorker();

# Ajout du serveur par défaut (localhost).
$gmworker->addServer();

# Enregistre la fonction "reverse" avec le serveur.
$gmworker->addFunction("reverse", "reverse_fn");

print
"Attente d'une tâche...\n";
while(
$gmworker->work())
{
if (
$gmworker->returnCode() != GEARMAN_SUCCESS)
{
echo
"return_code : " . $gmworker->returnCode() . "\n";
break;
}
}

function
reverse_fn($job)
{
echo
"Tâche reçue : " . $job->handle() . "\n";

$workload = $job->workload();
$workload_size = $job->workloadSize();

echo
"Workload : $workload ($workload_size)\n";

# This status loop is not needed, just showing how it works
for ($x= 0; $x < $workload_size; $x++)
{
echo
"Envoi du statut : " + $x + 1 . "/$workload_size terminé\n";
$job->sendStatus($x+1, $workload_size);
$job->sendData(substr($workload, $x, 1));
sleep(1);
}

$result= strrev($workload);
echo
"Résultat : $result\n";

# Retourne ce que l'on souhaite retourner au client.
return $result;
}

?>

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

Worker output:

Début
Attente d'une tâche...
Tâche reçue : H:foo.local:106
Workload : Hello! (6)
1/6 terminé
2/6 terminé
3/6 terminé
4/6 terminé
5/6 terminé
6/6 terminé
Résultat : !olleH

Sortie client :

Début
Envoi de la tâche
Statut : 1/6 terminé
Données : H
Statut : 2/6 terminé
Données : e
Statut : 3/6 terminé
Données : l
Statut : 4/6 terminé
Données : l
Status: 5/6 terminé
Données : o
Statut : 6/6 terminé
Données : !
Succè s: !olleH

Voir aussi

add a note add a note

User Contributed Notes 2 notes

up
0
stanislav dot reshetnev at gmail dot com
9 years ago
doNormal don't work in version php_gearman 0.8.3:

PHP Fatal error:  Call to undefined method GearmanClient::doNormal()

But it exist in version 1.1.2.
up
-2
fastest963 at gmail dot com
11 years ago
do() or doNormal() will block until the job is accepted by the worker. Looping is only necessary if you care about the response from the job. Use setTimeout() to control how long PHP will block.

If it does timeout, it throws a PHP Warning:
PHP Warning:  GearmanClient::do(): _client_do(GEARMAN_TIMEOUT) occured during gearman_client_run_tasks() -> libgearman/client.cc:174
To Top