Task.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\process;
  8. use plugin\saiadmin\app\logic\tool\CrontabLogic;
  9. use Webman\Push\Api;
  10. use Workerman\Crontab\Crontab;
  11. class Task
  12. {
  13. public function onWorkerStart()
  14. {
  15. $this->initStart();
  16. }
  17. public function initStart()
  18. {
  19. $logic = new CrontabLogic();
  20. $taskList = $logic->where('status', 1)->select();
  21. foreach ($taskList as $item) {
  22. new Crontab($item->rule, function () use ($logic, $item) {
  23. $logic->run($item->id);
  24. });
  25. }
  26. }
  27. public function reload()
  28. {
  29. echo "重启Crontab\n";
  30. $list = Crontab::getAll();
  31. foreach ($list as $item) {
  32. Crontab::remove($item->getId());
  33. }
  34. $this->initStart();
  35. }
  36. public function run($args)
  37. {
  38. echo '任务调用:'.date('Y-m-d H:i:s')."\n";
  39. var_dump('参数:'. $args);
  40. $api = new Api(
  41. 'http://127.0.0.1:3232',
  42. config('plugin.webman.push.app.app_key'),
  43. config('plugin.webman.push.app.app_secret')
  44. );
  45. // 给订阅 saiadmin 的所有客户端推送 message 事件的消息
  46. $return_ret = [
  47. 'event' => 'ev_new_message',
  48. 'message' => '新消息通知',
  49. 'data' => [
  50. [
  51. 'id' => 1,
  52. 'title' => '系统消息',
  53. 'content' => '欢迎使用saiadmin框架',
  54. 'create_time' => date('Y-m-d H:i:s'),
  55. 'send_user' => [
  56. 'nickname' => '系统管理员',
  57. 'avatar' => ''
  58. ]
  59. ]
  60. ]
  61. ];
  62. $api->trigger('saiadmin', 'message', $return_ret);
  63. }
  64. }