Skip to content

Skip the sleep when the job returns true - #57

Merged
DavidBadura merged 1 commit into
1.6.xfrom
idle-aware-sleep
Oct 2, 2026
Merged

DavidBadura merged 1 commit into
1.6.xfrom
idle-aware-sleep

Conversation

@DavidBadura

Copy link
Copy Markdown
Member

The worker always slept between iterations, even when the job just processed something and there is probably more to do. A job can now return true to say it did work, and the worker starts the next iteration right away. This is mainly for queue consumers, which can then drain a full queue without a pause after every message and only sleep once it is empty.

Only a strict true skips the sleep. Jobs returning nothing keep the current behaviour, so this is BC safe. The job type is now Closure(Closure):(bool|void).

This touches the same loop as #52, so whichever is merged second needs a small rebase.

@DavidBadura DavidBadura added this to the 1.6.0 milestone Oct 2, 2026
@DavidBadura DavidBadura added the enhancement New feature or request label Oct 2, 2026
Comment thread src/DefaultWorker.php
private Closure $timeMeasure;

/** @param Closure(Closure):void $job */
/** @param Closure(Closure):(bool|void) $job return true if the job did work to skip the sleep */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

isnt it more true|void ? :D

@DavidBadura DavidBadura Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Only true changes the behaviour, that's right. I'd still keep bool though, for two reasons.

false reads as an explicit "nothing to do, sleep", which makes the intent clearer than mixing return; and return true;:

$worker = DefaultWorker::create(
    static function (callable $stop) use ($queue): bool {
        $message = $queue->pop();

        if ($message === null) {
            return false; // queue is empty, sleep
        }

        handle($message);

        return true; // there may be more, continue immediately
    },
);

And it lets a job pass a bool result straight through, which is common for consumer APIs:

// with bool|void
static fn (callable $stop): bool => $consumer->consumeOne();

// with true|void
static function (callable $stop) use ($consumer) {
    if ($consumer->consumeOne()) {
        return true;
    }
};

With true|void, PHPStan rejects the first closure because false is not allowed, so the result has to be converted by hand. That needs an extra branch just to drop false, and the closure can't have a native return type anymore: true|void isn't valid PHP, and true|null would throw a TypeError on the path without a return, unless you add an explicit return null.

A job can now return true to signal that it did work, so the next
iteration starts right away. This lets queue consumers drain a full
queue without pausing between messages. Any other return value keeps
the regular sleep behaviour.
@DavidBadura
DavidBadura merged commit eae43ed into 1.6.x Oct 2, 2026
16 checks passed
@DavidBadura
DavidBadura deleted the idle-aware-sleep branch October 2, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants