-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobExecute.php
87 lines (71 loc) · 2.16 KB
/
jobExecute.php
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
<?php
declare(strict_types=1);
/**
* This example demonstrates a very simple system for running executable classes (jobs)
* that have been serialized in a message queue.
*
* The executables take parameters in their constructors and implement __invoke method to become callable.
* The __invoke method receives any services that are needed for the job to execute
* (e.g. database connection, mailing service, logger, etc.).
*/
use Dakujem\Sleeve;
use Dakujem\Wire\Genie;
class AnswerQuestionJob
{
private string $theQuestion;
public function __construct(string $theQuestion)
{
$this->theQuestion = $theQuestion;
}
/**
* Note the required service in parameters:
*/
public function __invoke(Oracle $oracle): ?string
{
return $oracle->answer($this->theQuestion);
}
}
class Queue
{
public function getNextJob()
{
// This would be fetched from the message queue or a database or wherever you store async jobs in.
$message = '
{
"class": "AnswerQuestionJob",
"args": ["Will there be a storm tomorrow?"]
}
';
return json_decode($message);
}
}
class Oracle
{
public function answer(string $question): ?string
{
$seed = rand(-10, 10);
return $seed > 0 ? 'yes' : ($seed < 0 ? 'no' : null);
}
}
// The service container has its services registered by their class name:
$container = new Sleeve([
Oracle::class => fn() => new Oracle(),
]);
if (!$container->get(Oracle::class) instanceof Oracle) {
// this won't happen
throw new Exception('WTF.');
}
// Fetch the next job from the job queue:
[$className, $args] = (new Queue())->getNextJob();
// Instantiate the job:
$job = new $className(...$args);
if (!is_callable($job)) {
throw new Exception('The job is not callable.');
}
// Since we have no idea what services the job needs to be executed
// and there may be multiple jobs with different signatures,
// we utilize dynamic dependency resolution:
$g = new Genie($container);
$g->invoke($job);
// That's it!
// Our job is called and the Oracle class is fetched from the container and delivered as an argument.