RebootVmJob.php 2.0 KB

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