File Monitoring Component

Background:

Workerman runs in memory, which avoids repeatedly reading from the disk and repeatedly interpreting and compiling PHP, in order to achieve the highest performance. Therefore, after modifying business code, a manual reload or restart is required for the changes to take effect.

At the same time, Workerman provides a file monitoring service that automatically runs reload when it detects a file update, reloading the PHP files. Developers can include it in their projects to start along with the project.

Download Address for File Monitoring Service:

  1. No dependency version: https://github.com/walkor/workerman-filemonitor

  2. Dependency inotify version: https://github.com/walkor/workerman-filemonitor-inotify (requires installation of the inotify extension)

Differences Between the Two Versions:

The version from address 1 uses a method of polling the file modification time every second to determine if the file has been updated.

The version from address 2 utilizes the Linux kernel's inotify mechanism, where the system proactively notifies Workerman when a file is updated.

Generally, the first no-dependency version is sufficient for use.

Usage Instructions

Copy the Applications/FileMonitor directory to your project's Applications directory.

If your project does not have an Applications directory, you can copy the Applications/FileMonitor/start.php file to any location in your project, and require it in your startup script.

The monitoring component defaults to monitoring the Applications directory. If you need to change this, you can modify the $monitor_dir variable in Applications/FileMonitor/start.php. It is recommended that the value of $monitor_dir be an absolute path.

Note:

  • The reload feature is not supported on Windows systems, and this monitoring service cannot be used.
  • It only takes effect in debug mode; file monitoring will not execute under daemon mode (the reason why daemon mode is not supported is explained below).
  • Only files loaded after Worker::runAll is executed can be hot-reloaded, or in other words, only files loaded within onXXX callbacks can be hot-reloaded.

Why is daemon mode not supported?

Daemon mode is generally the mode used for online production environments. When deploying a version in a production environment, multiple files are usually released at once, and there may be dependencies between files. Since syncing multiple files to disk takes some time, there may be a moment when the files on disk are incomplete. If file updates are detected at this moment and reload is executed, there is a risk of fatal errors due to missing files.

Additionally, in a production environment, bugs may sometimes need to be debugged online. If code is directly edited and saved, it will take effect immediately, which could lead to syntax errors causing the online service to become unavailable. The correct approach should be to check the code for syntax errors using php -l yourfile.php after saving the code, before reloading the hot-updated code.

If a developer truly needs file monitoring and automatic updates enabled in daemon mode, they can modify the Applications/FileMonitor/start.php code by removing the condition in the Worker::$daemonize section.