Completed
Push — master ( 29ee36...16ede1 )
by Tom
03:58
created

RunCommand::askJobCode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 3
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 3
1
<?php
2
3
namespace N98\Magento\Command\System\Cron;
4
5
use Exception;
6
use Magento\Cron\Model\Schedule;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class RunCommand extends AbstractCronCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('sys:cron:run')
17
            ->addArgument('job', InputArgument::OPTIONAL, 'Job code')
18
            ->setDescription('Runs a cronjob by job code');
19
        $help = <<<HELP
20
If no `job` argument is passed you can select a job from a list.
21
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM
22
HELP;
23
        $this->setHelp($help);
24
    }
25
26
    /**
27
     * @param InputInterface $input
28
     * @param OutputInterface $output
29
     * @throws \Exception
30
     * @return int|void
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        list($jobCode, $jobConfig, $model) = $this->getJobForExecuteMethod($input, $output);
35
36
        $callback = array($model, $jobConfig['method']);
37
38
        $output->write(
39
            '<info>Run </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> '
40
        );
41
42
        /* @var $schedule \Magento\Cron\Model\Schedule */
43
        $schedule = $this->cronScheduleCollection->getNewEmptyItem();
44
        $schedule
45
            ->setJobCode($jobCode)
46
            ->setStatus(Schedule::STATUS_RUNNING)
47
            ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
48
            ->save();
49
50
        try {
51
            $this->state->emulateAreaCode('crontab', $callback, array($schedule));
52
53
            $schedule
54
                ->setStatus(Schedule::STATUS_SUCCESS)
55
                ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
56
                ->save();
57
        } catch (Exception $e) {
58
            $schedule
59
                ->setStatus(Schedule::STATUS_ERROR)
60
                ->setMessages($e->getMessage())
61
                ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
62
                ->save();
63
        }
64
65
        $output->writeln('<info>done</info>');
66
    }
67
}
68