|
| 1 | +# Application Sidekicks |
| 2 | + |
| 3 | +Sidekicks are long-running PHP workers that run **outside the HTTP request cycle**. |
| 4 | +They observe their environment (Redis Sentinel, secret vaults, feature flag services, etc.) |
| 5 | +and publish configuration to HTTP workers in real time — without polling, TTLs, or redeployment. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +1. A sidekick runs its own event loop (subscribe to Redis, watch files, poll an API, etc.) |
| 10 | +2. It calls `frankenphp_sidekick_set_vars()` to publish key-value pairs |
| 11 | +3. HTTP workers call `frankenphp_sidekick_get_vars()` to read the latest snapshot |
| 12 | +4. The first `get_vars` call **blocks until the sidekick has published** — no startup race condition |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +```caddyfile |
| 17 | +example.com { |
| 18 | + php_server { |
| 19 | + sidekick_entrypoint /app/bin/console |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Each `php_server` block has its own isolated sidekick scope. |
| 25 | + |
| 26 | +## PHP API |
| 27 | + |
| 28 | +### `frankenphp_sidekick_get_vars(string|array $name, float $timeout = 30.0): array` |
| 29 | + |
| 30 | +Starts a sidekick (at-most-once) and returns its published variables. |
| 31 | + |
| 32 | +- First call blocks until the sidekick calls `set_vars()` or the timeout expires |
| 33 | +- Subsequent calls return the latest snapshot immediately |
| 34 | +- When `$name` is an array, all sidekicks start in parallel and vars are returned keyed by name: |
| 35 | + |
| 36 | +```php |
| 37 | +$redis = frankenphp_sidekick_get_vars('redis-watcher'); |
| 38 | +// ['MASTER_HOST' => '10.0.0.1', 'MASTER_PORT' => '6379'] |
| 39 | + |
| 40 | +$all = frankenphp_sidekick_get_vars(['redis-watcher', 'feature-flags']); |
| 41 | +// ['redis-watcher' => [...], 'feature-flags' => [...]] |
| 42 | +``` |
| 43 | + |
| 44 | +- `$name` is available as `$_SERVER['FRANKENPHP_SIDEKICK_NAME']` and `$_SERVER['argv'][1]` in the entrypoint script |
| 45 | +- Throws `RuntimeException` on timeout, missing entrypoint, or sidekick crash |
| 46 | +- Works in both worker and non-worker mode |
| 47 | + |
| 48 | +### `frankenphp_sidekick_set_vars(array $vars): void` |
| 49 | + |
| 50 | +Publishes a snapshot of string key-value pairs from inside a sidekick. |
| 51 | +Each call **replaces** the entire snapshot atomically. |
| 52 | + |
| 53 | +- Throws `RuntimeException` if not called from a sidekick context |
| 54 | +- Throws `ValueError` if keys or values are not strings |
| 55 | + |
| 56 | +### `frankenphp_sidekick_should_stop(): bool` |
| 57 | + |
| 58 | +Returns `true` when FrankenPHP is shutting down. |
| 59 | + |
| 60 | +- Throws `RuntimeException` if not called from a sidekick context |
| 61 | + |
| 62 | +## Example |
| 63 | + |
| 64 | +### Sidekick Entrypoint |
| 65 | + |
| 66 | +```php |
| 67 | +<?php |
| 68 | +// bin/console |
| 69 | + |
| 70 | +require __DIR__.'/../vendor/autoload.php'; |
| 71 | + |
| 72 | +$command = $_SERVER['FRANKENPHP_SIDEKICK_NAME'] ?? ''; |
| 73 | + |
| 74 | +match ($command) { |
| 75 | + 'redis-watcher' => runRedisWatcher(), |
| 76 | + default => throw new \RuntimeException("Unknown sidekick: $command"), |
| 77 | +}; |
| 78 | + |
| 79 | +function runRedisWatcher(): void |
| 80 | +{ |
| 81 | + frankenphp_sidekick_set_vars([ |
| 82 | + 'MASTER_HOST' => '10.0.0.1', |
| 83 | + 'MASTER_PORT' => '6379', |
| 84 | + ]); |
| 85 | + |
| 86 | + while (!frankenphp_sidekick_should_stop()) { |
| 87 | + $master = discoverRedisMaster(); |
| 88 | + frankenphp_sidekick_set_vars([ |
| 89 | + 'MASTER_HOST' => $master['host'], |
| 90 | + 'MASTER_PORT' => (string) $master['port'], |
| 91 | + ]); |
| 92 | + usleep(100_000); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### HTTP Worker |
| 98 | + |
| 99 | +```php |
| 100 | +<?php |
| 101 | +// public/index.php |
| 102 | + |
| 103 | +require __DIR__.'/../vendor/autoload.php'; |
| 104 | + |
| 105 | +$app = new App(); |
| 106 | +$app->boot(); |
| 107 | + |
| 108 | +while (frankenphp_handle_request(function () use ($app) { |
| 109 | + $redis = frankenphp_sidekick_get_vars('redis-watcher'); |
| 110 | + |
| 111 | + $app->handle($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER + $redis); |
| 112 | +})) { |
| 113 | + gc_collect_cycles(); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Graceful Degradation |
| 118 | + |
| 119 | +```php |
| 120 | +if (function_exists('frankenphp_sidekick_get_vars')) { |
| 121 | + $config = frankenphp_sidekick_get_vars('config-watcher'); |
| 122 | +} else { |
| 123 | + $config = ['MASTER_HOST' => getenv('REDIS_HOST') ?: '127.0.0.1']; |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Runtime Behavior |
| 128 | + |
| 129 | +- Sidekicks get their own dedicated thread from the reserved pool; they don't reduce HTTP capacity |
| 130 | +- Execution timeout is automatically disabled |
| 131 | +- Shebangs (`#!/usr/bin/env php`) are silently skipped |
| 132 | +- `SCRIPT_FILENAME` is set to the entrypoint's full path |
| 133 | +- `$_SERVER['FRANKENPHP_SIDEKICK_NAME']` and `$_SERVER['argv'][1]` contain the sidekick name |
| 134 | +- Crash recovery: automatic restart with exponential backoff |
| 135 | +- Graceful shutdown via `frankenphp_sidekick_should_stop()` |
| 136 | +- Use `error_log()` or `frankenphp_log()` for logging — avoid `echo` |
0 commit comments