Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: service-based deferred listeners #4

Merged
merged 1 commit into from Mar 10, 2020

Conversation

weierophinney
Copy link
Contributor

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:

  • Adds a Psr\Container\ContainerInterface $container argument to
    TaskInterface::__invoke().
  • Modifies TaskWorker to compose the PSR-11 container, and pass it to
    tasks as it invokes them.
  • Adds ServiceBasedTask, which accepts a service name and payload
    arguments to its constructor. When invoked, it will pull the service
    from the container. If the service is a DeferredServiceListener, it
    will pull the listener from that instance and use that. It invokes the
    service.
  • Adds DeferredServiceListener, which creates a ServiceBasedTask with
    the service name passed to its constructor, and defines a
    getListener() method for getting the actual listener to use when
    handling the task.
  • Adds DeferredServiceListenerDelegator, which creates a
    DeferredServiceListener using the service name and the result of
    calling the delegator's factory argument.

What this means is that users can assign the
DeferredServiceListenerDelegator as a delegator factory on listeners
that 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.

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:

- Adds a `Psr\Container\ContainerInterface $container` argument to
  `TaskInterface::__invoke()`.
- Modifies `TaskWorker` to compose the PSR-11 container, and pass it to
  tasks as it invokes them.
- Adds `ServiceBasedTask`, which accepts a service name and payload
  arguments to its constructor. When invoked, it will pull the service
  from the container. If the service is a `DeferredServiceListener`, it
  will pull the listener from that instance and use that. It invokes the
  service.
- Adds `DeferredServiceListener`, which creates a ServiceBasedTask with
  the service name passed to its constructor, and defines a
  `getListener()` method for getting the actual listener to use when
  handling the task.
- Adds `DeferredServiceListenerDelegator`, which creates a
  `DeferredServiceListener` using the service name and the result of
  calling the delegator's factory argument.

What this means is that users can assign the
`DeferredServiceListenerDelegator` as a delegator factory on listeners
that should defer execution to the Swoole task workers. This will be
safer than the `DeferredListenerDelegator` in almost every case.
weierophinney added a commit that referenced this pull request Mar 10, 2020
@weierophinney weierophinney merged commit 017dc16 into master Mar 10, 2020
@weierophinney weierophinney deleted the feature/fix-closure-tasks branch March 10, 2020 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant