TestWorkerStartProject.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Http;
  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. * @var string
  12. */
  13. protected $signature = 'test:worker.start.project {project} {--token=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $appUrl = config('app.url');
  37. $projectId = $this->argument('project');
  38. $token = $this->option('token');
  39. // 如果 role 选项未提供(为空),提示用户输入
  40. if (empty($token)) {
  41. $token = $this->ask('Please enter the user token:');
  42. }
  43. $status = $this->choice(
  44. 'Which framework do you prefer?',
  45. ['published', 'restarted', 'stop'],
  46. 0 // 默认选择 Laravel(索引 0)
  47. );
  48. $response = Http::withToken($token)
  49. ->get($appUrl . "/api/v2/task?view=project&project_id={$projectId}&status=all&order=order&dir=asc");
  50. if ($response->failed()) {
  51. $this->error('task read fail' . $response->json('message'));
  52. Log::error('task read fail', ['data' => $response->body()]);
  53. return 1;
  54. }
  55. $tasks = $response->json()['data']['rows'];
  56. foreach ($tasks as $key => $task) {
  57. $this->info("[{$key}]task " . $task['title'] . ' status ' . $task['status']);
  58. $response = Http::withToken($token)
  59. ->patch($appUrl . "/api/v2/task-status/" . $task['id'], ['status' => $status]);
  60. if ($response->failed()) {
  61. $this->error('task status fail' . $response->json('message'));
  62. Log::error('task status fail', ['data' => $response->body()]);
  63. }
  64. $this->info("[{$key}]task status changed {$status}");
  65. }
  66. return 0;
  67. }
  68. }