博文 > 【进阶】 workerman也有定时任务哦

【进阶】 workerman也有定时任务哦

Php 10002018-08-15 08:20:00

一、下载workerman
https://www.workerman.net/download
二、下载workerman/mysql
http://doc3.workerman.net/640201
1、定时函数为匿名函数(闭包)

  1. use WorkermanWorker;
  2. use WorkermanLibTimer;
  3. require_once './Workerman/Autoloader.php';
  4. $task = new Worker();
  5. // 开启多少个进程运行定时任务,注意多进程并发问题
  6. $task->count = 1;
  7. $task->onWorkerStart = function($task){
  8. // 每2.5秒执行一次
  9. $time_interval = 2.5;
  10. Timer::add($time_interval, function(){
  11. echo "task runn";
  12. });
  13. };
  14. // 运行worker
  15. Worker::runAll();

2、定时函数为普通函数

  1. require_once './Workerman/Autoloader.php';
  2. use WorkermanWorker;
  3. use WorkermanLibTimer;
  4. // 普通的函数
  5. function send_mail($to, $content)
  6. {
  7. echo "send mail ...n";
  8. }
  9. $task = new Worker();
  10. $task->onWorkerStart = function($task){
  11. $to = 'workerman@workerman.net';
  12. $content = 'hello workerman';
  13. // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次
  14. Timer::add(10, 'send_mail', array($to, $content), false);
  15. };
  16. // 运行worker
  17. Worker::runAll();

3、定时函数为类的方法

  1. require_once './Workerman/Autoloader.php';
  2. use WorkermanWorker;
  3. use WorkermanLibTimer;
  4. class Mail{
  5. // 注意,回调函数属性必须是public
  6. public function send($to, $content)
  7. {
  8. echo "send mail ...n";
  9. }
  10. }
  11. $task = new Worker();
  12. $task->onWorkerStart = function($task){
  13. // 10秒后发送一次邮件
  14. $mail = new Mail();
  15. $to = 'workerman@workerman.net';
  16. $content = 'hello workerman';
  17. Timer::add(10, array($mail, 'send'), array($to, $content), false);
  18. };
  19. // 运行worker
  20. Worker::runAll();
  21. 4、定时函数为类方法(类内部使用定时器)
  22. require_once './Workerman/Autoloader.php';
  23. use WorkermanWorker;
  24. use WorkermanLibTimer;
  25. class Mail{
  26. // 注意,回调函数属性必须是public
  27. public function send($to, $content){
  28. echo "send mail ...n";
  29. }
  30. public function sendLater($to, $content){
  31. // 回调的方法属于当前的类,则回调数组第一个元素为$this
  32. Timer::add(10, array($this, 'send'), array($to, $content), false);
  33. }
  34. }
  35. $task = new Worker();
  36. $task->onWorkerStart = function($task){
  37. // 10秒后发送一次邮件
  38. $mail = new Mail();
  39. $to = 'workerman@workerman.net';
  40. $content = 'hello workerman';
  41. $mail->sendLater($to, $content);
  42. };
  43. // 运行worker
  44. Worker::runAll();

5、定时函数为类的静态方法

  1. require_once './Workerman/Autoloader.php';
  2. use WorkermanWorker;
  3. use WorkermanLibTimer;
  4. class Mail{
  5. // 注意这个是静态方法,回调函数属性也必须是public
  6. public static function send($to, $content){
  7. echo "send mail ...n";
  8. }
  9. }
  10. $task = new Worker();
  11. $task->onWorkerStart = function($task){
  12. // 10秒后发送一次邮件
  13. $to = 'workerman@workerman.net';
  14. $content = 'hello workerman';
  15. // 定时调用类的静态方法
  16. Timer::add(10, array('Mail', 'send'), array($to, $content), false);
  17. };
  18. // 运行worker
  19. Worker::runAll();

