/**
  * Loads the configuration file and initializes the scheduler accordingly.
  * Inits all workers
  * @param string $configFileName
  */
 private function loadConfig($configFileName = null)
 {
     $firstLoad = is_null($this->schedulerConfig);
     if ($firstLoad) {
         $this->schedulerConfig = new KSchedulerConfig($configFileName);
         date_default_timezone_set($this->schedulerConfig->getTimezone());
         $pid = $this->schedulerConfig->getLogDir() . '/batch.pid';
         if (file_exists($pid)) {
             KalturaLog::err("Scheduler already running - pid[" . file_get_contents($pid) . "]");
             exit(1);
         }
         file_put_contents($pid, getmypid());
         KalturaLog::info(file_get_contents('VERSION.txt'));
         $this->loadRunningTasks();
     } else {
         if (!$this->schedulerConfig->reloadRequired()) {
             return;
         }
         sleep(2);
         // make sure the file finsied to be written
         $this->schedulerConfig->load();
     }
     KScheduleHelperManager::clearFilters();
     $this->queueSizes = array();
     KalturaLog::info("Loading configuration file at: " . date('Y-m-d H:i'));
     $configItems = $this->createConfigItem($this->schedulerConfig->toArray());
     $taskConfigs = $this->schedulerConfig->getTaskConfigList();
     $this->logDir = $this->schedulerConfig->getLogDir();
     $this->statusInterval = $this->schedulerConfig->getStatusInterval();
     $this->schedulerStatusInterval = $this->schedulerConfig->getSchedulerStatusInterval();
     KDwhClient::setEnabled($this->schedulerConfig->getDwhEnabled());
     KDwhClient::setFileName($this->schedulerConfig->getDwhPath());
     $this->logWorkerInterval = $this->schedulerConfig->getLogWorkerInterval();
     $taskConfigsValidations = array();
     foreach ($taskConfigs as $taskConfig) {
         /* @var $taskConfig KSchedularTaskConfig */
         if (is_null($taskConfig->type)) {
             // is the scheduler itself
             continue;
         }
         if (isset($taskConfigsValidations[$taskConfig->id])) {
             KalturaLog::err("Duplicated worker id [{$taskConfig->id}] in worker names [{$taskConfig->name}] and [" . $taskConfigsValidations[$taskConfig->id] . "]");
             $this->keepRunning = false;
             return;
         }
         if (in_array($taskConfig->name, $taskConfigsValidations)) {
             KalturaLog::err("Duplicated worker name [{$taskConfig->name}] in worker ids [{$taskConfig->id}] and [" . array_search($taskConfig->name, $taskConfigsValidations) . "]");
             $this->keepRunning = false;
             return;
         }
         $taskConfigsValidations[$taskConfig->id] = $taskConfig->name;
         $subConfigItems = $this->createConfigItem($taskConfig->toArray(), $taskConfig->id, $taskConfig->name);
         $configItems = array_merge($configItems, $subConfigItems);
     }
     KalturaLog::info("sending configuration to the server");
     KScheduleHelperManager::saveConfigItems($configItems);
 }
