Snapshots.php 4.5 KB

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