RebootVmJob.php 1.7 KB

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