<?php

namespace ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\Reinstall;

use ModulesGarden\ProxmoxAddon\App\Jobs\BaseJob;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class BackupVmJob extends BaseJob
{
    use ProductService;

    protected function initServices()
    {

    }

    public function handle($text = null)
    {
        $this->initParams();
        $this->initServices();
        $this->initVm();
        if ($this->isDone())
        {
            return true;
        }
        elseif ($this->isTaskRunning())
        {
            $this->sleep(20);
            return false;
        }
        $storage = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : 'local';
        $routing = $this->configuration()->isBackupRouting() == "Yes" ? "1" : "0";
        //backup
        $taskId = sl('Vm')->getVm()->backup($storage, $routing);
        //save task id
        $this->putModelDataAndSave(["taskId" => $taskId, "node" => sl('Vm')->getVm()->getNode()]);
        $this->sleep(20);
        return false;
    }


}