AudioJob.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Common\Library\Alltools;
  13. class AudioJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $msgid;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($msgid)
  23. {
  24. //
  25. $this->msgid = $msgid;
  26. Log::info(date('Y-m-d H:i:d').':'.$msgid);
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $result = $this->goaaa();
  36. /*if($result === false){
  37. //防止失败
  38. $key = 'kgelavarel_giftjob_'.$this->msgid;
  39. $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
  40. if($limit_rs === true){
  41. //echo 123;
  42. dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
  43. }else{
  44. //echo 345;
  45. }
  46. }*/
  47. }
  48. //主要调用
  49. public function goaaa(){
  50. //
  51. $data = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->where('plantask_status',0)->first();
  52. if(empty($data)){return true;}
  53. DB::beginTransaction();
  54. $Alltools = new Alltools;
  55. $result = $Alltools->shouyi($data->to_user_id,$data->money,'user_match_audio_log',$this->msgid);
  56. if($result !== true){
  57. DB::rollBack();
  58. return false;
  59. }
  60. //
  61. $update = [
  62. 'plantask_status' => 1,
  63. ];
  64. $rs_update = DB::table('mt_user_match_audio_log')->where('id',$this->msgid)->update($update);
  65. if(!$rs_update){
  66. Db::rollBack();
  67. return false;
  68. }
  69. DB::commit();
  70. return true;
  71. }
  72. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  73. {
  74. $app_name = Config::get('app.name');// laravel name
  75. $key = "{$app_name}:{$key}";
  76. $redis = Redis::connection();
  77. //
  78. //指定键值新增+1 并获取
  79. $count = $redis->incr($key);
  80. if ($count > $apiLimit) {
  81. return false;
  82. }
  83. //设置过期时间
  84. if ($count == 1) {
  85. $redis->pExpire($key, $apiLimitTime);
  86. }
  87. return true;
  88. }
  89. }