|
@@ -2,12 +2,13 @@
|
|
|
|
|
|
namespace WorkflowBundle\Command;
|
|
|
|
|
|
-use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use WorkflowBundle\Services\TaskLoggerService;
|
|
|
|
|
|
-class LockWaitCommand extends Command
|
|
|
+class LockWaitCommand extends ContainerAwareCommand
|
|
|
{
|
|
|
|
|
|
protected function configure()
|
|
@@ -15,32 +16,12 @@ class LockWaitCommand extends Command
|
|
|
$this
|
|
|
->setName('lock:wait')
|
|
|
->setDescription('Lock wait command')
|
|
|
- ->setHelp('Lock wait command. Wait for lock a file. The lock can be either exclusive or shared')
|
|
|
- ->addArgument(
|
|
|
- 'filename',
|
|
|
- InputOption::VALUE_REQUIRED,
|
|
|
- 'Filename'
|
|
|
- )
|
|
|
- ->addOption(
|
|
|
- 'dir',
|
|
|
- null,
|
|
|
- InputOption::VALUE_REQUIRED,
|
|
|
- 'Directory path file',
|
|
|
- '/tmp'
|
|
|
- )
|
|
|
- ->addOption(
|
|
|
- 'timeout',
|
|
|
- null,
|
|
|
- InputOption::VALUE_REQUIRED,
|
|
|
- 'Timeout execution (ms)',
|
|
|
- 600
|
|
|
- )
|
|
|
- ->addOption(
|
|
|
- 'no-exclusive',
|
|
|
- 'no-e',
|
|
|
- InputOption::VALUE_NONE,
|
|
|
- 'No exclusive lock'
|
|
|
- )
|
|
|
+ ->setHelp('Lock wait command. Wait for lock a file. The lock can be either exclusive or shared')
|
|
|
+ ->addArgument('filename', InputOption::VALUE_REQUIRED, 'Filename')
|
|
|
+ ->addOption('command', 'c', InputOption::VALUE_REQUIRED, 'Command to execute')
|
|
|
+ ->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Directory path file', '/tmp')
|
|
|
+ ->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'Timeout execution (ms)', 600)
|
|
|
+ ->addOption('no-exclusive', 'no-e', InputOption::VALUE_NONE, 'No exclusive lock')
|
|
|
;
|
|
|
}
|
|
|
|
|
@@ -51,16 +32,17 @@ class LockWaitCommand extends Command
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
{
|
|
|
$filename = $input->getArgument('filename');
|
|
|
+ $command = $input->getOption('command');
|
|
|
$dir = $input->getOption('dir');
|
|
|
$timeout = $input->getOption('timeout');
|
|
|
$no_exclusive = $input->getOption('no-exclusive');
|
|
|
- if ($filename) {
|
|
|
+ if ($filename && $command) {
|
|
|
$handle = $this->fopen($filename, $dir);
|
|
|
- if ($this->flock($handle, $timeout, $no_exclusive)) {
|
|
|
+ if ($this->flock($handle, $command, $timeout, $no_exclusive)) {
|
|
|
$output->writeln('<info>OK</info>');
|
|
|
}
|
|
|
} else {
|
|
|
- $output->writeln('<error>Enter a valid filename</error>');
|
|
|
+ $output->writeln('<error>Enter a valid filename and command</error>');
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -91,35 +73,51 @@ class LockWaitCommand extends Command
|
|
|
|
|
|
/**
|
|
|
* @param resource $handle
|
|
|
+ * @param string $command
|
|
|
* @param int $timeout
|
|
|
* @param boolean $no_exclusive
|
|
|
*
|
|
|
- * @throws \Exception
|
|
|
+ * @return boolean
|
|
|
*/
|
|
|
- protected function flock($handle, $timeout = 600, $no_exclusive = false)
|
|
|
+ protected function flock($handle, $command, $timeout = 600, $no_exclusive = false)
|
|
|
{
|
|
|
pcntl_signal(SIGALRM, function() {
|
|
|
exit(1);
|
|
|
});
|
|
|
pcntl_alarm($timeout);
|
|
|
- try {
|
|
|
- $operation = LOCK_EX;
|
|
|
- if ($no_exclusive) {
|
|
|
- $operation = LOCK_SH;
|
|
|
- }
|
|
|
- if (!flock($handle, $operation)) {
|
|
|
- throw new \Exception("Timeout");
|
|
|
- }
|
|
|
- } finally {
|
|
|
- pcntl_alarm(0);
|
|
|
- pcntl_signal_dispatch();
|
|
|
- pcntl_signal(SIGALRM, SIG_DFL);
|
|
|
+ $operation = LOCK_EX;
|
|
|
+ if ($no_exclusive) {
|
|
|
+ $operation = LOCK_SH;
|
|
|
}
|
|
|
-
|
|
|
+ if (flock($handle, $operation)) {
|
|
|
+ $exit_code = $this->runProcess($command)['exit_code'];
|
|
|
+ } else {
|
|
|
+ echo 'No se pudo obtener el lock' . PHP_EOL;
|
|
|
+ $exit_code = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ pcntl_alarm(0);
|
|
|
+ pcntl_signal_dispatch();
|
|
|
+ pcntl_signal(SIGALRM, SIG_DFL);
|
|
|
+
|
|
|
flock($handle, LOCK_UN);
|
|
|
fclose($handle);
|
|
|
|
|
|
- return true;
|
|
|
+ exit($exit_code);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $filename
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected function runProcess($filename)
|
|
|
+ {
|
|
|
+ /* @var $taskloggerService TaskLoggerService */
|
|
|
+ $taskloggerService = $this->getContainer()->get('flowdat_tasklogger_service');
|
|
|
+
|
|
|
+ return $taskloggerService->runProcess($filename);
|
|
|
}
|
|
|
|
|
|
}
|