ShutdownVmJob.php 2.5 KB

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