-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_bootstrap.php
57 lines (46 loc) · 1.94 KB
/
worker_bootstrap.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
<?php
use CultuurNet\UDB3\CommandHandling\QueueJob;
use CultuurNet\UDB3\Log\ContextEnrichingLogger;
use Psr\Container\ContainerInterface;
require_once 'vendor/autoload.php';
Resque_Event::listen(
'beforePerform',
function (Resque_Job $job) {
/** @var ContainerInterface $container */
$container = require __DIR__ . '/bootstrap.php';
$logger = new ContextEnrichingLogger(
$container->get('logger_factory.resque_worker')($job->queue),
['job_id' => $job->payload['id']]
);
$logger->info('job_started');
try {
$args = $job->getArguments();
$context = unserialize(base64_decode($args['context']));
$container->get('impersonator')->impersonate($context);
// Command bus service name is based on queue name + _command_bus_out.
// Eg. Queue "event" => command bus "event_command_bus_out".
$commandBusServiceName = $job->queue . '_command_bus_out';
// Allows to access the command bus and logger in perform() of jobs that come out of the queue.
QueueJob::setLogger($logger);
QueueJob::setCommandBus($container->get($commandBusServiceName));
} catch (Throwable $e) {
$logger->error('job_failed', ['exception' => $e]);
$logger->info('job_finished');
// Make sure to exit so the job doesn't get performed if there's an error in the beforePerform
// Don't re-throw because it will pollute the logs
exit;
}
}
);
Resque_Event::listen(
'afterPerform',
function (Resque_Job $job) {
/** @var ContainerInterface $container */
$container = require __DIR__ . '/bootstrap.php';
$logger = new ContextEnrichingLogger(
$container->get('logger_factory.resque_worker')($job->queue),
['job_id' => $job->payload['id']]
);
$logger->info('job_finished');
}
);