Back to Hyperf

Watcher (Hot Reload)

docs/en/watcher.md

3.1.683.2 KB
Original Source

Watcher (Hot Reload)

Since version 2.0 uses BetterReflection to collect the abstract syntax tree (AST) and reflection data, the scanning speed is much slower than version 1.1.

The first startup of application will be slower because there is no scan cache exists. Subsequent startup speeds will be improved, but because BetterReflection needs to be instantiated, the startup time is still relatively long.

In addition to solving the above startup problems, the Watcher component also handles restarting the application immediately after file modification.

This component is only suitable for development environment, please use it with caution in production environment.

Installation

bash
composer require hyperf/watcher --dev

Configuration

Publish configuration

bash
php bin/hyperf.php vendor:publish hyperf/watcher

Configuration instructions

NameDefaultDescription
driverScanFileDriverThe default polling file watcher
binPHP_BINARYThe script used to start the service, for example: php -d swoole.use_shortname=Off
watch.dirapp, configWatched directories
watch.file.envWached files
watch.interval2000Polling interval (ms)
ext.php, .envFile extension in the listening directory

Driver support

DriverNotes
Hyperf\Watcher\Driver\ScanFileDriverno extension required
Hyperf\Watcher\Driver\FswatchDriverrequires fswatch
Hyperf\Watcher\Driver\FindDriverrequires find, MAC requires gfind
Hyperf\Watcher\Driver\FindNewerDriverrequires find

fswatch Installation

Mac:

bash
brew install fswatch

Ubuntu/Debian

bash
apt-get install fswatch

Linux:

bash
wget https://github.com/emcrisostomo/fswatch/releases/download/1.14.0/fswatch-1.14.0.tar.gz \
&& tar -xf fswatch-1.14.0.tar.gz \
&& cd fswatch-1.14.0/ \
&& ./configure \
&& make \
&& make install

Startup

Because of the directory structure, the start command has to be run in the root directory of project.

bash
php bin/hyperf.php server:watch

Startup with docker

When configuring a file watcher for hot-reloading in Docker, specify the entry point in the Dockerfile as follows:

bash
ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "server:watch"]

Problems

  • For now, there is a slight problem in the Alpine Docker environment, which will be improved in the future version.
  • Deletion of files and modification of .env require a manual restart to take effect.