maxwellhealth/taskr
Composer 安装命令:
composer require maxwellhealth/taskr
包简介
taskr is a small PHP library that makes it easy to fork callbacks into child processes.
README 文档
README
taskr is a small PHP library that makes it easy to fork callbacks into child processes.
Compatibility
PHP 5.4, 5.5, 5.6, and 7
Quick Start
Before running a task, we'll need to define a handler. The handler is what the task will use to do things like writing a log or setting data about the task. In the example below, we've implemented HandlerInterface by extending HandlerAbstract with our own handler that will use a flat file for logging and MongoDB for task data.
<?php use Taskr\HandlerAbstract; use Taskr\Task; class MyHandler extends HandlerAbstract { private $collection; private $logPath; public function __construct(\MongoCollection $collection) { $this->collection = $collection; $this->logPath = '/tmp/'; } public function writeLog($message) { file_put_contents($this->logPath . $this->getTaskId(true) . '.log', $message . "\n", FILE_APPEND); } public function readLog($offset = 0) { $handle = fopen($this->logPath . $this->getTaskId(true) . '.log', 'r'); $contents = ''; fseek($handle, $offset); while (!feof($handle)) { $contents .= fgets($handle, 4096); } return $contents; } public function set($key, $value) { $this->collection->update([ 'taskId' => $this->getTaskId(true), ], [ '$set' => ['taskId' => $this->getTaskId(true), $key => $value], ], ['upsert' => true]); } public function get($key = null, $default = null) { $fields = []; if (!is_null($key)) { $fields[$key] = true; } $doc = $this->collection->findOne(['taskId' => $this->getTaskId(true)], $fields); if (!$doc) { return $default; } if (is_null($key)) { return $doc; } if (!array_key_exists($key, $doc)) { return $default; } return $doc[$key]; } public function setValues(array $values) { $values['taskId'] = $this->getTaskId(true); $this->collection->update([ 'taskId' => $this->getTaskId(true), ], [ '$set' => $values, ], ['upsert' => true]); } }
In the same file, let's create a new instance of our handler which we'll pass as the first argument to a new instance of Task. The second argument of our task object is a callable type which will be called by Task::run().
$mongo = new \MongoClient(); $db = $mongo->taskr; $collection = $db->tasks; $handler = new MyHandler($collection); $task = new Task($handler, function(MyHandler $handler) { $handler->writeLog('The task has started'); sleep(2); $handler->complete(); }); $task->run();
Calling $task->run() will fork the current PHP process using pcntl_fork() and run the task callable in a separate child process. In this example, the original process will end and the child process will live on writing to a log, sleeping for 2 seconds, and finally ending.
maxwellhealth/taskr 适用场景与选型建议
maxwellhealth/taskr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.84k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 maxwellhealth/taskr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maxwellhealth/taskr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 28.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-21