Sekvenčné reťazce
Najbežnejším vzorom pracovného postupu je lineárny reťazec krokov. Každý krok sa spustí po dokončení predchádzajúceho a dostane priebežne zhromažďovaný stav.
Metóda ->then()
Na pridanie sekvenčných krokov do pracovného postupu použite ->then():
use Queuety\Queuety;
Queuety::workflow( 'onboarding' )
->then( CreateAccountHandler::class )
->then( SendWelcomeEmailHandler::class )
->then( NotifyAdminHandler::class )
->dispatch( [ 'email' => 'user@example.com' ] );
Kroky sa vykonávajú v poradí, v akom boli pridané. Pracovný postup pokračuje až po úspešnom dokončení aktuálneho kroku.
Rozhranie Step
Obslužné funkcie krokov pracovného postupu implementujú rozhranie Queuety\Step:
use Queuety\Step;
class CreateAccountHandler implements Step {
public function handle( array $state ): array {
$user_id = wp_create_user( $state['email'], wp_generate_password() );
return [ 'user_id' => $user_id ];
}
public function config(): array {
return [
'max_attempts' => 3,
];
}
}
Hlavné rozdiely oproti rozhraniu Handler:
Handler (úlohy) |
Step (pracovné postupy) |
|
|---|---|---|
| Signatúra metódy | handle(array $payload): void |
handle(array $state): array |
| Vstup | Údaje úlohy | Zhromaždený stav pracovného postupu |
| Výstup | Nič (void) | Údaje, ktoré sa zlúčia so stavom |
Tok stavu
Každý krok dostane celý zhromaždený stav a vráti pole nových dvojíc kľúč – hodnota. Vrátené údaje sa zlúčia so stavom pre nasledujúce kroky:
class FetchUserHandler implements Step {
public function handle( array $state ): array {
// $state contains: ['user_id' => 42]
$user = get_user_by( 'ID', $state['user_id'] );
return [ 'user_name' => $user->display_name, 'user_email' => $user->user_email ];
}
public function config(): array {
return [];
}
}
class GenerateReportHandler implements Step {
public function handle( array $state ): array {
// $state contains: ['user_id' => 42, 'user_name' => 'Alice', 'user_email' => 'alice@example.com']
$report = build_report( $state['user_name'] );
return [ 'report_url' => $report->url ];
}
public function config(): array {
return [];
}
}
Konfigurácia kroku
Metóda config() podporuje nasledujúce kľúče:
| Kľúč | Typ | Predvolená hodnota | Opis |
|---|---|---|---|
max_attempts |
int |
3 |
Maximálny počet opakovaných pokusov pre tento krok |
backoff |
string |
exponential |
Stratégia čakania pred opakovaním |
Príklad postupnosti LLM
Bežným prípadom použitia je reťazenie volaní LLM API so spracovaním údajov:
use Queuety\Step;
class BuildPromptHandler implements Step {
public function handle( array $state ): array {
$user = get_user_by( 'ID', $state['user_id'] );
$posts = get_posts( [ 'author' => $user->ID, 'numberposts' => 10 ] );
$context = implode( "\n", array_map( fn( $p ) => $p->post_content, $posts ) );
return [
'prompt' => "Summarize this author's recent work:\n\n{$context}",
];
}
public function config(): array {
return [];
}
}
class CallOpenAIHandler implements Step {
public function handle( array $state ): array {
$response = wp_remote_post( 'https://api.openai.com/v1/chat/completions', [
'timeout' => 120,
'headers' => [
'Authorization' => 'Bearer ' . OPENAI_API_KEY,
'Content-Type' => 'application/json',
],
'body' => json_encode( [
'model' => 'gpt-4',
'messages' => [ [ 'role' => 'user', 'content' => $state['prompt'] ] ],
] ),
] );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return [ 'summary' => $body['choices'][0]['message']['content'] ];
}
public function config(): array {
return [ 'max_attempts' => 5 ];
}
}
class SaveSummaryHandler implements Step {
public function handle( array $state ): array {
$post_id = wp_insert_post( [
'post_title' => "Summary for user #{$state['user_id']}",
'post_content' => $state['summary'],
'post_status' => 'publish',
] );
return [ 'summary_post_id' => $post_id ];
}
public function config(): array {
return [];
}
}
Spustite postupnosť:
Queuety::workflow( 'summarize_author' )
->then( BuildPromptHandler::class )
->then( CallOpenAIHandler::class )
->then( SaveSummaryHandler::class )
->on_queue( 'llm' )
->max_attempts( 5 )
->dispatch( [ 'user_id' => 42 ] );
Ak volanie OpenAI vyprší, pracovník zopakuje iba tento krok. Výzva zostavená v kroku 0 je už bezpečne uložená v stave pracovného postupu.