Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | require_once 'CRM/Core/Page.php'; | |
4 | ||
5 | /** | |
6 | * An example page which queues several tasks and then executes them | |
7 | */ | |
8 | class CRM_Demoqueue_Page_DemoQueue extends CRM_Core_Page { | |
31037a42 | 9 | |
6a488035 | 10 | function run() { |
56a0eade TO |
11 | $queueName = 'demoqueue_' . time(); |
12 | ||
13 | $queue = Civi::queue($queueName, [ | |
6a488035 | 14 | 'type' => 'Sql', |
56a0eade TO |
15 | 'runner' => 'task', |
16 | 'error' => 'abort', | |
b7c0a88f | 17 | ]); |
31037a42 | 18 | |
6a488035 TO |
19 | for ($i = 0; $i < 5; $i++) { |
20 | $queue->createItem(new CRM_Queue_Task( | |
b7c0a88f | 21 | ['CRM_Demoqueue_Page_DemoQueue', 'doMyWork'], // callback |
22 | [$i, "Task $i takes $i second(s)"], // arguments | |
6a488035 TO |
23 | "Task $i" // title |
24 | )); | |
25 | if ($i == 2) { | |
26 | $queue->createItem(new CRM_Queue_Task( | |
b7c0a88f | 27 | ['CRM_Demoqueue_Page_DemoQueue', 'addMoreWork'], // callback |
28 | [], // arguments | |
6a488035 TO |
29 | "Add More Work" // title |
30 | )); | |
31 | } | |
32 | } | |
31037a42 | 33 | |
56a0eade TO |
34 | \Civi\Api4\UserJob::create()->setValues([ |
35 | 'type_id:label' => 'Contact Import', | |
36 | 'status_id:name' => 'in_progress', | |
37 | 'queue_id.name' => $queue->getName(), | |
38 | ])->execute(); | |
39 | ||
b7c0a88f | 40 | $runner = new CRM_Queue_Runner([ |
6a488035 TO |
41 | 'title' => ts('Demo Queue Runner'), |
42 | 'queue' => $queue, | |
b7c0a88f | 43 | 'onEnd' => ['CRM_Demoqueue_Page_DemoQueue', 'onEnd'], |
6a488035 | 44 | 'onEndUrl' => CRM_Utils_System::url('civicrm/demo-queue/done'), |
b7c0a88f | 45 | ]); |
6a488035 TO |
46 | $runner->runAllViaWeb(); // does not return |
47 | } | |
31037a42 | 48 | |
6a488035 TO |
49 | /** |
50 | * Perform some business logic | |
d7c8cf03 EM |
51 | * @param \CRM_Queue_TaskContext $ctx |
52 | * @param $delay | |
53 | * @param $message | |
54 | * @return bool | |
6a488035 TO |
55 | */ |
56 | static function doMyWork(CRM_Queue_TaskContext $ctx, $delay, $message) { | |
57 | sleep(1); | |
58 | //sleep($delay); | |
59 | //$ctx->log->info($message); // PEAR Log interface | |
60 | //$ctx->logy->info($message); // PEAR Log interface -- broken, PHP error | |
61 | //CRM_Core_DAO::executeQuery('select from alsdkjfasdf'); // broken, PEAR error | |
62 | //throw new Exception('whoz'); // broken, exception | |
63 | return TRUE; // success | |
64 | } | |
31037a42 | 65 | |
6a488035 TO |
66 | /** |
67 | * Perform some business logic | |
d7c8cf03 EM |
68 | * @param \CRM_Queue_TaskContext $ctx |
69 | * @return bool | |
6a488035 TO |
70 | */ |
71 | static function addMoreWork(CRM_Queue_TaskContext $ctx) { | |
72 | sleep(1); | |
73 | for ($i = 0; $i < 5; $i++) { | |
74 | $ctx->queue->createItem(new CRM_Queue_Task( | |
b7c0a88f | 75 | ['CRM_Demoqueue_Page_DemoQueue', 'doMyWork'], // callback |
76 | [$i, "Extra task $i takes $i second(s)"], // arguments | |
6a488035 | 77 | "Extra Task $i" // title |
b7c0a88f | 78 | ), [ |
6a488035 | 79 | 'weight' => -1, |
b7c0a88f | 80 | ]); |
6a488035 TO |
81 | } |
82 | return TRUE; // success | |
83 | } | |
31037a42 | 84 | |
6a488035 TO |
85 | /** |
86 | * Handle the final step of the queue | |
d7c8cf03 | 87 | * @param \CRM_Queue_TaskContext $ctx |
6a488035 TO |
88 | */ |
89 | static function onEnd(CRM_Queue_TaskContext $ctx) { | |
90 | //CRM_Utils_System::redirect('civicrm/demo-queue/done'); | |
91 | CRM_Core_Error::debug_log_message('finished task'); | |
92 | //$ctx->logy->info($message); // PEAR Log interface -- broken, PHP error | |
93 | //CRM_Core_DAO::executeQuery('select from alsdkjfasdf'); // broken, PEAR error | |
94 | //throw new Exception('whoz'); // broken, exception | |
95 | } | |
96 | } |