Snapshots.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Cron;
  3. use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\SnapshotVmJob;
  4. use ModulesGarden\ProxmoxAddon\App\Models\Job;
  5. use ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration;
  6. use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
  7. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  8. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  9. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  10. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  11. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15. use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
  16. class Snapshots extends Command
  17. {
  18. use WhmcsParams;
  19. use ProductService;
  20. use ApiService;
  21. /**
  22. * Command name
  23. * @var string
  24. */
  25. protected $name = 'snapshots';
  26. /**
  27. * Command description
  28. * @var string
  29. */
  30. protected $description = 'Snapshots schedule';
  31. /**
  32. * Command help text
  33. * @var string
  34. */
  35. protected $help = '';
  36. /**
  37. * @param InputInterface $input
  38. * @param OutputInterface $output
  39. * @param SymfonyStyle $io
  40. * @throws \Exception
  41. * @query daily SELECT *, DATE(`updated_at`) as t FROM `ProxmoxAddon_SnapshotJob` where `period` = 'daily' and TIMESTAMP(`start_time`) < TIMESTAMP(NOW()) and TIMESTAMP(updated_at) < TIMESTAMP(`start_time`) or ( `period` = 'daily' AND `start_time` is NULL AND DATE(`updated_at`) != DATE(NOW()) )
  42. * @query hourly SELECT *, DATE(`updated_at`) as t FROM `ProxmoxAddon_SnapshotJob` where `period` = 'hourly' and TIMESTAMP(NOW()) >= TIMESTAMP( DATE_ADD(updated_at, INTERVAL `run_every` HOUR ))
  43. */
  44. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  45. {
  46. $io->title('Snapshots schedule: Starting');
  47. $productIds = ProductConfiguration::ofSetting('permissionSnapshotJob')
  48. ->where("value", "not like", '\"\"')
  49. ->pluck("product_id")
  50. ->all();
  51. if (empty($productIds))
  52. {
  53. $io->error("Scheduled Snapshot Jobs is not configured");
  54. return;
  55. }
  56. $h = (new Hosting())->getTable();
  57. $sj = (new SnapshotJob())->getTable();
  58. $entities = SnapshotJob::select("{$sj}.*")
  59. ->leftJoin($h, "{$h}.id", '=', "{$sj}.hosting_id")
  60. ->whereRaw("{$sj}.period = 'daily'
  61. AND TIMESTAMP({$sj}.start_time) < TIMESTAMP(NOW())
  62. AND TIMESTAMP({$sj}.updated_at) < TIMESTAMP({$sj}.start_time)
  63. OR ( {$sj}.period = 'daily'
  64. AND {$sj}.start_time IS NULL
  65. AND DATE({$sj}.updated_at) != DATE(NOW())
  66. )
  67. OR (
  68. {$sj}.period = 'hourly'
  69. AND TIMESTAMP(NOW()) >= TIMESTAMP( DATE_ADD({$sj}.updated_at, INTERVAL {$sj}.run_every HOUR ))
  70. )
  71. ")
  72. ->where("{$h}.domainstatus", "Active")
  73. ->whereIn("{$h}.packageid", $productIds);
  74. $i = 0;
  75. /**
  76. * DAYName(NOW()) Friday
  77. * @var SnapshotJob $entity
  78. */
  79. foreach ($entities->get() as $entity)
  80. {
  81. try
  82. {
  83. $now = new \DateTime();
  84. $today = $now->format("l");
  85. if($entity->period == "daily" && ( $entity->days && !in_array($today, $entity->days) ) ){
  86. continue;
  87. }
  88. $i++;
  89. $output->writeln(sprintf("Synchronize snapshot job #%s (Hosting ID %s)",$entity->id, $entity->hosting_id));
  90. if(!Job::waiting()->ofJob(SnapshotVmJob::class)->ofHostingId($entity->hosting_id)->count()){
  91. $job=["snapshotJobId"=> $entity->id];
  92. queue(SnapshotVmJob::class, $job, null, "hosting", $entity->hosting_id);
  93. $entity->update(['updated_at' => date("Y-m-d H:i:s")]);
  94. $output->writeln(sprintf("Snapshot job #%s has been synchronized", $entity->id));
  95. }
  96. }catch (\Exception $ex)
  97. {
  98. $io->error( $ex->getMessage());
  99. }
  100. }
  101. $output->writeln("");
  102. $io->success([
  103. sprintf("Synchronize snapshots schedule: %s Entries Processed.", $i),
  104. "Snapshots schedule: Done"
  105. ]);
  106. }
  107. }