-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathCostsExampleTask.php
More file actions
85 lines (72 loc) · 1.89 KB
/
CostsExampleTask.php
File metadata and controls
85 lines (72 loc) · 1.89 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
<?php
declare(strict_types=1);
namespace Queue\Queue\Task;
use Queue\Queue\AddFromBackendInterface;
use Queue\Queue\AddInterface;
use Queue\Queue\Task;
/**
* A Costs QueueTask example.
*/
class CostsExampleTask extends Task implements AddInterface, AddFromBackendInterface {
/**
* @var int
*/
public int $costs = 55;
/**
* @var int
*/
protected int $sleep = 10;
/**
* To invoke from CLI execute:
* - bin/cake queue add Queue.CostsExample
*
* @param string|null $data
*
* @return void
*/
public function add(?string $data): void {
$this->io->out('CakePHP Queue CostsExample task.');
$this->io->hr();
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
$this->io->out(' bin/cake queue run');
$this->io->out(' ');
$this->io->out('You can find the sourcecode of this task in: ');
$this->io->out(__FILE__);
$this->io->out(' ');
$this->QueuedJobs->createJob('Queue.CostsExample');
$this->io->success('OK, job created, now run the worker');
}
/**
* @return string|null
*/
public function description(): ?string {
return __d('queue', 'Demonstrates cost management to prevent server overload');
}
/**
* CostsExample run function.
* This function is executed, when a worker is executing a task.
*
* @param array<string, mixed> $data The array passed to QueuedJobsTable::createJob()
* @param int $jobId The id of the QueuedJob entity
*
* @return void
*/
public function run(array $data, int $jobId): void {
$this->io->hr();
$this->io->out('CakePHP Queue CostsExample task.');
sleep($this->sleep);
$this->io->hr();
$this->io->success(' -> Success, the CostsExample Job was run. <-');
}
/**
* @param int $seconds
*
* @return void
*/
public function setSleep(int $seconds): void {
$this->sleep = $seconds;
}
}