StartTaskAction.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 启动任务Action
  9. *
  10. * 用于启动待执行的清理任务
  11. */
  12. class StartTaskAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '启动任务';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $taskId = $this->getKey();
  25. // 调用服务启动任务
  26. $result = CleanupService::startTask($taskId);
  27. if (!$result['success']) {
  28. return $this->response()
  29. ->error('启动失败:' . $result['message']);
  30. }
  31. return $this->response()
  32. ->success('任务启动成功!')
  33. ->detail('任务已开始执行,请在任务列表中查看进度。')
  34. ->refresh();
  35. } catch (\Exception $e) {
  36. return $this->response()
  37. ->error('启动失败:' . $e->getMessage());
  38. }
  39. }
  40. /**
  41. * 确认对话框
  42. */
  43. public function confirm()
  44. {
  45. return [
  46. '确认启动任务?',
  47. '⚠️ 任务启动后将开始执行数据清理操作,请确保已做好备份!'
  48. ];
  49. }
  50. /**
  51. * 权限检查
  52. */
  53. public function allowed()
  54. {
  55. $row = $this->row;
  56. return $row->status == 1; // 只有待执行状态的任务可以启动
  57. }
  58. }