Skip to content

Conversation

@josbeir
Copy link
Contributor

@josbeir josbeir commented Dec 17, 2025

This PR adds support for running queue jobs in isolated subprocesses. That allows code changes to take effect immediately without restarting long‑running workers, speeding up iterative development and reducing workflow friction. Subprocess execution also contains job failures to the child process, improving stability of the main worker and making debugging safer and more predictable.

Key Features

Subprocess Execution: New SubprocessProcessor wraps the standard processor to execute jobs in isolated PHP processes via proc_open(), communicating job data and results over stdin using JSON.

CLI Command: SubprocessJobRunnerCommand handles single job execution within the subprocess, accepting job data via STDIN and returning results via STDOUT.

Worker Integration: Added --subprocess flag to WorkerCommand to enable subprocess mode. Also configurable via Queue.subprocess config key.

Configuration

// Enable globally
Configure::write('Queue.subprocess', true);

// Or use CLI flag
bin/cake queue worker --subprocess

Note about logging to stdout

As messaging between worker and sub command is using stdout with json i decided to redirect all other stdout logging to stderr. This seemed like a fine balance as subprocesses would only be used locally. And even if they where to be used in production for any reason (memory, crash resilience?) then a dedicated log engine could be used instead.

Copilot AI review requested due to automatic review settings December 17, 2025 14:06

This comment was marked as resolved.

@josbeir josbeir marked this pull request as draft December 17, 2025 14:47
@josbeir josbeir force-pushed the sub-process branch 2 times, most recently from c80bb73 to 849f868 Compare December 17, 2025 15:58
@josbeir josbeir self-assigned this Dec 17, 2025
@josbeir josbeir added the enhancement New feature or request label Dec 17, 2025
@josbeir josbeir added this to the 2.next milestone Dec 17, 2025
@josbeir josbeir marked this pull request as ready for review December 17, 2025 16:51
@josbeir josbeir requested review from ADmad and markstory December 17, 2025 16:51
}

try {
$data = json_decode($input, true, 512, JSON_THROW_ON_ERROR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's 512? Can we use the union of the flag constants?

Comment on lines +127 to +134
while (!feof(STDIN)) {
$chunk = fread(STDIN, 8192);
if ($chunk === false) {
break;
}

$input .= $chunk;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we read input from $io Using stdin will make this harder to test/mock/change in the future.


/**
* Configure logging to use STDERR to prevent job logs from contaminating STDOUT.
* Reconfigures all CakePHP loggers to write to STDERR with no additional formatting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Won't that break logging outputs to files/syslog log drain services?

Comment on lines +196 to +199
return [
'success' => false,
'error' => sprintf('Subprocess output exceeded maximum size of %d bytes', $maxOutputSize),
];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.


$exitCode = proc_close($process);

if ($exitCode !== 0 && empty($output)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the output ever be '0'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request needs squashing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants