mohamedallou/taskworker

Provides a process pool to integrate with web applications

v0.1.3 2023-01-14 19:58 UTC

This package is not auto-updated.

Last update: 2024-04-22 00:23:12 UTC


README

Getting started

Provides a process pool to integrate with web applications This Package is supposed to work in cooperation with fpm, or any process manager, since the scheduling and the output of response to the end web server is delegated to the fpm and is not handled within the package.

This process pool can also be used to run asynchronous tasks in background as well, as a message queue processor.

Architecture

The goal of this library is primarly to start the fpm child processes with a very light script that doesn't have the overhead of bootstrapping the whole web application with its configuration and dependencies, but instead it serializes the request and forwards it the actual long running worker process which contains an existing Web application instance in memory that handles then the request. The worker process is only started once on server start and doesn't need to be initialized with every request. This setting can save between 11% to 50% of the response time.

Installation

In your shell run:

composer require midouxxx/taskworker

Usage

Create a bootstrap file for the current web application dependencies and initialize the app and the dependency injection container. The bootstrap file needs to initialize a variable with the name of $taskHandler in the global scope which will be used by the further worker process steps.

Run the *start-worker-pool script with the given workers number and the path to the bootstrap file e.g:

php start-worker-pool.php --workers 10 --bootstrap="../examples/bootstrap.php"

Under examples folder you can find an example of basic index.php and bootstrap file that show the usage with existing applications

N.B

Due the current implementation, the maximum number of workers is limited to 255.

Provide an implementation of the TaskRunnerInterface. The handle method needs to contain the logic of the handling the unserialized request object.

Laminas Mezzio

The package provides basic example of the integration of this library with Laminas Mezzio App, a similar process is also# applicable to Symfony Application. The Mezzio TaskHandler class can be used as an alternative implementation for the Mezzio Application. We provide a serializable Stream and Response classes, since the orignal response class and its stream dependency are not properly serilazable and suitable to be sent over sockets.

Docker

The packages provides an example init.sh script that can be used to initialize a docker fpm image. You have to add the call to this script in your dockerfile within a CMD command. (There can only be one CMD entry in the docker file) e.g:

CMD ["/bin/bash", "-c", "set -e && /var/www/init.sh"]

Don't forget to add again the necessary CMD commands that are used in the original CMD image to your init.sh script, to keep the original behaviour of your docker image.

Roadmap

Build a standalone PHP Process manager as an fpm alternative.

License

MIT License

Project status

This project is still in early development phase.

Workflow:

Connect to Worker => Send Command => Get Socket => Wait For Response / Or ignore it

No Master Process, no manager : All processes are similar

Todos:

  • Check and Fix Signal Handling
  • Create a Docker Configuration To Expose Multiple Ports representing the workers
  • Handle New Lines and Specify Response Length instead of Using Fgets that terminates with each new Line
  • Use a main worker Scheduler that will be exposed externally
  • Use Round Robin with Request Number and Restart after configured X number of requests
  • Add Stop Command From the Client To the Worker with tcp

Ideas

  • ping the worker from the client to see if it answers quickly if not then it's busy/ besides the use of shared memory
  • Optimize Shared memory read to get the free workers
  • The Worker Pool Server will Keep Record Of the free workers, and will notify the waiting process for free workers with their ports as soon as one becomes available.
  • The Worker process script shares his status with the Worker Pool Server as soon as it finishes processing a request.
  • The Worker Pool Server assign the worker pool clients the port numbers of the freed workers consecutively so that no client will request an already assigned worker.
  • Add the possibility to restart the process after some time to avoid memory leaks

Other Ideas

  • Create Cron Manager
  • Create Async Jobs Worker and Queue Manager

    Exposing Multiple Ports

    In Docker File:

    EXPOSE 5000 5001 5002 5003
    

    and in the docker-compose file

    varnish:
      ports:
          - 5000
          - 5001
          - 5002
          - 5003