AsyncWorkCommand.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * 异步任务脚本
  4. * @author >
  5. */
  6. require_once(APP_PATH."/protected/vendor/autoload.php");
  7. use Qiniu\Auth;
  8. use Qiniu\Storage\BucketManager;
  9. class AsyncWorkCommand extends CConsoleCommand{
  10. public function run($args) {
  11. error_reporting(E_ALL);
  12. ini_set('memory_limit', '512M');
  13. ignore_user_abort(true);
  14. set_time_limit(0);
  15. $time_H = time();
  16. while (true) {
  17. sleep(3);
  18. $time = time();
  19. if($time - $time_H > 3600){
  20. exit();
  21. }
  22. //用户注册后异步任务
  23. $list = new ARedisList('after_user_reg');
  24. $auth = new Auth(Yii::app()->params['qiniuConfig']['ak'],Yii::app()->params['qiniuConfig']['sk']);
  25. $bmgr = new BucketManager($auth);
  26. while ($list->getCount() > 0) {
  27. $user_id = $list->pop();
  28. try {
  29. $userAr = RUser::get(new MongoId($user_id));
  30. //抓取头像到七牛
  31. $url = $userAr->avatar;
  32. if(strpos($url,'qiniu')===false){
  33. $bucket = Yii::app()->params['qiniuConfig']['avatars'];
  34. $key = dechex(time()).rand(10,99).'.jpg';
  35. list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
  36. echo "=====> fetch $url to bucket: $bucket key: $key\n";
  37. if ($err !== null) {
  38. file_put_contents(APP_PATH.'/download/log/after_user_reg.log',var_export($err,true),FILE_APPEND);
  39. } else {
  40. if(YII_DEBUG == true){
  41. $userAr->avatar = 'http://avatar.yiguanjia.club/'.$key;
  42. }else{
  43. $userAr->avatar = 'http://avatar.yiguanjia.club/'.$key;
  44. }
  45. }
  46. }
  47. $userAr->update(array('avatar'),true);
  48. } catch (Exception $e) {
  49. var_dump($e);
  50. continue;
  51. }
  52. }
  53. //订单支付成功后异步任务
  54. $list = new ARedisList('o2o_after_pay_success');
  55. while ($list->getCount() > 0) {
  56. $res = $list->pop();
  57. $input_data = json_decode($res,true);
  58. try {
  59. $order = ROrder::get(new MongoId($input_data['order_no']));
  60. CommonFn::sendOrderSms($order,$input_data['id']);
  61. } catch (Exception $e) {
  62. continue;
  63. }
  64. }
  65. //发送短信异步任务
  66. $list = new ARedisList('send_sms_list');
  67. $smsservice = Service::factory('SendSMSService');
  68. while ($list->getCount() > 0) {
  69. $key = $list->pop();
  70. try {
  71. $sms_data = json_decode($key,true);
  72. $smsservice->send_sms($sms_data['tpl'],$sms_data['mobile']);
  73. } catch (Exception $e) {
  74. continue;
  75. }
  76. }
  77. }
  78. }
  79. }