|
| 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, approximate TTLs, or redeployment. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +- A sidekick runs its own event loop (subscribe to Redis, watch files, poll an API, etc.) |
| 10 | +- It calls `frankenphp_sidekick_set_vars()` to publish a snapshot of key-value pairs |
| 11 | +- HTTP workers call `frankenphp_sidekick_get_vars()` to read the latest snapshot |
| 12 | +- The first call to get vars **blocks until the sidekick has published** its initial state to prevent race conditions on startup |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +Add a `sidekick_entrypoint` to your `php_server` block in the Caddyfile. |
| 17 | +This is the script that will be executed for every sidekick: |
| 18 | + |
| 19 | +```caddyfile |
| 20 | +example.com { |
| 21 | + php_server { |
| 22 | + sidekick_entrypoint /app/bin/console |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +## PHP API |
| 28 | + |
| 29 | +### `frankenphp_sidekick_get_vars(string|array $name, float $timeout = 30.0): array` |
| 30 | + |
| 31 | +Returns the latest variables published by sidekick(s). |
| 32 | +On the first call for a given name, this function: |
| 33 | + |
| 34 | +1. Starts the sidekick (at-most-once by name — safe with multiple HTTP workers) |
| 35 | +2. Blocks until the sidekick calls `frankenphp_sidekick_set_vars()` or the timeout expires |
| 36 | + |
| 37 | +Subsequent calls return the latest snapshot immediately. |
| 38 | + |
| 39 | +**When `$name` is a string**: returns the sidekick's vars as a flat associative array. |
| 40 | + |
| 41 | +**When `$name` is an array**: starts all sidekicks in parallel, waits for all to be ready, |
| 42 | +and returns vars keyed by sidekick name: |
| 43 | + |
| 44 | +```php |
| 45 | +$all = frankenphp_sidekick_get_vars(['redis-watcher', 'feature-flags']); |
| 46 | +// $all = [ |
| 47 | +// 'redis-watcher' => ['MASTER_HOST' => '10.0.0.1', ...], |
| 48 | +// 'feature-flags' => ['DARK_MODE' => '1', ...], |
| 49 | +// ] |
| 50 | +``` |
| 51 | + |
| 52 | +- **`$name`** identifies the sidekick(s) and is passed as `$_SERVER['argv'][1]` to the entrypoint script |
| 53 | +- **`$timeout`** in seconds (default: 30.0) — throws `RuntimeException` on timeout |
| 54 | +- Throws `RuntimeException` if no `sidekick_entrypoint` is configured, no thread is available, or a sidekick crashes before publishing |
| 55 | +- Throws `ValueError` if the array contains non-string or empty values |
| 56 | +- Works in both **worker mode** and **non-worker mode** (regular `php_server` without workers) |
| 57 | + |
| 58 | +### `frankenphp_sidekick_set_vars(array $vars): void` |
| 59 | + |
| 60 | +Publishes a snapshot of variables from inside a sidekick script. |
| 61 | +All keys and values must be strings. Each call **replaces** the entire snapshot atomically. |
| 62 | + |
| 63 | +- Can **only** be called from a sidekick context — throws `RuntimeException` otherwise |
| 64 | +- Throws `ValueError` if keys or values are not strings |
| 65 | + |
| 66 | +### `frankenphp_sidekick_should_stop(): bool` |
| 67 | + |
| 68 | +Returns `true` when FrankenPHP is shutting down. Sidekick scripts must poll this |
| 69 | +in their event loop to exit gracefully. |
| 70 | + |
| 71 | +- Can **only** be called from a sidekick context — throws `RuntimeException` otherwise |
| 72 | + |
| 73 | +```php |
| 74 | +while (!frankenphp_sidekick_should_stop()) { |
| 75 | + // do work |
| 76 | + usleep(100_000); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Example |
| 81 | + |
| 82 | +### Sidekick Entrypoint |
| 83 | + |
| 84 | +```php |
| 85 | +<?php |
| 86 | +// bin/console (or any sidekick entrypoint) |
| 87 | + |
| 88 | +require __DIR__.'/../vendor/autoload.php'; |
| 89 | + |
| 90 | +$command = $_SERVER['argv'][1] ?? ''; |
| 91 | + |
| 92 | +// Dispatch to the right sidekick based on the command name |
| 93 | +match ($command) { |
| 94 | + 'redis-watcher' => runRedisWatcher(), |
| 95 | + default => throw new \RuntimeException("Unknown sidekick: $command"), |
| 96 | +}; |
| 97 | + |
| 98 | +function runRedisWatcher(): void |
| 99 | +{ |
| 100 | + // Publish initial state — this unblocks any waiting get_vars() calls |
| 101 | + frankenphp_sidekick_set_vars([ |
| 102 | + 'REDIS_MASTER_HOST' => '10.0.0.1', |
| 103 | + 'REDIS_MASTER_PORT' => '6379', |
| 104 | + ]); |
| 105 | + |
| 106 | + while (!frankenphp_sidekick_should_stop()) { |
| 107 | + $master = discoverRedisMaster(); |
| 108 | + frankenphp_sidekick_set_vars([ |
| 109 | + 'REDIS_MASTER_HOST' => $master['host'], |
| 110 | + 'REDIS_MASTER_PORT' => (string) $master['port'], |
| 111 | + ]); |
| 112 | + usleep(100_000); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Reading from the HTTP Worker |
| 118 | + |
| 119 | +```php |
| 120 | +<?php |
| 121 | +// public/index.php |
| 122 | + |
| 123 | +require __DIR__.'/../vendor/autoload.php'; |
| 124 | + |
| 125 | +$app = new App(); |
| 126 | +$app->boot(); |
| 127 | + |
| 128 | +$handler = static function () use ($app) { |
| 129 | + // Single sidekick: returns its vars directly |
| 130 | + $redis = frankenphp_sidekick_get_vars('redis-watcher'); |
| 131 | + $host = $redis['REDIS_MASTER_HOST']; |
| 132 | + |
| 133 | + // Multiple sidekicks: starts all in parallel, returns vars keyed by name |
| 134 | + $all = frankenphp_sidekick_get_vars(['redis-watcher', 'feature-flags']); |
| 135 | + $host = $all['redis-watcher']['REDIS_MASTER_HOST']; |
| 136 | + $darkMode = $all['feature-flags']['DARK_MODE'] ?? '0'; |
| 137 | + |
| 138 | + echo $app->handle($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER); |
| 139 | +}; |
| 140 | + |
| 141 | +while (frankenphp_handle_request($handler)) { |
| 142 | + $app->terminate(); |
| 143 | + gc_collect_cycles(); |
| 144 | +} |
| 145 | + |
| 146 | +$app->shutdown(); |
| 147 | +``` |
| 148 | + |
| 149 | +### Graceful Degradation |
| 150 | + |
| 151 | +Applications can work with or without FrankenPHP: |
| 152 | + |
| 153 | +```php |
| 154 | +if (function_exists('frankenphp_sidekick_get_vars')) { |
| 155 | + $config = frankenphp_sidekick_get_vars('config-watcher'); |
| 156 | +} else { |
| 157 | + // Fallback: read from static config, environment, etc. |
| 158 | + $config = ['REDIS_MASTER_HOST' => getenv('REDIS_HOST') ?: '127.0.0.1']; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Runtime Behavior |
| 163 | + |
| 164 | +- **Execution timeout disabled**: sidekick threads automatically call `zend_unset_timeout()` |
| 165 | +- **Shebang support**: entrypoints with `#!/usr/bin/env php` aren't echoed |
| 166 | +- **Crash recovery**: if a sidekick script exits, FrankenPHP restarts it automatically (using the existing worker restart logic with exponential backoff) |
| 167 | +- **Graceful shutdown**: sidekicks detect shutdown via `frankenphp_sidekick_should_stop()` and exit their loop |
| 168 | +- **`SCRIPT_FILENAME`**: set to the entrypoint's full path, so `dirname(__DIR__)` works correctly |
| 169 | +- **`$_SERVER['argv']`**: follows CLI conventions — `argv[0]` is the script path, `argv[1]` is the sidekick name |
| 170 | + |
| 171 | +## Debugging |
| 172 | + |
| 173 | +- Use `error_log()` or `frankenphp_log()` for structured output — these go through FrankenPHP's logger |
| 174 | +- Avoid `echo` in sidekicks — it produces unstructured, unattributed log entries |
| 175 | +- Sidekick scripts can be tested standalone: `php bin/console redis-watcher` |
0 commit comments