Hot Code Reload

  • Since 2.3.0

To ease development against a running Swoole HTTP server, hot code reloading can be enabled.

With this feature enabled, a Swoole worker will monitor included PHP files using inotify, and will restart all workers if a file is changed, thus mitigating the need to manually restart the server to test changes.

This feature should only be used in your local development environment, and should not be used in production!

Requirements

  • ext-inotify

This library ships with an inotify based implementation of Zend\Expressive\Swoole\HotCodeReload\FileWatcherInterface. In order to use it, the inotify extension must be loaded.

Configuration

The following demonstrates all currently available configuration options:

// config/autoload/swoole.local.php

return [
    'zend-expressive-swoole' => [
        'hot-code-reload' => [
            // Set to true to enable hot code reload; the default is false.
            'enable' => true,

            // Time in milliseconds between checks to changes in files.
            'interval' => 500,
        ],
    ],
];

Limitations

Only files included by PHP after onWorkerStart will be reloaded. This means that Swoole will not reload any of the following:

  • New routes
  • New pipeline middleware
  • The Application instance, or any delegators used to modify it.
  • The Swoole HTTP server itself.

This limitation exists because the hot code reload features use the Swoole\Server::reload() method to notify Swoole to reload PHP files (see the Swoole reload() documentation for more details).

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!