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\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Command to resume suspended workflows. |
13
|
|
|
* |
14
|
|
|
* @todo add support for resuming a set based on path |
15
|
|
|
* @todo add support for the separate-process cli switch |
16
|
|
|
*/ |
17
|
|
|
class ResumeCommand extends AbstractCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Set up the command. |
21
|
|
|
* |
22
|
|
|
* Define the name, options and help text. |
23
|
|
|
*/ |
24
|
|
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
parent::configure(); |
27
|
|
|
|
28
|
|
|
$this |
29
|
|
|
->setName('kaliop:workflows:resume') |
30
|
|
|
->setDescription('Restarts any suspended workflows.') |
31
|
|
|
->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question.") |
32
|
|
|
->addOption('no-transactions', 'u', InputOption::VALUE_NONE, "Do not use a repository transaction to wrap each workflow. Unsafe, but needed for legacy slot handlers") |
33
|
|
|
->addOption('workflow', 'w', InputOption::VALUE_REQUIRED, 'A single workflow to resume (workflow name).', null) |
34
|
|
|
->setHelp(<<<EOT |
35
|
|
|
The <info>kaliop:workflows:resume</info> command allows you to resume any suspended workflow |
36
|
|
|
EOT |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the command. |
42
|
|
|
* |
43
|
|
|
* @param InputInterface $input |
44
|
|
|
* @param OutputInterface $output |
45
|
|
|
* @return null|int null or 0 if everything went fine, or an error code |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
$start = microtime(true); |
51
|
|
|
|
52
|
|
|
$this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output); |
53
|
|
|
|
54
|
|
|
$workflowService = $this->getWorkflowService(); |
55
|
|
|
|
56
|
|
|
$workflowName = $input->getOption('workflow'); |
57
|
|
|
if ($workflowName != null) { |
58
|
|
|
$suspendedWorkflow = $workflowService->getWorkflow($workflowName); |
59
|
|
|
if (!$suspendedWorkflow) { |
60
|
|
|
throw new \Exception("Workflow '$workflowName' not found"); |
61
|
|
|
} |
62
|
|
|
if ($suspendedWorkflow->status != Migration::STATUS_SUSPENDED) { |
63
|
|
|
throw new \Exception("Workflow '$workflowName' is not suspended, can not resume it"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$suspendedWorkflows = array($suspendedWorkflow); |
67
|
|
|
} else { |
68
|
|
|
$suspendedWorkflows = $workflowService->getWorkflowsByStatus(Migration::STATUS_SUSPENDED); |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
$output->writeln('<info>Found ' . count($suspendedWorkflows) . ' suspended workflows</info>'); |
72
|
|
|
|
73
|
|
|
if (!count($suspendedWorkflows)) { |
74
|
|
|
$output->writeln('Nothing to do'); |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// ask user for confirmation to make changes |
79
|
|
|
if ($input->isInteractive() && !$input->getOption('no-interaction')) { |
80
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
81
|
|
|
if (!$dialog->askConfirmation( |
|
|
|
|
82
|
|
|
$output, |
83
|
|
|
'<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', |
84
|
|
|
false |
85
|
|
|
) |
86
|
|
|
) { |
87
|
|
|
$output->writeln('<error>Workflow resuming cancelled!</error>'); |
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$executed = 0; |
93
|
|
|
$failed = 0; |
94
|
|
|
|
95
|
|
|
foreach($suspendedWorkflows as $suspendedWorkflow) { |
96
|
|
|
$output->writeln("<info>Resuming {$suspendedWorkflow->name}</info>"); |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$workflowService->resumeWorkflow($suspendedWorkflow, !$input->getOption('no-transactions')); |
100
|
|
|
$executed++; |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
$output->writeln("\n<error>Workflow failed! Reason: " . $e->getMessage() . "</error>\n"); |
103
|
|
|
$failed++; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$time = microtime(true) - $start; |
108
|
|
|
$output->writeln("Resumed $executed workflows, failed $failed"); |
109
|
|
|
$output->writeln("Time taken: ".sprintf('%.2f', $time)." secs, memory: ".sprintf('%.2f', (memory_get_peak_usage(true) / 1000000)). ' MB'); |
110
|
|
|
|
111
|
|
|
if ($failed) { |
112
|
|
|
return 2; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|