[英]PHP Eval alternative to include a file
我當前正在使用beantalk +主管+ PHP運行隊列系統。
我希望我的工作人員在有新版本可用時自動死亡(基本上是代碼更新)。
我當前的代碼如下
class Job1Controller extends Controller
{
public $currentVersion = 5;
public function actionIndex()
{
while (true) {
// check if a new version of the worker is available
$file = '/config/params.php';
$paramsContent = file_get_contents($file);
$params = eval('?>' . file_get_contents($file));
if ($params['Job1Version'] != $this->currentVersion) {
echo "not the same version, exit worker \n";
sleep(2);
exit();
} else {
echo "same version, continue processing \n";
}
}
}
}
當我更新代碼時,params文件將更改為新版本號,這將迫使工作程序終止。 我無法使用include,因為文件將在while循環中加載到內存中。 知道文件params.php在安全性方面並不關鍵,我想知道是否還有另一種方法?
編輯:params.php看起來如下:
<?php
return [
'Job1Version' => 5
];
$params = require($file);
由於您的文件具有return
語句,因此將傳遞返回的值。
經過幾次測試,我終於設法找到了不再需要版本控制的解決方案。
$reflectionClass = new \ReflectionClass($this);
$lastUpdatedTimeOnStart = filemtime($reflectionClass->getFileName());
while (true) {
clearstatcache();
$reflectionClass = new \ReflectionClass($this);
$lastUpdatedTime = filemtime($reflectionClass->getFileName());
if ($lastUpdatedTime != $lastUpdatedTimeOnStart) {
// An update has been made, exit
} else {
// worker hasn't been modified since running
}
}
每當文件被更新時,工作人員都會自動退出,這要感謝@Rudie向我指出了正確的方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.