ShutdownVmJob.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Jobs\Vps\LoadBalancer;
  3. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\BaseJob;
  4. use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
  5. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  6. class ShutdownVmJob extends BaseJob
  7. {
  8. use ProductService;
  9. /**
  10. * @var HighAvailabilityClusterService
  11. */
  12. protected $highAvailabilityClusterService;
  13. public function handle($text = null)
  14. {
  15. $this->initParams();
  16. $this->initServices();
  17. $this->setHostingId($this->getWhmcsParamByKey("serviceid"));
  18. $this->highAvailabilityClusterService = new HighAvailabilityClusterService();
  19. //shutdown
  20. if (!$this->getModelData()['shutdown'] && $this->vm()->isRunning())
  21. {
  22. $this->highAvailabilityClusterService->delete();
  23. $taskId = $this->vm()->shutdown();
  24. //save task id
  25. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "shutdown" => true]);
  26. $this->log->info(sprintf("VM %s - Shutdown", $this->vm()->getVmid()));
  27. //sleep
  28. $this->sleep(20);
  29. return false;
  30. }
  31. else
  32. {
  33. if ($this->getModelData()['shutdown'])
  34. {
  35. //start
  36. if ($this->isDone() && !$this->vm()->isRunning())
  37. {
  38. return true;
  39. }
  40. else
  41. {
  42. if ($this->getTask()->getStatus() == "running")
  43. {
  44. //sleep
  45. $this->sleep(20);
  46. return false;
  47. }
  48. else
  49. {
  50. if ($this->getTask()->isFalied() && $this->configuration()->isLoadBalancerStopOnUpgrade())
  51. {
  52. $taskId = $this->vm()->stop();
  53. $this->putModelDataAndSave(["taskId" => $taskId, "node" => $this->vm()->getNode(), "shutdown" => true]);
  54. $this->log->info(sprintf("VM %s - Stop", $this->vm()->getVmid()));
  55. $this->sleep(20);
  56. return false;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return true;
  63. }
  64. }