Esempio n. 1
0
 /**
  * @param int $taskIndex
  * @param string $logDir
  * @param string $phpPath
  * @param string $tasksetPath
  * @param KSchedularTaskConfig $taskConfig
  */
 public function __construct($taskIndex, $logDir, $phpPath, $tasksetPath, KSchedularTaskConfig $taskConfig)
 {
     $taskConfig->setTaskIndex($taskIndex);
     $logName = str_replace('kasync', '', strtolower($taskConfig->name));
     $logDate = date('Y-m-d');
     $logFile = "{$logDir}/{$logName}-{$taskIndex}-{$logDate}.log";
     $sysLogFile = "{$taskConfig->name}.{$taskIndex}";
     $this->taskConfig = $taskConfig;
     $taskConfigStr = base64_encode(serialize($taskConfig));
     $cmdLine = '';
     $cmdLine .= is_null($taskConfig->affinity) ? '' : "{$tasksetPath} -c " . ($taskConfig->affinity + $taskIndex) . ' ';
     $cmdLine = "{$phpPath} ";
     $cmdLine .= "{$taskConfig->scriptPath} ";
     $cmdLine .= "{$taskConfigStr} ";
     $cmdLine .= "'[" . mt_rand() . "]' ";
     if ($taskConfig->getUseSyslog()) {
         $cmdLine .= "2>&1 | logger -t {$sysLogFile}";
     } else {
         $cmdLine .= ">> {$logFile} 2>&1";
     }
     $descriptorspec = array();
     // stdin is a pipe that the child will read from
     //		$descriptorspec = array(0 => array("pipe", "r")); // stdin is a pipe that the child will read from
     //			1 => array ( "file" ,$logFile , "a"  ) ,
     //			2 => array ( "file" ,$logFile , "a"  ) ,
     //			1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
     //			2 => array("pipe", "w"),  // stdout is a pipe that the child will write to
     //			2 => array("file", "{$work_dir}/error-output.txt", "a") // stderr is a file to write to
     $other_options = array('suppress_errors' => FALSE, 'bypass_shell' => FALSE);
     KalturaLog::debug("Now executing [{$cmdLine}], [{$other_options}]");
     $process = proc_open($cmdLine, $descriptorspec, $pipes, null, null, $other_options);
     $this->pipes = $pipes;
     $this->handle = $process;
     $this->dieTime = time() + $taskConfig->maximumExecutionTime + 5;
 }
Esempio n. 2
0
 /**
  * @param KSchedularTaskConfig $taskConfig
  */
 public function __construct(KSchedularTaskConfig $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);
 }
 /**
  * @param KSchedularTaskConfig $taskConfig
  */
 private function exeJob(KSchedularTaskConfig $taskConfig)
 {
     $this->debug(__LINE__, "exeJob({$taskConfig->name})");
     $taskConfig->setTaskIndex(1);
     //$taskConfig->setInitOnly(true);
     $instance = new $taskConfig->type($taskConfig);
     $instance->run();
     $instance->done();
 }
 /**
  * @param int $partnerId
  * @param KalturaClient $client
  * @param KSchedularTaskConfig $taskConfig
  */
 public function configure($partnerId, KalturaClient $client, KSchedularTaskConfig $taskConfig)
 {
     $this->partnerId = $partnerId;
     $this->batchPartnerId = $taskConfig->getPartnerId();
     $this->client = $client;
     $this->pager = new KalturaFilterPager();
     $this->pager->pageSize = 100;
     if ($taskConfig->params->pageSize) {
         $this->pager->pageSize = $taskConfig->params->pageSize;
     }
 }
Esempio n. 5
0
 public function _cleanup()
 {
     if ($this->pipes) {
         foreach ($this->pipes as $index => $ref) {
             if (is_resource($ref)) {
                 fclose($ref);
             }
         }
         unset($this->pipes);
         $this->pipes = null;
     }
     if ($this->isMockedProcess) {
         $this->killProcess();
         KScheduleHelperManager::unlinkRunningBatch($this->taskConfig->name, $this->taskConfig->getTaskIndex());
         return;
     }
     if ($this->handle && is_resource($this->handle)) {
         $status = proc_get_status($this->handle);
         if (!$status['running']) {
             return;
         }
         $this->killProcess();
         proc_terminate($this->handle);
         proc_close($this->handle);
         $this->handle = null;
     }
 }
Esempio n. 6
0
 protected function startMonitor(array $files)
 {
     if ($this->monitorHandle && is_resource($this->monitorHandle)) {
         return;
     }
     $killConfig = new KBatchKillerConfig();
     $killConfig->pid = getmypid();
     $killConfig->maxIdleTime = self::$taskConfig->getMaxIdleTime();
     $killConfig->sleepTime = self::$taskConfig->getMaxIdleTime() / 2;
     /*
     Do not run killer process w/out set config->maxIdle
     */
     if ($killConfig->maxIdleTime <= 0 || is_null($killConfig->maxIdleTime)) {
         KalturaLog::info(__METHOD__ . ': The MaxIdleTime is not set properly. The Killer job will not run');
         return;
     }
     $killConfig->files = $files;
     //$killConfig->files = array("/root/anatol/0_phxt8hsa.api.log");
     $killConfig->sessionKey = $this->sessionKey;
     $killConfig->batchIndex = $this->getIndex();
     $killConfig->batchName = $this->getName();
     $killConfig->workerId = $this->getId();
     $killConfig->workerType = $this->getType();
     $killConfig->schedulerId = $this->getSchedulerId();
     $killConfig->schedulerName = $this->getSchedulerName();
     $killConfig->dwhPath = self::$taskConfig->getDwhPath();
     $killConfig->dwhEnabled = self::$taskConfig->getDwhEnabled();
     $phpPath = 'php';
     // TODO - get it from somewhere
     $killerPath = $this->getMonitorPath();
     $killerPathStr = base64_encode(serialize($killConfig));
     $cmdLine = "{$phpPath} {$killerPath} {$killerPathStr}";
     $descriptorspec = array();
     // stdin is a pipe that the child will read from
     $other_options = array('suppress_errors' => FALSE, 'bypass_shell' => FALSE);
     KalturaLog::debug("Killer config:\n" . print_r($killConfig, true));
     KalturaLog::debug("Now executing [{$cmdLine}]");
     KalturaLog::info('Starting monitor');
     $this->monitorHandle = proc_open($cmdLine, $descriptorspec, $pipes, null, null, $other_options);
 }
