Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Dec 8, 2020
1 parent 0304641 commit cb23e73
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
# file-watcher

## 热重启

例如在`Easyswoole`开发模式中,我们希望当有代码变动的时候,实现Server重启,示例代码如下:

```php
namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FileWatcher\FileWatcher;
use EasySwoole\FileWatcher\WatchRule;

class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
}

public static function mainServerCreate(EventRegister $register)
{
$watcher = new FileWatcher();
$rule = new WatchRule(EASYSWOOLE_ROOT."/App");
$watcher->addRule($rule);
$watcher->setOnChange(function (){
Logger::getInstance()->info('file change ,reload!!!');
ServerManager::getInstance()->getSwooleServer()->reload();
});
$watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
}
}
```
> 注意,reload仅仅针对Worker进程加载的代码有效
## 使用规则

### WatchRule
Expand Down Expand Up @@ -121,4 +86,39 @@ $fileWatcher->setOnChange(function (array $list, \EasySwoole\FileWatcher\WatchRu
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
/**@var \Swoole\Server $server **/
$fileWatcher->attachServer($server);
```
```

## EasySwoole中用于热重启

例如在`Easyswoole`开发模式中,我们希望当有代码变动的时候,实现Server重启,示例代码如下:

```php
namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FileWatcher\FileWatcher;
use EasySwoole\FileWatcher\WatchRule;

class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
}

public static function mainServerCreate(EventRegister $register)
{
$watcher = new FileWatcher();
$rule = new WatchRule(EASYSWOOLE_ROOT."/App");
$watcher->addRule($rule);
$watcher->setOnChange(function (){
Logger::getInstance()->info('file change ,reload!!!');
ServerManager::getInstance()->getSwooleServer()->reload();
});
$watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
}
}
```
> 注意,reload仅仅针对Worker进程加载的代码有效

0 comments on commit cb23e73

Please sign in to comment.