<?php
namespace App\Console\Logic;
use App\Common\Common;
class test
{
private $fileLock = 'WarehouseCreateOrder.lock';//文件锁
public function __construct()
{
}
//执行前
public function before()
{
$dir = storage_path();
$flagFile = $dir . DIRECTORY_SEPARATOR . $this->fileLock;
if (file_exists($flagFile)) {
//如果锁文件存在时间过长删除锁文件
if (time() - filemtime($flagFile) > 7200) {
@unlink($flagFile);
}
}
//如果锁文件存在,程序已经运行.
if (file_exists($flagFile)) {
echo "Is already running,please unlock! \n";
exit(0);
}
//加锁,创建锁文件
touch($flagFile);
if (preg_match('/linux/i', PHP_OS) || preg_match('/Unix/i', PHP_OS)) {
chmod($flagFile, 0777);
}
}
public function end()
{
$dir = storage_path();
$flagFile = $dir . DIRECTORY_SEPARATOR . $this->fileLock;
//解锁,删除锁文件
unlink($flagFile);
}
//用户信息同步的入口
public function run()
{
try {
$this->before();
//codeing
} catch (\Exception $e) {
} catch (\Throwable $e) {
} finally{
$this->end();
}
}
}