PauseTaskAction.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 暂停任务Action
  9. *
  10. * 用于暂停正在执行的清理任务
  11. */
  12. class PauseTaskAction 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::pauseTask($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 in_array($row->status, [2, 3]); // 备份中或执行中的任务可以暂停
  57. }
  58. /**
  59. * 渲染按钮
  60. */
  61. public function render()
  62. {
  63. return <<<HTML
  64. <a href="javascript:void(0);" class="btn btn-warning btn-xs" data-action="{$this->getHandleRoute()}">
  65. <i class="fa fa-pause"></i> {$this->title}
  66. </a>
  67. HTML;
  68. }
  69. }