-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow-example.php
More file actions
101 lines (90 loc) · 2.91 KB
/
workflow-example.php
File metadata and controls
101 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
/**
* Workflow example for the Spooled PHP SDK.
*/
require_once __DIR__ . '/../vendor/autoload.php';
use Spooled\SpooledClient;
use Spooled\Config\ClientOptions;
// Create client
$client = new SpooledClient(new ClientOptions(
apiKey: getenv('API_KEY') ?: 'your-api-key',
));
// Create a workflow for order processing
$workflow = $client->workflows->create([
'name' => 'order-processing-' . time(),
'description' => 'Process an order through validation, payment, and fulfillment',
'jobs' => [
[
'key' => 'validate-order',
'queue' => 'orders',
'payload' => [
'orderId' => 'ORD-12345',
'step' => 'validate',
],
],
[
'key' => 'process-payment',
'queue' => 'payments',
'payload' => [
'orderId' => 'ORD-12345',
'step' => 'payment',
'amount' => 99.99,
],
'dependsOn' => ['validate-order'],
],
[
'key' => 'send-confirmation',
'queue' => 'notifications',
'payload' => [
'orderId' => 'ORD-12345',
'step' => 'confirm',
'template' => 'order-confirmation',
],
'dependsOn' => ['process-payment'],
],
[
'key' => 'fulfill-order',
'queue' => 'fulfillment',
'payload' => [
'orderId' => 'ORD-12345',
'step' => 'fulfill',
],
'dependsOn' => ['process-payment'],
],
[
'key' => 'send-shipping-notification',
'queue' => 'notifications',
'payload' => [
'orderId' => 'ORD-12345',
'step' => 'shipping',
'template' => 'shipping-notification',
],
'dependsOn' => ['fulfill-order'],
],
],
]);
echo "Created workflow: {$workflow->id}\n";
echo "Name: {$workflow->name}\n";
echo "Status: {$workflow->status}\n";
echo "Total jobs: {$workflow->totalJobs}\n";
// List jobs in the workflow
echo "\nWorkflow jobs:\n";
$jobs = $client->workflows->jobs->list($workflow->id);
foreach ($jobs as $job) {
$deps = !empty($job->dependsOn) ? ' (depends on: ' . implode(', ', $job->dependsOn) . ')' : '';
echo " - {$job->key}: {$job->status}{$deps}\n";
}
// Monitor workflow progress
echo "\nMonitoring workflow progress...\n";
for ($i = 0; $i < 10; $i++) {
sleep(1);
$status = $client->workflows->get($workflow->id);
echo " Status: {$status->status} (completed: {$status->completedJobs}/{$status->totalJobs})\n";
if ($status->status === 'completed' || $status->status === 'failed' || $status->status === 'cancelled') {
break;
}
}
// Final status
$final = $client->workflows->get($workflow->id);
echo "\nFinal workflow status: {$final->status}\n";