feat: service-based deferred listeners #4
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In playing some more with Swoole task workers, I discovered that any
listeners you provide MUST be serializable - which poses a problem for
listeners that compose things such as HTTP clients, database adapters,
etc.
This patch:
Psr\Container\ContainerInterface $container
argument toTaskInterface::__invoke()
.TaskWorker
to compose the PSR-11 container, and pass it totasks as it invokes them.
ServiceBasedTask
, which accepts a service name and payloadarguments to its constructor. When invoked, it will pull the service
from the container. If the service is a
DeferredServiceListener
, itwill pull the listener from that instance and use that. It invokes the
service.
DeferredServiceListener
, which creates a ServiceBasedTask withthe service name passed to its constructor, and defines a
getListener()
method for getting the actual listener to use whenhandling the task.
DeferredServiceListenerDelegator
, which creates aDeferredServiceListener
using the service name and the result ofcalling the delegator's factory argument.
What this means is that users can assign the
DeferredServiceListenerDelegator
as a delegator factory on listenersthat should defer execution to the Swoole task workers. This will be
safer than the
DeferredListenerDelegator
in almost every case.Since this patch changes the signature of an interface method, I'm declaring it a new major version feature.