MqExportArticle.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\Mq;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. class MqExportArticle extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. * php artisan mq:export.article
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'mq:export.article';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '导出文章';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $exchange = 'router';
  38. $queue = 'export_article';
  39. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  40. Log::debug('mq:export_article start.');
  41. Mq::worker($exchange, $queue, function ($message) {
  42. $data = [
  43. 'id' => $message->id,
  44. '--format' => $message->format,
  45. 'query_id' => $message->queryId,
  46. '--origin' => $message->origin,
  47. '--translation' => $message->translation,
  48. ];
  49. if (isset($message->token) && is_string($message->token)) {
  50. $data['--token'] = $message->token;
  51. }
  52. if (isset($message->anthology) && is_string($message->anthology)) {
  53. $data['--anthology'] = $message->anthology;
  54. }
  55. if (isset($message->channel) && is_string($message->channel)) {
  56. $data['--channel'] = $message->channel;
  57. }
  58. $ok = $this->call('export:article', $data);
  59. if ($ok !== 0) {
  60. Log::error('mq:export.article fail', $data);
  61. } else {
  62. $this->info('Received article id='.$message->id.' result='.$ok);
  63. Log::debug('mq:export.article done ', $data);
  64. return $ok;
  65. }
  66. });
  67. return 0;
  68. }
  69. }