Skip to content

Commit 2a17953

Browse files
committed
[FEATURE] provide BeforeProcessRunnerExecutesCommandsEvent
1 parent d7e6e7b commit 2a17953

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

Classes/Domain/Service/ProcessRunner.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
*/
1414

1515
use B13\ContentSync\Domain\Model\Configuration;
16+
use B13\ContentSync\Event\BeforeProcessRunnerExecutesCommandsEvent;
1617
use B13\ContentSync\Exception;
18+
use Psr\EventDispatcher\EventDispatcherInterface;
1719
use Psr\Log\LoggerInterface;
1820
use Symfony\Component\Process\Process;
1921

2022
final readonly class ProcessRunner
2123
{
2224
public function __construct(
2325
private DatabaseParameterBuilder $databaseParameterBuilder,
24-
private LoggerInterface $logger
26+
private LoggerInterface $logger,
27+
private EventDispatcherInterface $eventDispatcher
2528
) {}
2629

2730
public function localToRemote(Configuration $configuration): void
@@ -42,8 +45,11 @@ public function localToRemote(Configuration $configuration): void
4245
$file = rtrim($file, '/');
4346
$commands[] = 'rsync -a --delete --omit-dir-times --no-owner --no-group ' . $localNode->getBasePath() . $file . '/ ' . $remoteNode->getConnection() . ':' . $remoteNode->getBasePath() . $file;
4447
}
48+
$beforeProcessRunnnerExecuteCommands = new BeforeProcessRunnerExecutesCommandsEvent($configuration, $commands);
49+
$this->eventDispatcher->dispatch($beforeProcessRunnnerExecuteCommands);
50+
$commands = $beforeProcessRunnnerExecuteCommands->commands;
4551
foreach ($commands as $command) {
46-
$this->exec($command);
52+
$this->exec($command, $beforeProcessRunnnerExecuteCommands->timeoutPerProcess);
4753
}
4854
}
4955

@@ -65,15 +71,18 @@ public function remoteToLocal(Configuration $configuration): void
6571
$file = rtrim($file, '/');
6672
$commands[] = 'rsync -a --delete --omit-dir-times --no-owner --no-group ' . $remoteNode->getConnection() . ':' . $remoteNode->getBasePath() . $file . '/ ' . $localNode->getBasePath() . $file;
6773
}
74+
$beforeProcessRunnnerExecuteCommands = new BeforeProcessRunnerExecutesCommandsEvent($configuration, $commands);
75+
$this->eventDispatcher->dispatch($beforeProcessRunnnerExecuteCommands);
76+
$commands = $beforeProcessRunnnerExecuteCommands->commands;
6877
foreach ($commands as $command) {
69-
$this->exec($command);
78+
$this->exec($command, $beforeProcessRunnnerExecuteCommands->timeoutPerProcess);
7079
}
7180
}
7281

73-
private function exec(string $cmd): void
82+
private function exec(string $cmd, int $timeout): void
7483
{
7584
$this->logger->debug($cmd);
76-
$process = Process::fromShellCommandline($cmd);
85+
$process = Process::fromShellCommandline(command: $cmd, timeout: $timeout);
7786
$process->run();
7887
if (!$process->isSuccessful()) {
7988
throw new Exception('cannot exec command ' . $cmd . ' with error ' . $process->getErrorOutput(), 1600757440);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace B13\ContentSync\Event;
6+
7+
/*
8+
* This file is part of TYPO3 CMS-based extension "content-sync" by b13.
9+
*
10+
* It is free software; you can redistribute it and/or modify it under
11+
* the terms of the GNU General Public License, either version 2
12+
* of the License, or any later version.
13+
*/
14+
15+
use B13\ContentSync\Domain\Model\Configuration;
16+
17+
final class BeforeProcessRunnerExecutesCommandsEvent
18+
{
19+
public int $timeoutPerProcess = 60;
20+
21+
public function __construct(
22+
public readonly Configuration $configuration,
23+
public array $commands
24+
) {}
25+
}

0 commit comments

Comments
 (0)