Completed
Push — master ( dda8d2...337592 )
by Dominik
02:20
created

src/ProcessesExecutor.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Saxulum\ProcessesExecutor;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
use Symfony\Component\Process\Process;
8
9
final class ProcessesExecutor implements ProcessesExecutorInterface
10
{
11
    /**
12
     * @var LoggerInterface
13
     */
14
    private $logger;
15
16
    /**
17
     * @param LoggerInterface|null $logger
18
     */
19
    public function __construct(LoggerInterface $logger = null)
20
    {
21
        $this->logger = $logger ?? new NullLogger();
22
    }
23
24
    /**
25
     * @param Process[]|array $processes
26
     * @param \Closure|null   $startCallback
27
     * @param \Closure|null   $iterationCallback
28
     * @param \Closure|null   $finishCallback
29
     * @param int             $parallelProcessCount
30
     * @param int             $iterationSleepInMicroseconds
31
     */
32
    public function execute(
33
        array $processes,
34
        \Closure $startCallback = null,
35
        \Closure $iterationCallback = null,
36
        \Closure $finishCallback = null,
37
        int $parallelProcessCount = 8,
38
        int $iterationSleepInMicroseconds = 0
39
    ) {
40
        $this->logger->info(self::LOG_START);
41
42
        /** @var Process[]|array $parallelProcesses */
43
        $parallelProcesses = [];
44
45
        do {
46
            foreach ($parallelProcesses as $key => $process) {
47
                if (false === $process->isRunning()) {
48
                    $this->finishCallback($process, $key, $finishCallback);
49
                    unset($parallelProcesses[$key]);
50
                }
51
            }
52
53
            while (count($parallelProcesses) < $parallelProcessCount) {
54
                if (null !== $key = key($processes)) {
55
                    $process = current($processes);
56
                    $process->start();
57
                    $this->startCallback($process, $key, $startCallback);
58
                    $parallelProcesses[$key] = $process;
59
                    next($processes);
60
                } else {
61
                    break;
62
                }
63
            }
64
65
            usleep($iterationSleepInMicroseconds);
66
67
            $this->callIterationCallback($parallelProcesses, $iterationCallback);
68
        } while ([] !== $parallelProcesses);
69
70
        $this->logger->info(self::LOG_FINISHED);
71
    }
72
73
    /**
74
     * @param Process       $process
75
     * @param mixed         $key
76
     * @param \Closure|null $startCallback
77
     */
78 View Code Duplication
    private function startCallback(Process $process, $key, \Closure $startCallback = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        if (null === $startCallback) {
81
            return;
82
        }
83
84
        $this->logger->debug(self::LOG_START_START_CALLBACK, ['process' => $process, 'key' => $key]);
85
        $startCallback($process, $key);
86
        $this->logger->debug(self::LOG_STOP_START_CALLBACK, ['process' => $process, 'key' => $key]);
87
    }
88
89
    /**
90
     * @param Process[]|array $processes
91
     * @param \Closure|null   $iterationCallback
92
     */
93
    private function callIterationCallback(array $processes, \Closure $iterationCallback = null)
94
    {
95
        if (null === $iterationCallback) {
96
            return;
97
        }
98
99
        $this->logger->debug(self::LOG_START_ITERATION_CALLBACK, ['processes' => $processes]);
100
        $iterationCallback($processes);
101
        $this->logger->debug(self::LOG_STOP_ITERATION_CALLBACK, ['processes' => $processes]);
102
    }
103
104
    /**
105
     * @param Process       $process
106
     * @param mixed         $key
107
     * @param \Closure|null $finishCallback
108
     */
109 View Code Duplication
    private function finishCallback(Process $process, $key, \Closure $finishCallback = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if (null === $finishCallback) {
112
            return;
113
        }
114
115
        $this->logger->debug(self::LOG_START_FINISH_CALLBACK, ['process' => $process, 'key' => $key]);
116
        $finishCallback($process, $key);
117
        $this->logger->debug(self::LOG_STOP_FINISH_CALLBACK, ['process' => $process, 'key' => $key]);
118
    }
119
}
120