Issues (62)

Command/ResumeCommand.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Kaliop\eZWorkflowEngineBundle\Command;
4
5
use Kaliop\eZMigrationBundle\API\Value\Migration;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Command to resume suspended workflows.
12
 *
13
 * @todo add support for resuming a set based on path
14
 * @todo add support for the separate-process cli switch
15
 */
16
class ResumeCommand extends AbstractCommand
17
{
18
    /**
19
     * Set up the command.
20
     *
21
     * Define the name, options and help text.
22
     */
23
    protected function configure()
24
    {
25
        parent::configure();
26
27
        $this
28
            ->setName('kaliop:workflows:resume')
29
            ->setDescription('Restarts any suspended workflows.')
30
            ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question.")
31
            ->addOption('no-transactions', 'u', InputOption::VALUE_NONE, "Do not use a repository transaction to wrap each workflow. Unsafe, but needed for legacy slot handlers")
32
            ->addOption('workflow', 'w', InputOption::VALUE_REQUIRED, 'A single workflow to resume (workflow name).', null)
33
            ->setHelp(<<<EOT
34
The <info>kaliop:workflows:resume</info> command allows you to resume any suspended workflow
35
EOT
36
            );
37
    }
38
39
    /**
40
     * Execute the command.
41
     *
42
     * @param InputInterface $input
43
     * @param OutputInterface $output
44
     * @return null|int null or 0 if everything went fine, or an error code
45
     * @throws \Exception
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $start = microtime(true);
50
51
        $this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output);
52
53
        $workflowService = $this->getWorkflowService();
54
55
        $workflowName = $input->getOption('workflow');
56
        if ($workflowName != null) {
57
            $suspendedWorkflow = $workflowService->getWorkflow($workflowName);
0 ignored issues
show
The method getWorkflow() does not exist on Kaliop\eZWorkflowEngineB...e\WorkflowServiceFacade. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
            /** @scrutinizer ignore-call */ 
58
            $suspendedWorkflow = $workflowService->getWorkflow($workflowName);
Loading history...
58
            if (!$suspendedWorkflow) {
59
                throw new \Exception("Workflow '$workflowName' not found");
60
            }
61
            if ($suspendedWorkflow->status != Migration::STATUS_SUSPENDED) {
62
                throw new \Exception("Workflow '$workflowName' is not suspended, can not resume it");
63
            }
64
65
            $suspendedWorkflows = array($suspendedWorkflow);
66
        } else {
67
            $suspendedWorkflows = $workflowService->getWorkflowsByStatus(Migration::STATUS_SUSPENDED);
0 ignored issues
show
The method getWorkflowsByStatus() does not exist on Kaliop\eZWorkflowEngineB...e\WorkflowServiceFacade. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            /** @scrutinizer ignore-call */ 
68
            $suspendedWorkflows = $workflowService->getWorkflowsByStatus(Migration::STATUS_SUSPENDED);
Loading history...
68
        };
69
70
        $output->writeln('<info>Found ' . count($suspendedWorkflows) . ' suspended workflows</info>');
71
72
        if (!count($suspendedWorkflows)) {
73
            $output->writeln('Nothing to do');
74
            return 0;
75
        }
76
77
        // ask user for confirmation to make changes
78
        if ($input->isInteractive() && !$input->getOption('no-interaction')) {
79
            $dialog = $this->getHelperSet()->get('dialog');
80
            if (!$dialog->askConfirmation(
0 ignored issues
show
The method askConfirmation() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\DialogHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            if (!$dialog->/** @scrutinizer ignore-call */ askConfirmation(
Loading history...
81
                $output,
82
                '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>',
83
                false
84
            )
85
            ) {
86
                $output->writeln('<error>Workflow resuming cancelled!</error>');
87
                return 0;
88
            }
89
        }
90
91
        $executed = 0;
92
        $failed = 0;
93
94
        foreach($suspendedWorkflows as $suspendedWorkflow) {
95
            $output->writeln("<info>Resuming {$suspendedWorkflow->name}</info>");
96
97
            try {
98
                $workflowService->resumeWorkflow($suspendedWorkflow, !$input->getOption('no-transactions'));
0 ignored issues
show
The method resumeWorkflow() does not exist on Kaliop\eZWorkflowEngineB...e\WorkflowServiceFacade. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
                $workflowService->/** @scrutinizer ignore-call */ 
99
                                  resumeWorkflow($suspendedWorkflow, !$input->getOption('no-transactions'));
Loading history...
99
                $executed++;
100
            } catch (\Exception $e) {
101
                $output->writeln("\n<error>Workflow failed! Reason: " . $e->getMessage() . "</error>\n");
102
                $failed++;
103
            }
104
        }
105
106
        $time = microtime(true) - $start;
107
        $output->writeln("Resumed $executed workflows, failed $failed");
108
        $output->writeln("Time taken: ".sprintf('%.2f', $time)." secs, memory: ".sprintf('%.2f', (memory_get_peak_usage(true) / 1000000)). ' MB');
109
110
        if ($failed) {
111
            return 2;
112
        }
113
114
        return 0;
115
    }
116
}
117