|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Documents\Job; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class CreateJobCommand extends ContainerAwareCommand |
|
12
|
|
|
{ |
|
13
|
|
|
protected function configure() |
|
14
|
|
|
{ |
|
15
|
|
|
$this |
|
16
|
|
|
->setName('dtc:queue:create_job') |
|
17
|
|
|
->addArgument('worker_name', InputArgument::REQUIRED, 'Name of worker', null) |
|
18
|
|
|
->addArgument('method', InputArgument::REQUIRED, 'Method of worker to invoke', null) |
|
19
|
|
|
->addArgument('args', InputArgument::IS_ARRAY, 'Argument(s) for invoking worker method') |
|
20
|
|
|
->setDescription('Create a job - for expert users'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
24
|
|
|
{ |
|
25
|
|
|
$container = $this->getContainer(); |
|
26
|
|
|
$jobManager = $container->get('dtc_queue.job_manager'); |
|
27
|
|
|
$workerManager = $container->get('dtc_queue.worker_manager'); |
|
28
|
|
|
|
|
29
|
|
|
$workerName = $input->getArgument('worker_name'); |
|
30
|
|
|
$methodName = $input->getArgument('method'); |
|
31
|
|
|
$args = $input->getArgument('args'); |
|
32
|
|
|
|
|
33
|
|
|
$worker = $workerManager->getWorker($workerName); |
|
34
|
|
|
|
|
35
|
|
|
if (!$worker) { |
|
36
|
|
|
throw new \Exception("Worker `{$workerName}` is not registered."); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$when = new \DateTime(); |
|
40
|
|
|
$batch = true; |
|
41
|
|
|
$priority = 1; |
|
42
|
|
|
|
|
43
|
|
|
$jobClass = $worker->getJobClass(); |
|
44
|
|
|
$job = new $jobClass($worker, $batch, $priority, $when); |
|
45
|
|
|
$job->setMethod($methodName); |
|
46
|
|
|
$job->setArgs($args); |
|
47
|
|
|
$job->setLocked(null); |
|
48
|
|
|
|
|
49
|
|
|
$jobManager->save($job); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|