6、定时函数为类的静态方法(带命名空间)

  1. namespace Task;
  2. require_once './Workerman/Autoloader.php';
  3. use WorkermanWorker;
  4. use WorkermanLibTimer;
  5. class Mail{
  6. // 注意这个是静态方法,回调函数属性也必须是public
  7. public static function send($to, $content){
  8. echo "send mail ...n";
  9. }
  10. }
  11. $task = new Worker();
  12. $task->onWorkerStart = function($task){
  13. // 10秒后发送一次邮件
  14. $to = 'workerman@workerman.net';
  15. $content = 'hello workerman';
  16. // 定时调用带命名空间的类的静态方法
  17. Timer::add(10, array('TaskMail', 'send'), array($to, $content), false);
  18. };
  19. // 运行worker
  20. Worker::runAll();

7、定时器中销毁当前定时器(use闭包方式传递$timer_id)

  1. use WorkermanWorker;
  2. use WorkermanLibTimer;
  3. require_once './Workerman/Autoloader.php';
  4. $task = new Worker();
  5. $task->onWorkerStart = function($task)
  6. {
  7. // 计数
  8. $count = 1;
  9. // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
  10. $timer_id = Timer::add(1, function()use(&$timer_id, &$count){
  11. echo "Timer run $countn";
  12. // 运行10次后销毁当前定时器
  13. if($count++ >= 10){
  14. echo "Timer::del($timer_id)n";
  15. Timer::del($timer_id);
  16. }
  17. });
  18. };
  19. // 运行worker
  20. Worker::runAll();

8、定时器中销毁当前定时器(参数方式传递$timer_id)

  1. require_once './Workerman/Autoloader.php';
  2. use WorkermanWorker;
  3. use WorkermanLibTimer;
  4. class Mail{
  5. public function send($to, $content, $timer_id)
  6. {
  7. // 临时给当前对象添加一个count属性,记录定时器运行次数
  8. $this->count = empty($this->count) ? 1 : $this->count;
  9. // 运行10次后销毁当前定时器
  10. echo "send mail {$this->count}...n";
  11. if($this->count++ >= 10){
  12. echo "Timer::del($timer_id)n";
  13. Timer::del($timer_id);
  14. }
  15. }
  16. }
  17. $task = new Worker();
  18. $task->onWorkerStart = function($task){
  19. $mail = new Mail();
  20. // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 &
  21. $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id));
  22. };
  23. // 运行worker
  24. Worker::runAll();

9、只在指定进程中设置定时器

一个worker实例有4个进程,只在id编号为0的进程上设置定时器。

  1. use WorkermanWorker;
  2. use WorkermanLibTimer;
  3. require_once './Workerman/Autoloader.php';
  4. $worker = new Worker();
  5. $worker->count = 4;
  6. $worker->onWorkerStart = function($worker){
  7. // 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器
  8. if($worker->id === 0){
  9. Timer::add(1, function(){
  10. echo "4个worker进程,只在0号进程设置定时器n";
  11. });
  12. }
  13. };
  14. // 运行worker
  15. Worker::runAll();

来看一个完整的示例,shipments.php用来写定时任务

  1. onWorkerStart = function ($task) {
  2. global $db, $redis;
  3. $db = new WorkermanMySQLConnection('127.0.0.1', '3306', 'root', 'root', 'test');
  4. $redis = new Redis();
  5. $redis->connect('127.0.0.1', 6379);
  6. $redis->auth("qqq123123.");
  7. $time_interval = 0.1;
  8. Timer::add($time_interval, function () {
  9. global $db, $redis;
  10. $insert['name'] = 123;
  11. $db->insert('shipments')->cols($insert)->query();
  12. // sleep(100);
  13. });
  14. };
  15. function curlGet($url = '', $options = []){
  16. $ch = curl_init($url);
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  19. if (!empty($options)) {
  20. curl_setopt_array($ch, $options);
  21. }
  22. //https请求 不验证证书和host
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  24. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  25. $data = curl_exec($ch);
  26. curl_close($ch);
  27. return $data;
  28. }
  29. function newGetOrderInfo($taobao, $orderId){
  30. $taobao = urlencode($taobao);
  31. $url = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$taobao&tid=$orderId";
  32. $json = curlGet($url);
  33. return json_decode($json, true)['trade'];
  34. }
  35. Worker::runAll();