class.jobs.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * 异步任务执行类
  4. */
  5. class Jobs
  6. {
  7. /**
  8. * 执行任务统一入口
  9. * @param string $name 任务名
  10. * @param array $args 参数列表
  11. */
  12. public function exec($name, $args)
  13. {
  14. if (!method_exists($this, $name)) {
  15. debug_log("method {$name} is not exits!", 'DEBUG', 'jobs');
  16. return false;
  17. }
  18. return call_user_func_array([$this, $name], $args);
  19. }
  20. /**
  21. * 测试
  22. */
  23. protected function test()
  24. {
  25. $args = func_get_args();
  26. debug_log('test(' . implode(',', $args) . ');', 'DEBUG', 'jobs');
  27. }
  28. /**
  29. * 抓百度页面
  30. */
  31. protected function curl_get($url)
  32. {
  33. if (empty($url)) {
  34. return;
  35. }
  36. $get = [];
  37. $opt = [];
  38. $error = '';
  39. $errno = 0;
  40. $code = 0;
  41. $resp = curl_get($url, $get, $opt, $error, $errno, $code);
  42. if ($errno || $code != 200) {
  43. debug_log("curl_get({$url}) failed: #{$errno},{$error},http-status={$code}", 'ERROR', 'jobs');
  44. } else {
  45. debug_log("curl_get({$url}) => {$resp}", 'DEBUG', 'jobs');
  46. }
  47. }
  48. /**
  49. * 打印当前进程的id
  50. */
  51. protected function pid()
  52. {
  53. debug_log('Current process\'s id is: ' . getmypid(), 'DEBUG', 'jobs');
  54. }
  55. }