Example #2
0
 /**
  * @param KSchedularTaskConfig $taskConfig
  */
 public function __construct($taskConfig = null)
 {
     /*
      *  argv[0] - the script name
      *  argv[1] - serialized KSchedulerConfig config
      */
     global $argv, $g_context;
     $this->sessionKey = uniqid('sess');
     $this->start = microtime(true);
     if (is_null($taskConfig)) {
         $data = gzuncompress(base64_decode($argv[1]));
         self::$taskConfig = unserialize($data);
     } else {
         self::$taskConfig = $taskConfig;
     }
     if (!self::$taskConfig) {
         die("Task config not supplied");
     }
     date_default_timezone_set(self::$taskConfig->getTimezone());
     // clear seperator between executions
     KalturaLog::debug('___________________________________________________________________________________');
     KalturaLog::stderr('___________________________________________________________________________________', KalturaLog::DEBUG);
     KalturaLog::info(file_get_contents(dirname(__FILE__) . "/../VERSION.txt"));
     if (!self::$taskConfig instanceof KSchedularTaskConfig) {
         KalturaLog::err('config is not a KSchedularTaskConfig');
         die;
     }
     KalturaLog::debug("set_time_limit({" . self::$taskConfig->maximumExecutionTime . "})");
     set_time_limit(self::$taskConfig->maximumExecutionTime);
     KalturaLog::info('Batch index [' . $this->getIndex() . '] session key [' . $this->sessionKey . ']');
     self::$kClientConfig = new KalturaConfiguration();
     self::$kClientConfig->setLogger($this);
     self::$kClientConfig->serviceUrl = self::$taskConfig->getServiceUrl();
     self::$kClientConfig->curlTimeout = self::$taskConfig->getCurlTimeout();
     if (isset(self::$taskConfig->clientConfig)) {
         foreach (self::$taskConfig->clientConfig as $attr => $value) {
             self::$kClientConfig->{$attr} = $value;
         }
     }
     self::$kClient = new KalturaClient(self::$kClientConfig);
     self::$kClient->setPartnerId(self::$taskConfig->getPartnerId());
     self::$clientTag = 'batch: ' . self::$taskConfig->getSchedulerName() . ' ' . get_class($this) . " index: {$this->getIndex()} sessionId: " . UniqueId::get();
     self::$kClient->setClientTag(self::$clientTag);
     //$ks = self::$kClient->session->start($secret, "user-2", KalturaSessionType::ADMIN);
     $ks = $this->createKS();
     self::$kClient->setKs($ks);
     KDwhClient::setEnabled(self::$taskConfig->getDwhEnabled());
     KDwhClient::setFileName(self::$taskConfig->getDwhPath());
     $this->onBatchUp();
     KScheduleHelperManager::saveRunningBatch($this->getName(), $this->getIndex());
 }
 private function loadConfig()
 {
     $firstLoad = true;
     if (!is_null($this->schedulerConfig)) {
         $firstLoad = false;
         // check if the helper updated the config file
         clearstatcache();
         $current_file_time = filemtime($this->configFileName);
         if ($current_file_time <= $this->schedulerConfig->getFileTimestamp()) {
             return;
         }
         sleep(2);
         // make sure the file finsied to be written
     }
     $this->schedulerConfig = new KSchedulerConfig($this->configFileName);
     date_default_timezone_set($this->schedulerConfig->getTimezone());
     $this->cleanQueueFiltersDir();
     $this->queueSizes = array();
     if ($firstLoad) {
         KalturaLog::info(file_get_contents('VERSION.txt'));
     }
     KalturaLog::info("Loading configuration file at: " . date('Y-m-d H:i'));
     $configItems = $this->createConfigItem($this->schedulerConfig->getScheduler()->toArray());
     $taskConfigs = $this->schedulerConfig->getTaskConfigList();
     $this->logDir = $this->schedulerConfig->getLogDir();
     $this->maxExecutionTime = $this->schedulerConfig->getMaxExecutionTime();
     $this->statusInterval = $this->schedulerConfig->getStatusInterval();
     $this->schedulerStatusInterval = $this->schedulerConfig->getSchedulerStatusInterval();
     KDwhClient::setEnabled($this->schedulerConfig->getDwhEnabled());
     KDwhClient::setFileName($this->schedulerConfig->getDwhPath());
     $taskConfigsValidations = array();
     foreach ($taskConfigs as $taskConfig) {
         /* @var $taskConfig KSchedularTaskConfig */
         if (is_null($taskConfig->type)) {
             // is the scheduler itself
             continue;
         }
         if (isset($taskConfigsValidations[$taskConfig->id])) {
             KalturaLog::err("Duplicated worker id [{$taskConfig->id}] in worker names [{$taskConfig->name}] and [" . $taskConfigsValidations[$taskConfig->id] . "]");
             $this->keepRunning = false;
             return;
         }
         if (in_array($taskConfig->name, $taskConfigsValidations)) {
             KalturaLog::err("Duplicated worker name [{$taskConfig->name}] in worker ids [{$taskConfig->id}] and [" . array_search($taskConfig->name, $taskConfigsValidations) . "]");
             $this->keepRunning = false;
             return;
         }
         $taskConfigsValidations[$taskConfig->id] = $taskConfig->name;
         $vars = get_object_vars($taskConfig);
         $subConfigItems = $this->createConfigItem($vars, $taskConfig->id, $taskConfig->name);
         $configItems = array_merge($configItems, $subConfigItems);
     }
     KalturaLog::info("sending configuration to the server");
     KScheduleHelperManager::saveConfigItems($this->schedulerConfig->getConfigItemsFilePath(), $configItems);
     set_time_limit($this->maxExecutionTime);
     $this->initAllWorkers();
 }
Example #4
0
 public function __construct(KBatchKillerConfig $config)
 {
     $this->config = $config;
     KDwhClient::setEnabled($config->dwhEnabled);
     KDwhClient::setFileName($config->dwhPath);
 }
 /**
  * @param KSchedularTaskConfig $taskConfig
  */
 public function __construct($taskConfig = null)
 {
     /*
      *  argv[0] - the script name
      *  argv[1] - serialized KSchedulerConfig config
      */
     global $argv, $g_context;
     $this->sessionKey = uniqid('sess');
     $this->start = microtime(true);
     if (is_null($taskConfig)) {
         $this->taskConfig = unserialize(base64_decode($argv[1]));
     } else {
         $this->taskConfig = $taskConfig;
     }
     if (!$this->taskConfig) {
         die("Task config not supplied");
     }
     date_default_timezone_set($this->taskConfig->getTimezone());
     // clear seperator between executions
     KalturaLog::debug('___________________________________________________________________________________');
     KalturaLog::info(file_get_contents(dirname(__FILE__) . "/../VERSION.txt"));
     if (!$this->taskConfig instanceof KSchedularTaskConfig) {
         KalturaLog::err('config is not a KSchedularTaskConfig');
         die;
     }
     KalturaLog::debug("set_time_limit({$this->taskConfig->maximumExecutionTime})");
     set_time_limit($this->taskConfig->maximumExecutionTime);
     KalturaLog::debug('This batch index: ' . $this->getIndex());
     KalturaLog::debug('This session key: ' . $this->sessionKey);
     $this->kClientConfig = new KalturaConfiguration($this->taskConfig->getPartnerId());
     $this->kClientConfig->setLogger($this);
     $this->kClientConfig->serviceUrl = $this->taskConfig->getServiceUrl();
     $this->kClientConfig->curlTimeout = $this->taskConfig->getCurlTimeout();
     $this->kClientConfig->clientTag = 'batch: ' . $this->taskConfig->getSchedulerName();
     $this->kClient = new KalturaClient($this->kClientConfig);
     //$ks = $this->kClient->session->start($secret, "user-2", KalturaSessionType::ADMIN);
     $ks = $this->createKS();
     $this->kClient->setKs($ks);
     KDwhClient::setEnabled($this->taskConfig->getDwhEnabled());
     KDwhClient::setFileName($this->taskConfig->getDwhPath());
     $this->onBatchUp();
     KScheduleHelperManager::saveRunningBatch($this->taskConfig->getCommandsDir(), $this->getName(), $this->getIndex());
 }