2022-12-17 20:38:00 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Start file for windows
|
|
|
|
*/
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
use Dotenv\Dotenv;
|
2022-12-17 20:38:00 +08:00
|
|
|
use process\Monitor;
|
|
|
|
use support\App;
|
|
|
|
use Workerman\Worker;
|
|
|
|
|
|
|
|
ini_set('display_errors', 'on');
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
|
|
if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
|
|
|
|
if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
|
|
|
|
Dotenv::createUnsafeImmutable(base_path())->load();
|
|
|
|
} else {
|
|
|
|
Dotenv::createMutable(base_path())->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
App::loadAllConfig(['route']);
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
$errorReporting = config('app.error_reporting');
|
|
|
|
if (isset($errorReporting)) {
|
|
|
|
error_reporting($errorReporting);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
$runtimeProcessPath = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
|
|
|
|
if (!is_dir($runtimeProcessPath)) {
|
|
|
|
mkdir($runtimeProcessPath);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
$processFiles = [
|
2022-12-17 20:38:00 +08:00
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'start.php'
|
|
|
|
];
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach (config('process', []) as $processName => $config) {
|
|
|
|
$processFiles[] = write_process_file($runtimeProcessPath, $processName, '');
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (config('plugin', []) as $firm => $projects) {
|
|
|
|
foreach ($projects as $name => $project) {
|
|
|
|
if (!is_array($project)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach ($project['process'] ?? [] as $processName => $config) {
|
|
|
|
$processFiles[] = write_process_file($runtimeProcessPath, $processName, "$firm.$name");
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach ($projects['process'] ?? [] as $processName => $config) {
|
|
|
|
$processFiles[] = write_process_file($runtimeProcessPath, $processName, $firm);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
function write_process_file($runtimeProcessPath, $processName, $firm): string
|
2022-12-17 20:38:00 +08:00
|
|
|
{
|
2024-01-31 22:15:08 +08:00
|
|
|
$processParam = $firm ? "plugin.$firm.$processName" : $processName;
|
|
|
|
$configParam = $firm ? "config('plugin.$firm.process')['$processName']" : "config('process')['$processName']";
|
|
|
|
$fileContent = <<<EOF
|
2022-12-17 20:38:00 +08:00
|
|
|
<?php
|
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
use Workerman\Worker;
|
2024-01-31 22:15:08 +08:00
|
|
|
use Workerman\Connection\TcpConnection;
|
2022-12-17 20:38:00 +08:00
|
|
|
use Webman\Config;
|
|
|
|
use support\App;
|
|
|
|
|
|
|
|
ini_set('display_errors', 'on');
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
|
|
|
|
if (is_callable('opcache_reset')) {
|
|
|
|
opcache_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
App::loadAllConfig(['route']);
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
worker_start('$processParam', $configParam);
|
2022-12-17 20:38:00 +08:00
|
|
|
|
|
|
|
if (DIRECTORY_SEPARATOR != "/") {
|
|
|
|
Worker::\$logFile = config('server')['log_file'] ?? Worker::\$logFile;
|
2024-01-31 22:15:08 +08:00
|
|
|
TcpConnection::\$defaultMaxPackageSize = config('server')['max_package_size'] ?? 10*1024*1024;
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Worker::runAll();
|
|
|
|
|
|
|
|
EOF;
|
2024-01-31 22:15:08 +08:00
|
|
|
$processFile = $runtimeProcessPath . DIRECTORY_SEPARATOR . "start_$processParam.php";
|
|
|
|
file_put_contents($processFile, $fileContent);
|
|
|
|
return $processFile;
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
if ($monitorConfig = config('process.monitor.constructor')) {
|
|
|
|
$monitor = new Monitor(...array_values($monitorConfig));
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
function popen_processes($processFiles)
|
2022-12-17 20:38:00 +08:00
|
|
|
{
|
2024-01-31 22:15:08 +08:00
|
|
|
$cmd = '"' . PHP_BINARY . '" ' . implode(' ', $processFiles);
|
2022-12-17 20:38:00 +08:00
|
|
|
$descriptorspec = [STDIN, STDOUT, STDOUT];
|
2024-01-31 22:15:08 +08:00
|
|
|
$resource = proc_open($cmd, $descriptorspec, $pipes, null, null, ['bypass_shell' => true]);
|
2022-12-17 20:38:00 +08:00
|
|
|
if (!$resource) {
|
|
|
|
exit("Can not execute $cmd\r\n");
|
|
|
|
}
|
|
|
|
return $resource;
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
$resource = popen_processes($processFiles);
|
2022-12-17 20:38:00 +08:00
|
|
|
echo "\r\n";
|
|
|
|
while (1) {
|
|
|
|
sleep(1);
|
|
|
|
if (!empty($monitor) && $monitor->checkAllFilesChange()) {
|
|
|
|
$status = proc_get_status($resource);
|
|
|
|
$pid = $status['pid'];
|
|
|
|
shell_exec("taskkill /F /T /PID $pid");
|
|
|
|
proc_close($resource);
|
2024-01-31 22:15:08 +08:00
|
|
|
$resource = popen_processes($processFiles);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|