AppPackJob.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * app 打包
  4. */
  5. namespace App\Jobs;
  6. use App\Models\Mysql\PackModel;
  7. use GuzzleHttp\Client;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\Events\JobExceptionOccurred;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. class AppPackJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $params = [];
  18. /**
  19. * @param array $params
  20. */
  21. public function __construct(array $params = [])
  22. {
  23. $this->params = $params;
  24. }
  25. /**
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. // 延时3秒
  32. // sleep(3);
  33. // 超过3次
  34. logger('AppPackJob-handle-1', ['attempts' => $this->attempts(), 'params' => $this->params]);
  35. if ($this->attempts() >= 3) {
  36. $message = '重试次数超过3次!';
  37. $job = $this->job;
  38. $message .= ',链接名称' . $job->getConnectionName();
  39. $message .= ',所属队列名称' . $job->getQueue();
  40. $message .= ',所属类' . $job->getName();
  41. // 推送写日志
  42. logger($message, $this->params);
  43. // 删除脚本
  44. $this->delete();
  45. return;
  46. }
  47. $formData = [
  48. 'timeout' => 30,
  49. 'headers' => [
  50. 'X-TOKEN' => env('PACK_TOKEN', 'NrVcnuCUveQbr'),
  51. 'Content-Type' => 'application/json',
  52. 'Accept' => 'application/json',
  53. ],
  54. 'json' => [
  55. $this->params,
  56. ],
  57. ];
  58. $url = env('PACK_URL', 'http://dispatch_php.test.wg8.site') . '/api/channel/pack';
  59. $Client = new Client();
  60. try {
  61. $Client->post($url, $formData);
  62. } catch (\Exception $exception) {
  63. if (!PackModel::query()->where(
  64. [
  65. 'channel_code' => $this->params['channel_code'],
  66. 'mom_app_uid' => $this->params['mom_app_uid'],
  67. 'android_or_ios' => $this->params['android_or_ios'],
  68. 'mom_app_version' => $this->params['mom_app_version'],
  69. ]
  70. )->update(['status' => 2])) {
  71. logger('打包失败!-' . $exception->getMessage(), $this->params);
  72. }
  73. return;
  74. }
  75. }
  76. }