_runModelPath = $runModelPath; $this->_configValueFactory = $configValueFactory; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * Cron settings after save * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function afterSave() { $enabled = $this->getData(self::XML_PATH_BACKUP_ENABLED); $time = $this->getData(self::XML_PATH_BACKUP_TIME); $frequency = $this->getData(self::XML_PATH_BACKUP_FREQUENCY); $frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY; $frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY; if ($enabled) { $cronExprArray = [ (int) $time[1], # Minute (int) $time[0], # Hour $frequency == $frequencyMonthly ? '1' : '*', # Day of the Month '*', # Month of the Year $frequency == $frequencyWeekly ? '1' : '*', # Day of the Week ]; $cronExprString = join(' ', $cronExprArray); } else { $cronExprString = ''; } try { $this->_configValueFactory->create()->load( self::CRON_STRING_PATH, 'path' )->setValue( $cronExprString )->setPath( self::CRON_STRING_PATH )->save(); $this->_configValueFactory->create()->load( self::CRON_MODEL_PATH, 'path' )->setValue( $this->_runModelPath )->setPath( self::CRON_MODEL_PATH )->save(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t save the Cron expression.')); } return parent::afterSave(); } }