Esempio n. 7
0
 public function load()
 {
     $this->configTimestamp = $this->calculateFileTimestamp();
     $configFileName = $this->configFileName;
     KalturaLog::log("loading configuration {$configFileName} at " . $this->configTimestamp);
     if (is_dir($this->configFileName)) {
         $configFileName = kEnvironment::get('cache_root_path') . DIRECTORY_SEPARATOR . 'batch' . DIRECTORY_SEPARATOR . 'config.ini';
         $this->implodeDirectoryFiles($configFileName);
     }
     $hostname = self::getHostname();
     parent::__construct($configFileName, $hostname, true);
     $this->name = $hostname;
     $this->hostName = $hostname;
     $this->taskConfigList = array();
     foreach ($this->enabledWorkers as $workerName => $maxInstances) {
         if (!$maxInstances) {
             continue;
         }
         $task = new KSchedularTaskConfig($configFileName, $workerName, $maxInstances);
         $task->setPartnerId($this->getPartnerId());
         $task->setSecret($this->getSecret());
         $task->setCurlTimeout($this->getCurlTimeout());
         $task->setSchedulerId($this->getId());
         $task->setSchedulerName($this->getName());
         $task->setServiceUrl($this->getServiceUrl());
         $task->setDwhPath($this->getDwhPath());
         $task->setDirectoryChmod($this->getDirectoryChmod());
         $task->setChmod($this->getChmod());
         $task->setDwhEnabled($this->getDwhEnabled());
         $task->setTimezone($this->getTimezone());
         $task->setInitOnly(false);
         $task->setRemoteServerUrl($this->getRemoteServerUrl());
         $task->setMaxIdleTime($this->getMaxIdleTime());
         $this->taskConfigList[$workerName] = $task;
     }
 }
 /**
  * @return int the batch instance index
  */
 public function getIndex()
 {
     return $this->taskConfig->getTaskIndex();
 }
 private function parseConfgiFile($configFileName)
 {
     $this->config = new Zend_Config_Ini($configFileName);
     foreach ($this->config as $taskData) {
         $task = new KSchedularTaskConfig();
         $task->id = $taskData->id;
         $task->name = $taskData->name;
         $task->type = $taskData->type;
         $task->maximumExecutionTime = $taskData->maximumExecutionTime;
         $task->friendlyName = $taskData->friendlyName;
         //7;
         $task->maxJobsEachRun = $taskData->maxJobsEachRun;
         //7;
         $task->scriptPath = $taskData->scriptPath;
         // "php C:/web/kaltura/support_prod/test/procmgr/class1.php";
         $task->scriptArgs = $taskData->scriptArgs;
         // "1";
         $task->maxInstances = $taskData->maxInstances;
         // 1;
         $task->sleepBetweenStopStart = $taskData->sleepBetweenStopStart;
         // 3;
         $task->startForQueueSize = $taskData->startForQueueSize;
         // 0;
         $task->autoStart = $taskData->autoStart;
         $task->enable = $taskData->enable;
         //1;
         $task->affinity = $taskData->affinity;
         $task->fileExistReties = $taskData->fileExistReties;
         $task->fileExistInterval = $taskData->fileExistInterval;
         $task->baseLocalPath = $taskData->baseLocalPath;
         $task->baseSharedPath = $taskData->baseSharedPath;
         $task->baseTempLocalPath = $taskData->baseTempLocalPath;
         $task->baseTempSharedPath = $taskData->baseTempSharedPath;
         $task->minCreatedAtMinutes = $taskData->minCreatedAtMinutes;
         $task->params = $taskData->params;
         if ($taskData->filter) {
             $task->filter = new KalturaBatchJobFilter();
             foreach ($taskData->filter as $attr => $value) {
                 $task->filter->{$attr} = $value;
             }
         }
         $task->setPartnerId($this->getPartnerId());
         $task->setSecret($this->getSecret());
         $task->setCurlTimeout($this->getCurlTimeout());
         $task->setSchedulerId($this->getId());
         $task->setSchedulerName($this->getName());
         $task->setServiceUrl($this->getServiceUrl());
         $task->setDwhPath($this->getDwhPath());
         $task->setHostName($this->getHostName());
         $task->setTimezone($this->getTimezone());
         $task->setQueueFiltersDir($this->getQueueFiltersDir());
         $task->setCommandsDir($this->getCommandsDir());
         $task->setUseSyslog($this->getUseSyslog());
         $task->setInitOnly(false);
         $task->setMaxIdleTime($this->getMaxIdleTime());
         $this->taskConfigList[$this->config->key()] = $task;
     }
 }
 public function unimpersonate()
 {
     $this->kClientConfig->partnerId = $this->taskConfig->getPartnerId();
     $this->kClient->setConfig($this->kClientConfig);
 }