andre %!s(int64=4) %!d(string=hai) anos
pai
achega
e943926429
Modificáronse 3 ficheiros con 180 adicións e 2 borrados
  1. 48 0
      app/Cron/QueueLoop.php
  2. 130 0
      app/Cron/Snapshots.php
  3. 2 2
      moduleVersion.php

+ 48 - 0
app/Cron/QueueLoop.php

@@ -0,0 +1,48 @@
+<?php
+
+
+namespace ModulesGarden\ProxmoxAddon\App\Cron;
+
+
+use ModulesGarden\ProxmoxAddon\Core\CommandLine\CommandLoop;
+use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
+use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+class QueueLoop extends  CommandLoop
+{
+
+    /**
+     * Command name
+     * @var string
+     */
+    protected $name = 'queue-loop';
+
+    /**
+     * Command description
+     * @var string
+     */
+    protected $description = 'Run module queue loop';
+
+    /**
+     * Command help text
+     * @var string
+     */
+    protected $help = 'Just run that command to start all queued tasks!';
+
+    protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
+    {
+        $queue = new \ModulesGarden\ProxmoxAddon\Core\Queue\Queue();
+        $queue->setCallBefore(function (Job $job) use ($input,$io)
+        {
+            $io->writeln("Running {$job->job}");
+            (new Hypervisor($this->getName(), $input->getOptions()))
+                ->ping();
+        });
+        $queue->process();
+    }
+
+
+}

+ 130 - 0
app/Cron/Snapshots.php

@@ -0,0 +1,130 @@
+<?php
+
+
+namespace ModulesGarden\ProxmoxAddon\App\Cron;
+
+
+use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\SnapshotVmJob;
+use ModulesGarden\ProxmoxAddon\App\Models\Job;
+use ModulesGarden\ProxmoxAddon\App\Models\ProductConfiguration;
+use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
+use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
+use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
+use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
+use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
+use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
+
+class Snapshots extends Command
+{
+    use WhmcsParams;
+    use ProductService;
+    use ApiService;
+
+    /**
+     * Command name
+     * @var string
+     */
+    protected $name = 'snapshots';
+
+    /**
+     * Command description
+     * @var string
+     */
+    protected $description = 'Snapshots schedule';
+
+    /**
+     * Command help text
+     * @var string
+     */
+    protected $help = '';
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @param SymfonyStyle $io
+     * @throws \Exception
+     * @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()) )
+     * @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 ))
+     */
+    protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
+    {
+        $io->title('Snapshots schedule: Starting');
+        $productIds = ProductConfiguration::ofSetting('permissionSnapshotJob')
+            ->where("value", "not like", '\"\"')
+            ->pluck("product_id")
+            ->all();
+        if (empty($productIds))
+        {
+            $io->error("Scheduled Snapshot Jobs is not configured");
+            return;
+        }
+        $h        = (new Hosting())->getTable();
+        $sj =  (new SnapshotJob())->getTable();
+        $entities = SnapshotJob::select("{$sj}.*")
+            ->leftJoin($h, "{$h}.id", '=', "{$sj}.hosting_id")
+            ->whereRaw("{$sj}.period = 'daily' 
+                        AND TIMESTAMP({$sj}.start_time) < TIMESTAMP(NOW()) 
+                        AND TIMESTAMP({$sj}.updated_at) < TIMESTAMP({$sj}.start_time)
+                        OR ( {$sj}.period = 'daily' 
+                             AND {$sj}.start_time IS NULL 
+                             AND DATE({$sj}.updated_at) != DATE(NOW()) 
+                             )
+                        OR (
+                            {$sj}.period = 'hourly' 
+                            AND  TIMESTAMP(NOW()) >= TIMESTAMP( DATE_ADD({$sj}.updated_at, INTERVAL {$sj}.run_every HOUR ))
+                            )
+                        ")
+            ->where("{$h}.domainstatus", "Active")
+            ->whereIn("{$h}.packageid", $productIds);
+        $i        = 0;
+        /**
+         * DAYName(NOW())        Friday
+         * @var  SnapshotJob $entity
+         */
+        foreach ($entities->get() as $entity)
+        {
+
+            try
+            {
+                $now = new \DateTime();
+                $today = $now->format("l");
+                if($entity->period == "daily" && ( $entity->days && !in_array($today, $entity->days) ) ){
+                    continue;
+                }
+                $i++;
+                $output->writeln(sprintf("Synchronize snapshot job #%s (Hosting ID %s)",$entity->id, $entity->hosting_id));
+                //cloud
+                if($entity->vm_id){
+                    $jobClass = \ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\SnapshotVmJob::class;
+                }else{//vps
+                    $jobClass = SnapshotVmJob::class;
+                }
+                $query = Job::waiting()
+                             ->ofHostingId($entity->hosting_id)
+                            ->ofJob($jobClass);
+                if($entity->vm_id){
+                    $query->ofCustomId($entity->vm_id);
+                }
+                if(!$query->count()){
+                    $job=["snapshotJobId"=> $entity->id];
+                    queue($jobClass, $job, null, "hosting", $entity->hosting_id, $entity->vm_id);
+                    $entity->update(['updated_at' => date("Y-m-d H:i:s")]);
+                    $output->writeln(sprintf("Snapshot job #%s has been synchronized", $entity->id));
+                }
+            }catch (\Exception $ex)
+            {
+                $io->error( $ex->getMessage());
+            }
+        }
+        $output->writeln("");
+        $io->success([
+            sprintf("Synchronize snapshots schedule: %s Entries Processed.", $i),
+            "Snapshots schedule: Done"
+        ]);
+    }
+
+}

+ 2 - 2
moduleVersion.php

@@ -6,5 +6,5 @@
  */
 
 $moduleVersion = '3.4.0';
-$moduleRevision = '42e8546c5004f40ea05a32823d432489281fd980';
-$moduleWikiUrl = 'http://www.docs.modulesgarden.com/Proxmox_Cloud_For_WHMCS';
+$moduleRevision = 'df118fd9f7a4c7adbd268e8cec4cff51cc9accd2';
+$moduleWikiUrl = 'http://www.docs.modulesgarden.com/Proxmox_VPS_For_WHMCS';