/package/app/app/batch/scheduler/KSchedulerConfig.class.php

https://github.com/richhl/kalturaCE · PHP · 596 lines · 421 code · 84 blank · 91 comment · 23 complexity · c463b067c32f0fd350eb7c9386e14bfe MD5 · raw file

  1. <?php
  2. /**
  3. * @package Scheduler
  4. *
  5. */
  6. class KSchedulerConfig
  7. {
  8. private $configFileName;
  9. private $config;
  10. private $configTimestamp;
  11. private $taskConfigList = array();
  12. /**
  13. * @param string $configFileName
  14. */
  15. public function __construct( $configFileName )
  16. {
  17. $this->configFileName = str_replace("\\", "/", $configFileName);
  18. $this->configTimestamp = filemtime(realpath($this->configFileName));
  19. $this->parseConfgiFile($this->configFileName);
  20. }
  21. /**
  22. * set the valus in the memory and save it to the file
  23. *
  24. * @param string $variable
  25. * @param string $value
  26. * @param string $taskName
  27. * @param string $variablePart
  28. * @return boolean
  29. */
  30. public function saveConfig($variable, $value, $taskName = null, $variablePart = null)
  31. {
  32. $ret = $this->setConfig($variable, $value, $taskName, $variablePart);
  33. if($ret)
  34. {
  35. $this->saveToIniFile($this->configFileName);
  36. $this->configTimestamp = filemtime($this->configFileName);
  37. }
  38. return $ret;
  39. }
  40. /**
  41. * set the valus in the memory
  42. *
  43. * @param string $variable
  44. * @param string $value
  45. * @param string $taskName
  46. * @param string $variablePart
  47. * @return boolean
  48. */
  49. public function setConfig($variable, $value, $taskName = null, $variablePart = null)
  50. {
  51. if(is_null($taskName))
  52. {
  53. if(is_null($variablePart))
  54. {
  55. $this->config->KScheduler->$variable = $value;
  56. }
  57. else
  58. {
  59. $this->config->KScheduler->$variable->$variablePart = $value;
  60. }
  61. return true;
  62. }
  63. if(!isset($this->taskConfigList[$taskName]))
  64. return false;
  65. $taskConfig = &$this->taskConfigList[$taskName];
  66. if(!isset($taskConfig->$variable))
  67. return false;
  68. if(!strlen($variablePart))
  69. {
  70. $taskConfig->$variable = $value;
  71. }
  72. else
  73. {
  74. $taskConfig->$variable->$variablePart = $value;
  75. }
  76. return true;
  77. }
  78. public function getFilePath()
  79. {
  80. return $this->configFileName;
  81. }
  82. public function getFileTimestamp()
  83. {
  84. return $this->configTimestamp;
  85. }
  86. public function getTaskConfigList()
  87. {
  88. return $this->taskConfigList;
  89. }
  90. public function getScheduler()
  91. {
  92. return $this->config->KScheduler;
  93. }
  94. public function getId()
  95. {
  96. return $this->config->KScheduler->id;
  97. }
  98. public function getName()
  99. {
  100. return $this->config->KScheduler->name;
  101. }
  102. public function getStatusInterval()
  103. {
  104. return $this->config->KScheduler->statusInterval;
  105. }
  106. public function getTasksetPath()
  107. {
  108. return $this->config->KScheduler->tasksetPath;
  109. }
  110. public function getConfigItemsFilePath()
  111. {
  112. return $this->config->KScheduler->configItemsFilePath;
  113. }
  114. public function getQueueFiltersDir()
  115. {
  116. return $this->config->KScheduler->queueFiltersDir;
  117. }
  118. public function getCommandsDir()
  119. {
  120. return $this->config->KScheduler->commandsDir;
  121. }
  122. public function getUseSyslog()
  123. {
  124. return $this->config->KScheduler->useSyslog;
  125. }
  126. public function getMaxIdleTime()
  127. {
  128. return $this->config->KScheduler->maxIdleTime;
  129. }
  130. public function getStatusFilePath()
  131. {
  132. return $this->config->KScheduler->statusFilePath;
  133. }
  134. public function getCommandResultsFilePath()
  135. {
  136. return $this->config->KScheduler->commandResultsFilePath;
  137. }
  138. public function getLogDir()
  139. {
  140. return $this->config->KScheduler->logDir;
  141. }
  142. public function getMaxExecutionTime()
  143. {
  144. return $this->config->KScheduler->maxExecutionTime;
  145. }
  146. public function getPartnerId()
  147. {
  148. return $this->config->KScheduler->partnerId;
  149. }
  150. public function getHostName()
  151. {
  152. return $this->config->KScheduler->hostName;
  153. }
  154. public function getDwhPath()
  155. {
  156. return $this->config->KScheduler->dwhPath;
  157. }
  158. public function getTimezone()
  159. {
  160. return $this->config->KScheduler->timezone;
  161. }
  162. public function getServiceUrl()
  163. {
  164. return $this->config->KScheduler->serviceUrl;
  165. }
  166. public function getCurlTimeout()
  167. {
  168. return $this->config->KScheduler->curlTimeout;
  169. }
  170. public function getSecret()
  171. {
  172. return $this->config->KScheduler->secret;
  173. }
  174. /**
  175. * @param string $name
  176. * @return KSchedularTaskConfig
  177. */
  178. public function getTaskConfig ( $name )
  179. {
  180. $taskConfig = $this->taskConfigList[$name];
  181. return $taskConfig;
  182. }
  183. private function parseConfgiFile ( $configFileName )
  184. {
  185. $this->config = new Zend_Config_Ini( $configFileName );
  186. foreach($this->config as $taskData)
  187. {
  188. $task = new KSchedularTaskConfig();
  189. $task->id = $taskData->id;
  190. $task->name = $taskData->name ;
  191. $task->type = $taskData->type;
  192. $task->maximumExecutionTime = $taskData->maximumExecutionTime ;
  193. $task->friendlyName = $taskData->friendlyName ; //7;
  194. $task->maxJobsEachRun = $taskData->maxJobsEachRun ; //7;
  195. $task->scriptPath = $taskData->scriptPath ; // "php C:/web/kaltura/support_prod/test/procmgr/class1.php";
  196. $task->scriptArgs= $taskData->scriptArgs ; // "1";
  197. $task->maxInstances = $taskData->maxInstances ; // 1;
  198. $task->sleepBetweenStopStart = $taskData->sleepBetweenStopStart; // 3;
  199. $task->startForQueueSize = $taskData->startForQueueSize; // 0;
  200. $task->autoStart = $taskData->autoStart;
  201. $task->enable = $taskData->enable; //1;
  202. $task->affinity = $taskData->affinity;
  203. $task->fileExistReties = $taskData->fileExistReties;
  204. $task->fileExistInterval = $taskData->fileExistInterval;
  205. $task->baseLocalPath = $taskData->baseLocalPath;
  206. $task->baseSharedPath = $taskData->baseSharedPath;
  207. $task->baseTempLocalPath = $taskData->baseTempLocalPath;
  208. $task->baseTempSharedPath = $taskData->baseTempSharedPath;
  209. $task->minCreatedAtMinutes = $taskData->minCreatedAtMinutes;
  210. $task->params = $taskData->params;
  211. if($taskData->filter)
  212. {
  213. $task->filter = new KalturaBatchJobFilter();
  214. foreach($taskData->filter as $attr => $value)
  215. $task->filter->$attr = $value;
  216. }
  217. $task->setPartnerId($this->getPartnerId());
  218. $task->setSecret($this->getSecret());
  219. $task->setCurlTimeout($this->getCurlTimeout());
  220. $task->setSchedulerId($this->getId());
  221. $task->setSchedulerName($this->getName());
  222. $task->setServiceUrl($this->getServiceUrl());
  223. $task->setDwhPath($this->getDwhPath());
  224. $task->setHostName($this->getHostName());
  225. $task->setTimezone($this->getTimezone());
  226. $task->setQueueFiltersDir($this->getQueueFiltersDir());
  227. $task->setCommandsDir($this->getCommandsDir());
  228. $task->setUseSyslog($this->getUseSyslog());
  229. $task->setInitOnly(false);
  230. $task->setMaxIdleTime($this->getMaxIdleTime());
  231. $this->taskConfigList[$this->config->key()] = $task;
  232. }
  233. }
  234. public function saveToIniFile ( $fileName )
  235. {
  236. $fileContent = '';
  237. foreach($this->config as $k => $v)
  238. {
  239. $fileContent .= "\n[$k]\n";
  240. $fileContent .= $this->configArrayToIni($v);
  241. }
  242. file_put_contents ( $fileName , $fileContent);
  243. }
  244. /**
  245. * taken from http://www.zfforum.de/showthread.php?t=1453
  246. */
  247. private function configArrayToIni($config, $parentPrefix = false, $lastPrefixCount = 0)
  248. {
  249. $str = "";
  250. if(is_array($config))
  251. {
  252. foreach ($config as $k => $v)
  253. {
  254. $prefix = $k;
  255. if($parentPrefix !== false)
  256. {
  257. $prefix = $parentPrefix . '.' . $prefix;
  258. }
  259. $prefixCount = substr_count($prefix, '.');
  260. if(is_array($v))
  261. {
  262. $str .= $this->configArrayToIni($v, $prefix, $prefixCount);
  263. }
  264. else
  265. {
  266. if($prefixCount != $lastPrefixCount)
  267. {
  268. $str .= "\n";
  269. }
  270. $str .= $prefix . (strlen($prefix) < 50 ? str_repeat(" ", 50-strlen($prefix)) : '') . "= " . $v . "\n";
  271. }
  272. $lastPrefixCount = $prefixCount;
  273. }
  274. }
  275. else
  276. {
  277. return $this->configArrayToIni($config->toArray(), $parentPrefix , $lastPrefixCount ) ;
  278. }
  279. return $str;
  280. }
  281. }
  282. /**
  283. * @package Scheduler
  284. *
  285. */
  286. class KSchedularTaskConfig
  287. {
  288. public $id;
  289. public $name;
  290. public $type;
  291. public $maximumExecutionTime;
  292. public $friendlyName;
  293. public $maxJobsEachRun;
  294. public $scriptPath;
  295. public $scriptArgs;
  296. public $maxInstances;
  297. public $sleepBetweenStopStart;
  298. public $startForQueueSize;
  299. public $enable;
  300. public $autoStart;
  301. public $affinity;
  302. public $fileExistReties;
  303. public $fileExistInterval;
  304. public $baseLocalPath;
  305. public $baseSharedPath;
  306. public $baseTempLocalPath;
  307. public $baseTempSharedPath;
  308. public $minCreatedAtMinutes;
  309. /**
  310. * @var array
  311. */
  312. public $params;
  313. /**
  314. * @var KalturaBatchJobFilter
  315. */
  316. public $filter;
  317. // these params are not coming from the config, but set by the scheduler before new batch execution
  318. private $taskIndex;
  319. private $schedulerId;
  320. private $schedulerName;
  321. private $partnerId;
  322. private $serviceUrl;
  323. private $secret;
  324. private $curlTimeout;
  325. private $dwhPath;
  326. private $hostName;
  327. private $timezone;
  328. private $commandsDir;
  329. private $useSyslog;
  330. private $queueFiltersDir;
  331. private $initOnly;
  332. private $maxIdleTime;
  333. public function getTaskIndex()
  334. {
  335. return $this->taskIndex;
  336. }
  337. /**
  338. * @param $maxIdleTime the $maxIdleTime to set
  339. */
  340. public function setMaxIdleTime($maxIdleTime)
  341. {
  342. $this->maxIdleTime = $maxIdleTime;
  343. }
  344. /**
  345. * @return the $maxIdleTime
  346. */
  347. public function getMaxIdleTime()
  348. {
  349. return $this->maxIdleTime;
  350. }
  351. /**
  352. * @param $initOnly the $initOnly to set
  353. */
  354. public function setInitOnly($initOnly)
  355. {
  356. $this->initOnly = $initOnly;
  357. }
  358. /**
  359. * @return the $initOnly
  360. */
  361. public function isInitOnly()
  362. {
  363. return $this->initOnly;
  364. }
  365. /**
  366. * @param $hostName the $hostName to set
  367. */
  368. public function setHostName($hostName)
  369. {
  370. if(is_null($this->hostName))
  371. $this->hostName = $hostName;
  372. }
  373. /**
  374. * @return the $hostName
  375. */
  376. public function getHostName()
  377. {
  378. return $this->hostName;
  379. }
  380. /**
  381. * @param $dwhPath the $dwhPath to set
  382. */
  383. public function setDwhPath($dwhPath)
  384. {
  385. if(is_null($this->dwhPath))
  386. $this->dwhPath = $dwhPath;
  387. }
  388. /**
  389. * @return the $dwhPath
  390. */
  391. public function getDwhPath()
  392. {
  393. return $this->dwhPath;
  394. }
  395. /**
  396. * @param $queueFiltersDir the $queueFiltersDir to set
  397. */
  398. public function setQueueFiltersDir($queueFiltersDir)
  399. {
  400. if(is_null($this->queueFiltersDir))
  401. $this->queueFiltersDir = $queueFiltersDir;
  402. }
  403. /**
  404. * @param $commandsDir the $commandsDir to set
  405. */
  406. public function setCommandsDir($commandsDir)
  407. {
  408. if(is_null($this->commandsDir))
  409. $this->commandsDir = $commandsDir;
  410. }
  411. /**
  412. * @param $useSyslog the $useSyslog to set
  413. */
  414. public function setUseSyslog($useSyslog)
  415. {
  416. if(is_null($this->useSyslog))
  417. $this->useSyslog = $useSyslog;
  418. }
  419. /**
  420. * @param $timezone the $timezone to set
  421. */
  422. public function setTimezone($timezone)
  423. {
  424. if(is_null($this->timezone))
  425. $this->timezone = $timezone;
  426. }
  427. /**
  428. * @return the $queueFiltersDir
  429. */
  430. public function getQueueFiltersDir()
  431. {
  432. return $this->queueFiltersDir;
  433. }
  434. /**
  435. * @return the $commandsDir
  436. */
  437. public function getCommandsDir()
  438. {
  439. return $this->commandsDir;
  440. }
  441. /**
  442. * @return the $useSyslog
  443. */
  444. public function getUseSyslog()
  445. {
  446. return $this->useSyslog;
  447. }
  448. /**
  449. * @return the $timezone
  450. */
  451. public function getTimezone()
  452. {
  453. return $this->timezone;
  454. }
  455. public function setTaskIndex($taskIndex)
  456. {
  457. $this->taskIndex = $taskIndex;
  458. }
  459. public function getSchedulerName()
  460. {
  461. return $this->schedulerName;
  462. }
  463. public function setSchedulerName($schedulerName)
  464. {
  465. $this->schedulerName = $schedulerName;
  466. }
  467. public function getSchedulerId()
  468. {
  469. return $this->schedulerId;
  470. }
  471. public function setSchedulerId($schedulerId)
  472. {
  473. $this->schedulerId = $schedulerId;
  474. }
  475. public function getPartnerId()
  476. {
  477. return $this->partnerId;
  478. }
  479. public function setPartnerId($partnerId)
  480. {
  481. if(is_null($this->partnerId))
  482. $this->partnerId = $partnerId;
  483. }
  484. public function getServiceUrl()
  485. {
  486. return $this->serviceUrl;
  487. }
  488. public function setServiceUrl($serviceUrl)
  489. {
  490. if(is_null($this->serviceUrl))
  491. $this->serviceUrl = $serviceUrl;
  492. }
  493. public function getSecret()
  494. {
  495. return $this->secret;
  496. }
  497. public function setSecret($secret)
  498. {
  499. if(is_null($this->secret))
  500. $this->secret = $secret;
  501. }
  502. public function getCurlTimeout()
  503. {
  504. return $this->curlTimeout;
  505. }
  506. public function setCurlTimeout($curlTimeout)
  507. {
  508. if(is_null($this->curlTimeout))
  509. $this->curlTimeout = $curlTimeout;
  510. }
  511. }
  512. ?>