Task.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Sep 19, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Cron;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  23. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Symfony\Component\Console\Style\SymfonyStyle;
  27. /**
  28. * Description of Task
  29. *
  30. * @author Pawel Kopec <pawelk@modulesgardne.com>
  31. */
  32. class Task extends Command
  33. {
  34. use main\App\Services\BaseService;
  35. /**
  36. * Command name
  37. * @var string
  38. */
  39. protected $name = 'task';
  40. /**
  41. * Command description
  42. * @var string
  43. */
  44. protected $description = '';
  45. /**
  46. * Command help text
  47. * @var string
  48. */
  49. protected $help = '';
  50. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  51. {
  52. $io->title('Synchronize tasks: Starting');
  53. $i = 0;
  54. foreach (main\App\Models\TaskHistory::where('status', "0")->get() as $taskHistory)
  55. {
  56. try
  57. {
  58. /* @var $taskHistory main\App\Models\TaskHistory */
  59. $i++;
  60. if (!$taskHistory->hosting)
  61. {
  62. $taskHistory->delete();
  63. $io->warning(sprintf("Task: %s, Hosting %s does not exist.", $taskHistory->id, $taskHistory->hosting_id));
  64. continue;
  65. }
  66. $this->setServerId($taskHistory->hosting->server);
  67. if ($this->getServer()->id != $this->getServerId())
  68. {
  69. unset($this->server, $this->api);
  70. }
  71. $this->getApi()->setInstance();
  72. $node = new proxmox\models\Node($taskHistory->node);
  73. $proxmoxTask = $node->task($taskHistory->upid);
  74. if (!$proxmoxTask->getExitstatus())
  75. {
  76. $output->writeln(sprintf("Task: %s current in progress.", $taskHistory->id));
  77. continue;
  78. }
  79. $taskHistory->description = $proxmoxTask->getExitstatus();
  80. $taskHistory->start_time = $proxmoxTask->getStartDate();
  81. $taskHistory->status = $proxmoxTask->getExitstatus() == "OK" ? "1" : "2";
  82. $taskHistory->save();
  83. $output->writeln(sprintf("Task: %s has been synchronized.", $taskHistory->id));
  84. }
  85. catch (\Exception $ex)
  86. {
  87. if ($taskHistory)
  88. {
  89. $io->error("Task Id #{$taskHistory->id}, " . $ex->getMessage());
  90. }
  91. else
  92. {
  93. $io->error($ex->getMessage());
  94. }
  95. }
  96. (new Hypervisor($this->getName(), $input->getOptions()))
  97. ->ping();
  98. }
  99. $output->writeln("");
  100. $io->success([
  101. sprintf("Synchronize tasks: %s Entries Processed.", $i),
  102. "Synchronize tasks: Done"
  103. ]);
  104. }
  105. }