TestWorkerStartProject.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. class TestWorkerStartProject extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. * php artisan test:worker.start.project 0c3d2f69-1098-428b-95db-f1183667c799
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'test:worker.start.project {project} {--token=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command 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. $appUrl = config('app.url');
  38. $projectId = $this->argument('project');
  39. $token = $this->option('token');
  40. // 如果 role 选项未提供(为空),提示用户输入
  41. if (empty($token)) {
  42. $token = $this->ask('Please enter the user token:');
  43. }
  44. $status = $this->choice(
  45. 'Which framework do you prefer?',
  46. ['published', 'restarted', 'stop'],
  47. 0 // 默认选择 Laravel(索引 0)
  48. );
  49. $response = Http::withToken($token)
  50. ->get($appUrl."/api/v2/task?view=project&project_id={$projectId}&status=all&order=order&dir=asc");
  51. if ($response->failed()) {
  52. $this->error('task read fail'.$response->json('message'));
  53. Log::error('task read fail', ['data' => $response->body()]);
  54. return 1;
  55. }
  56. $tasks = $response->json()['data']['rows'];
  57. foreach ($tasks as $key => $task) {
  58. $this->info("[{$key}]task ".$task['title'].' status '.$task['status']);
  59. $response = Http::withToken($token)
  60. ->patch($appUrl.'/api/v2/task-status/'.$task['id'], ['status' => $status]);
  61. if ($response->failed()) {
  62. $this->error('task status fail'.$response->json('message'));
  63. Log::error('task status fail', ['data' => $response->body()]);
  64. }
  65. $this->info("[{$key}]task status changed {$status}");
  66. }
  67. return 0;
  68. }
  69. }