Documentation/Launching the application/WebRotor

WebRotor

WebRotor is a PHP library that allows asynchronous execution of applications on shared hosting. As is known, shared hosting has many usage restrictions, but this specialized program provides all the benefits of asynchronous functionality even on shared hosting.

The core principle of WebRotor is that when a request is made to the application, the index file does not process the requests directly but rather sends them to workers and fetches the responses back for display. Moreover, the worker is actually implemented as the code of this same index file. The workers are standard CRON-like processes, which are now available on practically every hosting provider. The difference in configuring these workers lies only in the designs of hosting providers' admin panels.

For the application to function correctly when launched using this technology, it must be developed with support for asynchronous mode.

Under the term "asynchrony" this document groups together true asynchronous mode and the conventional long-running mode, since the recommendations for both are identical.

In asynchronous mode, the loaded framework configurations, classes, initialized services, and cached data are reused, which significantly accelerates performance.
However, there are distinctive features, such as the need to monitor memory leaks more closely and eliminate blocking operations, and many familiar third-party libraries do not support asynchronous mode.
Storing state in the logical parts of the application becomes undesirable, especially if it is related to a request.

The framework interfaces ResetInterface and RollbackInterface are intended for resetting state after each asynchronous request.

To use WebRotor, you will need to modify the /public_html/index.php file (which is the presumed path to the index file on shared hosting) so that the HLEB2 framework runs in a loop. Here is a basic working example:

<?php
// File /public_html/index.php

use Phphleb\Webrotor\Config;
use 
Phphleb\Webrotor\Src\Handler\NyholmPsr7Creator;
use 
Phphleb\Webrotor\WebRotor;
use 
Psr\Http\Message\ResponseInterface;
use 
Psr\Http\Message\ServerRequestInterface;

include 
__DIR__ "/../vendor/autoload.php";

$psr7Creator = new NyholmPsr7Creator(); // or GuzzlePsr7Creator()

$config = new Config();
$config->logLevel 'warning';
$config->workerNum 2// Must correspond to the number of workers
$config->workerLifetimeSec 120// Must correspond to the worker launch interval
$config->runtimeDirectory __DIR__ '/../storage/wr-runtime';
$config->logDirectory __DIR__ '/../storage/wr-logs';

$server = new WebRotor($config);
$server->init($psr7Creator);

// Framework initialization outside the loop.
$framework = new Hleb\HlebAsyncBootstrap(__DIR__);

$server->run(function(ServerRequestInterface $requestResponseInterface $response) use ($framework) {
    
$res $framework->load($request)->getResponse();

    
$response->getBody()->write($res->getBody());
    foreach(
$res->getHeaders() as $name => $header) {
        
$response $response->withHeader($name$header);
    }
    return 
$response->withStatus($res->getStatus());
});

This code uses the HTTP client libraries nyholm/psr7 and nyholm/psr7-server, which need to be installed additionally.

To complete this configuration, you will also need to launch "workers" on your hosting. These are essentially the CRON-like processes provided by the hosting service. Typically, they are configured in the hosting admin panel, and while the design of the panel can vary, the principle remains the same. You need to launch two handlers at a two-minute interval (as shown in the settings above):

*/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=1

*/2 * * * * /usr/local/bin/php7.2 /my-project/public_html/index.php --id=2

These two processes differ only in the ID number for the workers. After this, all requests coming to the application will be handled by two asynchronous workers.
For more details, refer to the library documentation: github.com/phphleb/webrotor

For local development, you can avoid running workers, as requests will be processed in the usual manner if they are not running or inactive. This way, standard debugging tools, such as xDebug, will be available locally.

Shared hosting FrankenPHP

Страница создана: @fomiash
К началу страницы