Skip to content

Commit 18af8fa

Browse files
committed
Custom callable
1 parent 662e810 commit 18af8fa

File tree

7 files changed

+71
-35
lines changed

7 files changed

+71
-35
lines changed

src/Connection.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public function __construct(
4040

4141
/**
4242
* Checks if the given connection is the same as this one.
43-
*
44-
* @param \ProtoneMedia\LaravelTaskRunner\Connection $connection
4543
*/
4644
public function is(Connection $connection): bool
4745
{

src/PendingTask.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class PendingTask
1919

2020
private ?string $outputPath = null;
2121

22+
/**
23+
* @var callable|null
24+
*/
25+
private $onOutput = null;
26+
2227
public function __construct(
2328
public readonly Task $task
2429
) {
@@ -36,10 +41,26 @@ public static function make(string|Task|PendingTask $task): static
3641
return $task instanceof PendingTask ? $task : new PendingTask($task);
3742
}
3843

44+
/**
45+
* A PHP callback to run whenever there is some output available on STDOUT or STDERR.
46+
*/
47+
public function onOutput(callable $callback): self
48+
{
49+
$this->onOutput = $callback;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* Returns the callback that should be run whenever there is some output available on STDOUT or STDERR.
56+
*/
57+
public function getOnOutput(): ?callable
58+
{
59+
return $this->onOutput;
60+
}
61+
3962
/**
4063
* Returns the connection, if set.
41-
*
42-
* @return \ProtoneMedia\LaravelTaskRunner\Connection|null
4364
*/
4465
public function getConnection(): ?Connection
4566
{
@@ -48,8 +69,6 @@ public function getConnection(): ?Connection
4869

4970
/**
5071
* Setter for the connection.
51-
*
52-
* @param string|\ProtoneMedia\LaravelTaskRunner\Connection $connection
5372
*/
5473
public function onConnection(string|Connection $connection): self
5574
{
@@ -142,8 +161,6 @@ public function writeOutputTo(string $outputPath = null): self
142161

143162
/**
144163
* Checks if the given connection is the same as the connection of this task.
145-
*
146-
* @param bool|string|\ProtoneMedia\LaravelTaskRunner\Connection|callable|null $connection
147164
*/
148165
public function shouldRunOnConnection(bool|string|Connection|callable $connection = null): bool
149166
{
@@ -189,9 +206,6 @@ public function storeInTemporaryDirectory(): string
189206

190207
/**
191208
* Dispatches the task to the given task runner.
192-
*
193-
* @param \ProtoneMedia\LaravelTaskRunner\TaskDispatcher|null $taskDispatcher
194-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput|null
195209
*/
196210
public function dispatch(TaskDispatcher $taskDispatcher = null): ?ProcessOutput
197211
{

src/ProcessOutput.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ class ProcessOutput
1414

1515
private ?ProcessResult $illuminateResult = null;
1616

17+
/**
18+
* @var callable|null
19+
*/
20+
private $onOutput = null;
21+
22+
/**
23+
* A PHP callback to run whenever there is some output available on STDOUT or STDERR.
24+
*/
25+
public function onOutput(callable $callback): self
26+
{
27+
$this->onOutput = $callback;
28+
29+
return $this;
30+
}
31+
1732
/**
1833
* Appends the buffer to the output.
1934
*
@@ -23,6 +38,10 @@ class ProcessOutput
2338
public function __invoke(string $type, $buffer)
2439
{
2540
$this->buffer .= $buffer;
41+
42+
if ($this->onOutput) {
43+
($this->onOutput)($type, $buffer);
44+
}
2645
}
2746

2847
/**

src/ProcessRunner.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ class ProcessRunner
99
{
1010
/**
1111
* Runs the given process and waits for it to finish.
12-
*
13-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
1412
*/
15-
public function run(PendingProcess $process): ProcessOutput
13+
public function run(PendingProcess $process, callable $onOutput = null): ProcessOutput
1614
{
17-
return tap(new ProcessOutput, function (ProcessOutput $output) use ($process) {
15+
$output = new ProcessOutput;
16+
17+
if ($onOutput) {
18+
$output->onOutput($onOutput);
19+
}
20+
21+
return tap($output, function (ProcessOutput $output) use ($process) {
1822
$timeout = false;
1923

2024
try {

src/RemoteProcessRunner.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@
77

88
class RemoteProcessRunner
99
{
10+
/**
11+
* @var callable|null
12+
*/
13+
private $onOutput = null;
14+
1015
public function __construct(
1116
private Connection $connection,
1217
private ProcessRunner $processRunner
1318
) {
1419
}
1520

21+
/**
22+
* A PHP callback to run whenever there is some output available on STDOUT or STDERR.
23+
*/
24+
public function onOutput(callable $callback): self
25+
{
26+
$this->onOutput = $callback;
27+
28+
return $this;
29+
}
30+
1631
/**
1732
* Runs the full path of given script on the remote server.
1833
*/
@@ -61,8 +76,6 @@ private function sshOptions(): array
6176

6277
/**
6378
* Formats the script and output paths, and runs the script.
64-
*
65-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
6679
*/
6780
public function runUploadedScript(string $script, string $output, int $timeout = 0): ProcessOutput
6881
{
@@ -74,8 +87,6 @@ public function runUploadedScript(string $script, string $output, int $timeout =
7487

7588
/**
7689
* Formats the script and output paths, and runs the script in the background.
77-
*
78-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
7990
*/
8091
public function runUploadedScriptInBackground(string $script, string $output, int $timeout = 0): ProcessOutput
8192
{
@@ -90,8 +101,6 @@ public function runUploadedScriptInBackground(string $script, string $output, in
90101

91102
/**
92103
* Wraps the script in a bash subshell command, and runs it over SSH.
93-
*
94-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
95104
*/
96105
private function run(string $script, int $timeout = 0): ProcessOutput
97106
{
@@ -104,17 +113,15 @@ private function run(string $script, int $timeout = 0): ProcessOutput
104113
]);
105114

106115
$output = $this->processRunner->run(
107-
FacadesProcess::command($command)->timeout($timeout > 0 ? $timeout : null)
116+
FacadesProcess::command($command)->timeout($timeout > 0 ? $timeout : null),
117+
$this->onOutput
108118
);
109119

110120
return $this->cleanupOutput($output);
111121
}
112122

113123
/**
114124
* Removes the known hosts warning from the output.
115-
*
116-
* @param \ProtoneMedia\LaravelTaskRunner\ProcessOutput $processOutput
117-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
118125
*/
119126
private function cleanupOutput(ProcessOutput $processOutput): ProcessOutput
120127
{

src/Task.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public function getName(): string
3838

3939
/**
4040
* Returns the timeout of the task in seconds.
41-
*
42-
* @return int
4341
*/
4442
public function getTimeout(): ?int
4543
{
@@ -148,8 +146,6 @@ public function getScript(): string
148146

149147
/**
150148
* Returns a new PendingTask with this task.
151-
*
152-
* @return \ProtoneMedia\LaravelTaskRunner\PendingTask
153149
*/
154150
public function pending(): PendingTask
155151
{
@@ -158,8 +154,6 @@ public function pending(): PendingTask
158154

159155
/**
160156
* Returns a new PendingTask with this task.
161-
*
162-
* @return \ProtoneMedia\LaravelTaskRunner\PendingTask
163157
*/
164158
public static function make(...$arguments): PendingTask
165159
{

src/TaskDispatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public function __construct(private ProcessRunner $processRunner)
2828

2929
/**
3030
* Runs the given task.
31-
*
32-
* @return null|\ProtoneMedia\LaravelTaskRunner\ProcessOutput
3331
*/
3432
public function run(PendingTask $pendingTask): ?ProcessOutput
3533
{
@@ -79,8 +77,6 @@ private function runInBackground(PendingTask $pendingTask)
7977

8078
/**
8179
* Runs the given task on the given connection.
82-
*
83-
* @return \ProtoneMedia\LaravelTaskRunner\ProcessOutput
8480
*/
8581
private function runOnConnection(PendingTask $pendingTask): ProcessOutput
8682
{
@@ -90,6 +86,10 @@ private function runOnConnection(PendingTask $pendingTask): ProcessOutput
9086
['connection' => $pendingTask->getConnection(), 'processRunner' => $this->processRunner]
9187
);
9288

89+
if ($outputCallbable = $pendingTask->getOnOutput()) {
90+
$runner->onOutput($outputCallbable);
91+
}
92+
9393
$id = $pendingTask->getId() ?: Str::random(32);
9494

9595
$runner->verifyScriptDirectoryExists()->upload("{$id}.sh", $pendingTask->task->getScript());

0 commit comments

Comments
 (0)