xcaleigh / deamons
一个使用反射避免修改库中代码,以处理守护进程且无死锁的PHP库
dev-master
2022-11-30 16:18 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-29 06:14:35 UTC
README
一个使用反射避免修改库中代码,以处理守护进程且无死锁的PHP库
Composer和数据库
composer require xcalegroup/deamons
创建以下表
CREATE TABLE `deamon_handler` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`unik_id` varchar(200) NOT NULL,
`stop` tinyint(4) NOT NULL DEFAULT 0,
`timestamp` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `deamon_jobs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`deamon_id` int(11) NOT NULL,
`job_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`class` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`data` text COLLATE utf8_unicode_ci NOT NULL,
`created` timestamp NULL DEFAULT current_timestamp(),
`updated` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1975 DEFAULT CHARSET=utf8
要求
您需要在项目中添加一个json配置文件
{
"version" : "1.0",
"max_deamons" : 15,
"database" : {
"DB_TYPE" : "mysql",
"DB_HOST" : "localhost",
"DB_USER" : "dbuser",
"DB_PASS" : "password",
"DB_NAME" : "dbname"
},
"deamons" : [
{
"class" : "cars",
"include" : "./examples/cars.php"
}
]
}
如何启动守护进程
Your start the Deamon as a cron job.
Set the config file as a parameter to the file.
Your stop the Deamon as a cron job or call to the stop_deamons.php file.
Set the config file as a parameter to the file.
Or you can run it for test in a browser using the ?config=deamons.json
You can use file paths in the url
在你的类中实现抽象函数**
require_once("./deamonJob.php");
class cars extends DeamonJob
{
/**
* Execute the code for the job with the data supplied in the request method
* @param $data contains the data stored for each job in the request method.
*/
public function execute($data)
{
echo "Do code with :";
print_r($data);
}
/**
* Return the data variables to use in execute method.
* Each array entry equals a new job. You decide how many jobs to start for each deamon.
* The content for each array entry can be anything you like. Values, JSON, arrays, objects etc.
* All data is being json_encoded
* Note: return null on no more data
*/
public function request()
{
return array(array("car" => "BMW", "model" => "330e"), array("car" => "Audi", "model" => "A6"));
}
}