Class
Swerve
What swerve gives the application: its log, the cache and the messages shared by every worker, and the state of the worker.
Introduction#
Every method is static. Workers share no memory: the cache and the messages go through the
master process. A coroutine that swerve.php starts waits for the worker to serve before its
first call goes through; directly in swerve.php the cache and publishing throw (see docs/bootstrap.md).
Class synopsis#
final class Swerve {
/* Methods */
public static Swerve::log(): LoggerInterface
public static Swerve::setLog(LoggerInterface $log): void
public static Swerve::publish(string $topic, mixed $message): void
public static Swerve::publishRaw(string $topic, string $bytes): void
public static Swerve::subscribe(
string $topic,
float $maxLag = 30.0,
?float $heartbeat = null
): Subscription
public static Swerve::cache(): CacheInterface
public static Swerve::claim(string $name): Claim
public static Swerve::draining(): bool
public static Swerve::onShutdown(Closure $callback): void
public static Swerve::awaitShutdown(?float $timeout = null): bool
public static Swerve::ini(array $settings): void
public static Swerve::onWorkerStart(Closure $callback): void
public static Swerve::getVersion(): string
}Methods#
| Method | Description |
|---|---|
log | The log of this process: swerve's own PSR-3 logger, in its format (the time, this worker's slot). |
setLog | Replace the logger that Swerve::log returns. |
publish | Send a message to every subscriber of $topic, in every worker process of this swerve, this one included. |
publishRaw | Send $bytes unchanged to every subscriber of $topic, in every worker process of this swerve, this one included. |
subscribe | Receive what is published to $topic from now on. |
cache | The cache every worker shares, held by the master process. |
claim | A handle on the name $name, which one holder at a time can hold across the workers. |
draining | Whether this worker drains: it is shutting down, reloading or being recycled. |
onShutdown | Run $callback when this worker must now close its connections: for an upgraded connection, to say goodbye in its protocol and end it. |
awaitShutdown | Wait until this worker must close its connections, see Swerve::onShutdown. |
ini | php.ini settings a package needs in every swerve process. Call it from a file listed in your package's composer.json files: Composer loads it after swerve's own, as the master starts. swerve applies the settings with its startup restart, over its own settings; an explicit -d on the command line still wins. |
onWorkerStart | Run $callback in every worker as it starts, before the application loads, inside the worker's event loop, so it may start coroutines that run for the worker's life. Register it the way Swerve::ini says; calls after startup change nothing. A callback that throws stops the worker, as an application that fails to load does. |
getVersion | The installed version, as Composer knows it: 0.1.0, or dev-main in a checkout. |
Examples#
use Swerve\Swerve;
// Without swerve's master process (a script, a test), Swerve works within this process
phasync::run(function () {
$chat = Swerve::subscribe('chat'); // subscribed from here on
Swerve::publish('chat', ['user' => 'ann', 'text' => 'hi']);
foreach ($chat as $message) {
echo "$message->user: $message->text\n";
break;
}
Swerve::cache()->set('visits', 1, 60);
echo 'visits: ', Swerve::cache()->get('visits'), "\n";
});Output
ann: hi visits: 1
use Swerve\Swerve;
Swerve::log()->info('started');
Swerve::cache()->set('hits', 1, 60);
Swerve::publish('chat', ['user' => 'ann', 'text' => 'hi']);See also#
CacheThe cache every worker shares, from Swerve::cache(): a PSR-16 cache held by the master process, the least recently used entries evicted past --cache-size.ClaimA handle on a name that one holder at a time can hold across the whole server, fromSwerve::claim.SubscriptionThe messages published to a topic, fromSwerve::subscribe: iterate over it to receive them.