/**
  * 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);
 }
 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();
 }