/package/app/app/api_v3/services/BatchControlService.php

https://bitbucket.org/pandaos/kaltura · PHP · 824 lines · 482 code · 147 blank · 195 comment · 56 complexity · 60ef3c774268062dee5a3f3a0f22d4fa MD5 · raw file

  1. <?php
  2. /**
  3. * batch service lets you handle different batch process from remote machines.
  4. * As oppesed to other ojects in the system, locking mechanism is critical in this case.
  5. * For this reason the GetExclusiveXX, UpdateExclusiveXX and FreeExclusiveXX actions are important for the system's intergity.
  6. * In general - updating batch object should be done only using the UpdateExclusiveXX which in turn can be called only after
  7. * acuiring a batch objet properly (using GetExclusiveXX).
  8. * If an object was aquired and should be returned to the pool in it's initial state - use the FreeExclusiveXX action
  9. *
  10. * Terminology:
  11. * LocationId
  12. * ServerID
  13. * ParternGroups
  14. *
  15. * @service batchcontrol
  16. * @package api
  17. * @subpackage services
  18. */
  19. class BatchControlService extends KalturaBaseService
  20. {
  21. // use initService to add a peer to the partner filter
  22. /**
  23. * @ignore
  24. */
  25. public function initService($serviceId, $serviceName, $actionName)
  26. {
  27. parent::initService($serviceId, $serviceName, $actionName);
  28. // parent::applyPartnerFilterForClass ( new BatchJobPeer() );
  29. // parent::applyPartnerFilterForClass ( new notificationPeer() );
  30. }
  31. // --------------------------------- scheduler support functions --------------------------------- //
  32. /**
  33. * batch reportStatus action saves the a status attribute from a remote scheduler and returns pending commands for the scheduler
  34. *
  35. * @action reportStatus
  36. * @param KalturaScheduler $scheduler The scheduler
  37. * @param KalturaSchedulerStatusArray $schedulerStatuses A scheduler status array
  38. * @param KalturaWorkerQueueFilterArray $workerQueueFilters Filters list to get queues
  39. * @return KalturaSchedulerStatusResponse
  40. */
  41. function reportStatusAction(KalturaScheduler $scheduler, KalturaSchedulerStatusArray $schedulerStatuses, KalturaWorkerQueueFilterArray $workerQueueFilters)
  42. {
  43. $schedulerDb = $this->getOrCreateScheduler($scheduler);
  44. $schedulerChanged = false;
  45. // saves the statuses to the DB
  46. foreach($schedulerStatuses as $schedulerStatus)
  47. {
  48. $schedulerStatus->schedulerId = $schedulerDb->getId();
  49. $schedulerStatus->schedulerConfiguredId = $scheduler->configuredId;
  50. if($schedulerStatus->workerConfiguredId)
  51. {
  52. $worker = $this->getOrCreateWorker($schedulerDb, $schedulerStatus->workerConfiguredId, $schedulerStatus->workerType);
  53. $worker->setStatus($schedulerStatus->type, $schedulerStatus->value);
  54. $worker->save();
  55. $schedulerStatus->workerId = $worker->getId();
  56. }
  57. else
  58. {
  59. $schedulerChanged = true;
  60. $schedulerDb->setStatus($schedulerStatus->type, $schedulerStatus->value);
  61. }
  62. $schedulerStatusDb = new SchedulerStatus();
  63. $schedulerStatus->toObject($schedulerStatusDb);
  64. $schedulerStatusDb->save();
  65. }
  66. if($schedulerChanged)
  67. $schedulerDb->save();
  68. // creates a response
  69. $schedulerStatusResponse = new KalturaSchedulerStatusResponse();
  70. // gets the control pannel commands
  71. $c = new Criteria();
  72. $c->add(ControlPanelCommandPeer::SCHEDULER_ID, $schedulerDb->getId());
  73. $c->add(ControlPanelCommandPeer::TYPE, KalturaControlPanelCommandType::CONFIG, Criteria::NOT_EQUAL);
  74. $c->add(ControlPanelCommandPeer::STATUS, KalturaControlPanelCommandStatus::PENDING);
  75. $commandsList = ControlPanelCommandPeer::doSelect($c);
  76. foreach($commandsList as $command)
  77. {
  78. $command->setStatus(KalturaControlPanelCommandStatus::HANDLED);
  79. $command->save();
  80. }
  81. $schedulerStatusResponse->controlPanelCommands = KalturaControlPanelCommandArray::fromControlPanelCommandArray($commandsList);
  82. // gets new configs
  83. $c = new Criteria();
  84. $c->add(SchedulerConfigPeer::SCHEDULER_ID, $schedulerDb->getId());
  85. $c->add(SchedulerConfigPeer::COMMAND_STATUS, KalturaControlPanelCommandStatus::PENDING);
  86. $configList = SchedulerConfigPeer::doSelect($c);
  87. foreach($configList as $config)
  88. {
  89. $config->setCommandStatus(KalturaControlPanelCommandStatus::HANDLED);
  90. $config->save();
  91. }
  92. $schedulerStatusResponse->schedulerConfigs = KalturaSchedulerConfigArray::fromSchedulerConfigArray($configList);
  93. // gets queues length
  94. $schedulerStatusResponse->queuesStatus = new KalturaBatchQueuesStatusArray();
  95. foreach($workerQueueFilters as $workerQueueFilter)
  96. {
  97. $dbJobType = kPluginableEnumsManager::apiToCore('BatchJobType', $workerQueueFilter->jobType);
  98. $filter = $workerQueueFilter->filter->toFilter($dbJobType);
  99. $batchQueuesStatus = new KalturaBatchQueuesStatus();
  100. $batchQueuesStatus->jobType = $workerQueueFilter->jobType;
  101. $batchQueuesStatus->workerId = $workerQueueFilter->workerId;
  102. $batchQueuesStatus->size = kBatchManager::getQueueSize($workerQueueFilter->schedulerId, $workerQueueFilter->workerId, $dbJobType, $filter);
  103. $schedulerStatusResponse->queuesStatus[] = $batchQueuesStatus;
  104. }
  105. return $schedulerStatusResponse;
  106. }
  107. /**
  108. * batch getOrCreateScheduler returns a scheduler by name, create it if doesnt exist
  109. *
  110. * @param KalturaScheduler $scheduler
  111. * @return Scheduler
  112. */
  113. private function getOrCreateScheduler(KalturaScheduler $scheduler)
  114. {
  115. $c = new Criteria();
  116. $c->add ( SchedulerPeer::CONFIGURED_ID, $scheduler->configuredId);
  117. $schedulerDb = SchedulerPeer::doSelectOne($c, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  118. if($schedulerDb)
  119. {
  120. if(strlen($schedulerDb->getHost()) && $schedulerDb->getHost() != $scheduler->host)
  121. throw new KalturaAPIException(KalturaErrors::SCHEDULER_HOST_CONFLICT, $scheduler->configuredId, $scheduler->host, $schedulerDb->getHost());
  122. if($schedulerDb->getName() != $scheduler->name || $schedulerDb->getHost() != $scheduler->host)
  123. {
  124. $schedulerDb->setName($scheduler->name);
  125. $schedulerDb->setHost($scheduler->host);
  126. $schedulerDb->save();
  127. }
  128. return $schedulerDb;
  129. }
  130. $schedulerDb = new Scheduler();
  131. $schedulerDb->setLastStatus(time());
  132. $schedulerDb->setName($scheduler->name);
  133. $schedulerDb->setHost($scheduler->host);
  134. $schedulerDb->setConfiguredId($scheduler->configuredId);
  135. $schedulerDb->setDescription('');
  136. $schedulerDb->save();
  137. return $schedulerDb;
  138. }
  139. /**
  140. * batch getOrCreateWorker returns a worker by name, create it if doesnt exist
  141. *
  142. * @param Scheduler $scheduler The scheduler object
  143. * @param int $workerConfigId The worker configured id
  144. * @param KalturaBatchJobType $workerType The type of the remote worker
  145. * @param string $workerName The name of the remote worker
  146. * @return Worker
  147. */
  148. private function getOrCreateWorker(Scheduler $scheduler, $workerConfigId, $workerType = null, $workerName = null)
  149. {
  150. if(!is_null($workerType) && !is_numeric($workerType))
  151. $workerType = kPluginableEnumsManager::apiToCore('BatchJobType', $workerType);
  152. $c = new Criteria();
  153. $c->add ( SchedulerWorkerPeer::SCHEDULER_CONFIGURED_ID, $scheduler->getConfiguredId());
  154. $c->add ( SchedulerWorkerPeer::CONFIGURED_ID, $workerConfigId);
  155. $workerDb = SchedulerWorkerPeer::doSelectOne($c, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  156. if($workerDb)
  157. {
  158. $shouldSave = false;
  159. if(!is_null($workerName) && $workerDb->getName() != $workerName)
  160. {
  161. $workerDb->setName($workerName);
  162. $shouldSave = true;
  163. }
  164. if(!is_null($workerType) && $workerDb->getType() != $workerType)
  165. {
  166. $workerDb->setType($workerType);
  167. $shouldSave = true;
  168. }
  169. if($shouldSave)
  170. $workerDb->save();
  171. return $workerDb;
  172. }
  173. $workerDb = new SchedulerWorker();
  174. $workerDb->setLastStatus(time());
  175. $workerDb->setCreatedBy("Scheduler: " . $scheduler->getName());
  176. $workerDb->setUpdatedBy("Scheduler: " . $scheduler->getName());
  177. $workerDb->setSchedulerId($scheduler->getId());
  178. $workerDb->setSchedulerConfiguredId($scheduler->getConfiguredId());
  179. $workerDb->setConfiguredId($workerConfigId);
  180. $workerDb->setDescription('');
  181. if(!is_null($workerType))
  182. $workerDb->setType($workerType);
  183. if(!is_null($workerName))
  184. $workerDb->setName($workerName);
  185. $workerDb->save();
  186. return $workerDb;
  187. }
  188. /**
  189. * batch configLoaded action saves the configuration as loaded by a remote scheduler
  190. *
  191. * @action configLoaded
  192. * @param KalturaScheduler $scheduler The remote scheduler
  193. * @param string $configParam The parameter that was loaded
  194. * @param string $configValue The value that was loaded
  195. * @param string $configParamPart The parameter part that was loaded
  196. * @param int $workerConfigId The id of the job that the configuration refers to, not mandatory if the configuration refers to the scheduler
  197. * @param string $workerName The name of the job that the configuration refers to, not mandatory if the configuration refers to the scheduler
  198. * @return KalturaSchedulerConfig
  199. */
  200. function configLoadedAction(KalturaScheduler $scheduler, $configParam, $configValue, $configParamPart = null, $workerConfigId = null, $workerName = null)
  201. {
  202. $schedulerDb = $this->getOrCreateScheduler($scheduler);
  203. // saves the loaded config to the DB
  204. $configDb = new SchedulerConfig();
  205. $configDb->setSchedulerId($schedulerDb->getId());
  206. $configDb->setSchedulerName($scheduler->name);
  207. $configDb->setSchedulerConfiguredId($scheduler->configuredId);
  208. $configDb->setVariable($configParam);
  209. $configDb->setVariablePart($configParamPart);
  210. $configDb->setValue($configValue);
  211. if($workerConfigId)
  212. {
  213. $worker = $this->getOrCreateWorker($schedulerDb, $workerConfigId, null, $workerName);
  214. $configDb->setWorkerId($worker->getId());
  215. $configDb->setWorkerConfiguredId($workerConfigId);
  216. $configDb->setWorkerName($workerName);
  217. }
  218. $configDb->save();
  219. $config = new KalturaSchedulerConfig();
  220. $config->fromObject($configDb);
  221. return $config;
  222. }
  223. // --------------------------------- scheduler support functions --------------------------------- //
  224. // --------------------------------- control panel functions --------------------------------- //
  225. /**
  226. * batch stop action stops a scheduler
  227. *
  228. * @action stopScheduler
  229. * @param int $schedulerId The id of the remote scheduler location
  230. * @param int $adminId The id of the admin that called the stop
  231. * @param string $cause The reason it was stopped
  232. * @return KalturaControlPanelCommand
  233. */
  234. function stopSchedulerAction($schedulerId, $adminId, $cause)
  235. {
  236. $adminDb = kuserPeer::retrieveByPK($adminId);
  237. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  238. if(!$schedulerDb)
  239. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  240. $description = "Stop " . $schedulerDb->getName();
  241. // check if the same command already sent and not done yet
  242. $c = new Criteria();
  243. $c->add(ControlPanelCommandPeer::STATUS, array(KalturaControlPanelCommandStatus::PENDING, KalturaControlPanelCommandStatus::HANDLED), Criteria::IN);
  244. $c->add(ControlPanelCommandPeer::SCHEDULER_ID, $schedulerId);
  245. $c->add(ControlPanelCommandPeer::TYPE, KalturaControlPanelCommandType::STOP);
  246. $c->add(ControlPanelCommandPeer::TARGET_TYPE, KalturaControlPanelCommandTargetType::SCHEDULER);
  247. $commandExists = ControlPanelCommandPeer::doCount($c);
  248. if($commandExists > 0)
  249. throw new KalturaAPIException(KalturaErrors::COMMAND_ALREADY_PENDING);
  250. // saves the command to the DB
  251. $commandDb = new ControlPanelCommand();
  252. $commandDb->setSchedulerId($schedulerId);
  253. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  254. $commandDb->setCreatedById($adminId);
  255. $commandDb->setType(KalturaControlPanelCommandType::STOP);
  256. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  257. $commandDb->setDescription($description);
  258. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::SCHEDULER);
  259. if($adminDb)
  260. $commandDb->setCreatedBy($adminDb->getName());
  261. $commandDb->save();
  262. $command = new KalturaControlPanelCommand();
  263. $command->fromObject($commandDb);
  264. return $command;
  265. }
  266. /**
  267. * batch stop action stops a worker
  268. *
  269. * @action stopWorker
  270. * @param int $workerId The id of the job to be stopped
  271. * @param int $adminId The id of the admin that called the stop
  272. * @param string $cause The reason it was stopped
  273. * @return KalturaControlPanelCommand
  274. */
  275. function stopWorkerAction($workerId, $adminId, $cause)
  276. {
  277. $adminDb = kuserPeer::retrieveByPK($adminId);
  278. $workerDb = SchedulerWorkerPeer::retrieveByPK($workerId);
  279. if(!$workerDb)
  280. throw new KalturaAPIException(KalturaErrors::WORKER_NOT_FOUND, $workerId);
  281. $workerName = $workerDb->getName();
  282. $schedulerId = $workerDb->getSchedulerId();
  283. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  284. if(!$schedulerDb)
  285. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  286. $schedulerName = $schedulerDb->getName();
  287. $description = "Stop $workerName on $schedulerName";
  288. // check if the same command already sent and not done yet
  289. $c = new Criteria();
  290. $c->add(ControlPanelCommandPeer::STATUS, array(KalturaControlPanelCommandStatus::PENDING, KalturaControlPanelCommandStatus::HANDLED), Criteria::IN);
  291. $c->add(ControlPanelCommandPeer::SCHEDULER_ID, $schedulerId);
  292. $c->add(ControlPanelCommandPeer::TYPE, KalturaControlPanelCommandType::STOP);
  293. $c->add(ControlPanelCommandPeer::TARGET_TYPE, KalturaControlPanelCommandTargetType::JOB);
  294. $c->add(ControlPanelCommandPeer::WORKER_ID, $workerId);
  295. $commandExists = ControlPanelCommandPeer::doCount($c);
  296. if($commandExists > 0)
  297. throw new KalturaAPIException(KalturaErrors::COMMAND_ALREADY_PENDING);
  298. // saves the command to the DB
  299. $commandDb = new ControlPanelCommand();
  300. $commandDb->setSchedulerId($schedulerId);
  301. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  302. $commandDb->setWorkerId($workerId);
  303. $commandDb->setWorkerConfiguredId($workerDb->getConfiguredId());
  304. $commandDb->setWorkerName($workerName);
  305. $commandDb->setCreatedById($adminId);
  306. $commandDb->setType(KalturaControlPanelCommandType::STOP);
  307. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  308. $commandDb->setDescription($description);
  309. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::JOB);
  310. $commandDb->setCause($cause);
  311. if($adminDb)
  312. $commandDb->setCreatedBy($adminDb->getName());
  313. $commandDb->save();
  314. $command = new KalturaControlPanelCommand();
  315. $command->fromObject($commandDb);
  316. return $command;
  317. }
  318. /**
  319. * batch kill action forces stop og a batch on a remote scheduler
  320. *
  321. * @action kill
  322. * @param int $workerId The id of the job to be stopped
  323. * @param int $batchIndex The index of the batch job process to be stopped
  324. * @param int $adminId The id of the admin that called the stop
  325. * @param string $cause The reason it was stopped
  326. * @return KalturaControlPanelCommand
  327. */
  328. function killAction($workerId, $batchIndex, $adminId, $cause)
  329. {
  330. $adminDb = kuserPeer::retrieveByPK($adminId);
  331. $workerDb = SchedulerWorkerPeer::retrieveByPK($workerId);
  332. if(!$workerDb)
  333. throw new KalturaAPIException(KalturaErrors::WORKER_NOT_FOUND, $workerId);
  334. $workerName = $workerDb->getName();
  335. $schedulerId = $workerDb->getSchedulerId();
  336. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  337. if(!$schedulerDb)
  338. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  339. $schedulerName = $schedulerDb->getName();
  340. $description = "Stop $workerName on $schedulerName";
  341. if(is_null($workerName))
  342. $description = "Stop $schedulerName";
  343. // check if the same command already sent and not done yet
  344. $c = new Criteria();
  345. $c->add(ControlPanelCommandPeer::STATUS, array(KalturaControlPanelCommandStatus::PENDING, KalturaControlPanelCommandStatus::HANDLED), Criteria::IN);
  346. $c->add(ControlPanelCommandPeer::SCHEDULER_ID, $schedulerId);
  347. $c->add(ControlPanelCommandPeer::WORKER_ID, $workerId);
  348. $c->add(ControlPanelCommandPeer::WORKER_NAME, $workerName);
  349. $c->add(ControlPanelCommandPeer::BATCH_INDEX, $batchIndex);
  350. $c->add(ControlPanelCommandPeer::TYPE, KalturaControlPanelCommandType::KILL);
  351. $c->add(ControlPanelCommandPeer::TARGET_TYPE, KalturaControlPanelCommandTargetType::BATCH);
  352. $commandExists = ControlPanelCommandPeer::doCount($c);
  353. if($commandExists > 0)
  354. throw new KalturaAPIException(KalturaErrors::COMMAND_ALREADY_PENDING);
  355. // saves the command to the DB
  356. $commandDb = new ControlPanelCommand();
  357. $commandDb->setSchedulerId($schedulerId);
  358. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  359. $commandDb->setWorkerId($workerId);
  360. $commandDb->setWorkerConfiguredId($workerDb->getConfiguredId());
  361. $commandDb->setWorkerName($workerName);
  362. $commandDb->setBatchIndex($batchIndex);
  363. $commandDb->setCreatedById($adminId);
  364. $commandDb->setType(KalturaControlPanelCommandType::KILL);
  365. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  366. $commandDb->setDescription($description);
  367. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::BATCH);
  368. if($adminDb)
  369. $commandDb->setCreatedBy($adminDb->getName());
  370. $commandDb->save();
  371. $command = new KalturaControlPanelCommand();
  372. $command->fromObject($commandDb);
  373. return $command;
  374. }
  375. /**
  376. * batch start action starts a job
  377. *
  378. * @action startWorker
  379. * @param int $workerId The id of the job to be started
  380. * @param int $adminId The id of the admin that called the start
  381. * @param string $cause The reason it was started
  382. * @return KalturaControlPanelCommand
  383. */
  384. function startWorkerAction($workerId, $adminId, $cause = null)
  385. {
  386. $adminDb = kuserPeer::retrieveByPK($adminId);
  387. $workerDb = SchedulerWorkerPeer::retrieveByPK($workerId);
  388. if(!$workerDb)
  389. throw new KalturaAPIException(KalturaErrors::WORKER_NOT_FOUND, $workerId);
  390. $workerName = $workerDb->getName();
  391. $schedulerId = $workerDb->getSchedulerId();
  392. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  393. if(!$schedulerDb)
  394. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  395. $schedulerName = $schedulerDb->getName();
  396. $description = "Start $workerName on $schedulerName";
  397. // check if the same command already sent and not done yet
  398. $c = new Criteria();
  399. $c->add(ControlPanelCommandPeer::STATUS, array(KalturaControlPanelCommandStatus::PENDING, KalturaControlPanelCommandStatus::HANDLED), Criteria::IN);
  400. $c->add(ControlPanelCommandPeer::SCHEDULER_ID, $schedulerId);
  401. $c->add(ControlPanelCommandPeer::TYPE, KalturaControlPanelCommandType::START);
  402. $c->add(ControlPanelCommandPeer::TARGET_TYPE, KalturaControlPanelCommandTargetType::JOB);
  403. $c->add(ControlPanelCommandPeer::WORKER_ID, $workerId);
  404. $commandExists = ControlPanelCommandPeer::doCount($c);
  405. if($commandExists > 0)
  406. throw new KalturaAPIException(KalturaErrors::COMMAND_ALREADY_PENDING);
  407. // saves the command to the DB
  408. $commandDb = new ControlPanelCommand();
  409. $commandDb->setSchedulerId($schedulerId);
  410. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  411. $commandDb->setCreatedById($adminId);
  412. $commandDb->setType(KalturaControlPanelCommandType::START);
  413. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  414. $commandDb->setDescription($description);
  415. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::JOB);
  416. $commandDb->setWorkerId($workerId);
  417. $commandDb->setWorkerConfiguredId($workerDb->getConfiguredId());
  418. $commandDb->setWorkerName($workerName);
  419. if($adminDb)
  420. $commandDb->setCreatedBy($adminDb->getName());
  421. $commandDb->save();
  422. $command = new KalturaControlPanelCommand();
  423. $command->fromObject($commandDb);
  424. return $command;
  425. }
  426. /**
  427. * batch sets a configuration parameter to be loaded by a scheduler
  428. *
  429. * @action setSchedulerConfig
  430. * @param int $schedulerId The id of the remote scheduler location
  431. * @param int $adminId The id of the admin that called the setConfig
  432. * @param string $configParam The parameter to be set
  433. * @param string $configValue The value to be set
  434. * @param string $configParamPart The parameter part to be set - for additional params
  435. * @param string $cause The reason it was changed
  436. * @return KalturaControlPanelCommand
  437. */
  438. function setSchedulerConfigAction($schedulerId, $adminId, $configParam, $configValue, $configParamPart = null, $cause = null)
  439. {
  440. $adminDb = kuserPeer::retrieveByPK($adminId);
  441. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  442. if(!$schedulerDb)
  443. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  444. $schedulerName = $schedulerDb->getName();
  445. $description = "Configure $configParam on $schedulerName";
  446. if(!is_null($configParamPart))
  447. $description = "Configure $configParam.$configParamPart on $schedulerName";
  448. // saves the command to the DB
  449. $commandDb = new ControlPanelCommand();
  450. $commandDb->setSchedulerId($schedulerId);
  451. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  452. $commandDb->setCreatedById($adminId);
  453. $commandDb->setType(KalturaControlPanelCommandType::CONFIG);
  454. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  455. $commandDb->setDescription($description);
  456. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::SCHEDULER);
  457. $commandDb->setCause($cause);
  458. if($adminDb)
  459. $commandDb->setCreatedBy($adminDb->getName());
  460. $commandDb->save();
  461. // saves the new config to the DB
  462. $configDb = new SchedulerConfig();
  463. $configDb->setSchedulerId($schedulerId);
  464. $configDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  465. $configDb->setCommandId($commandDb->getId());
  466. $configDb->setCommandStatus(KalturaControlPanelCommandStatus::PENDING);
  467. $configDb->setSchedulerName($schedulerName);
  468. $configDb->setVariable($configParam);
  469. $configDb->setVariablePart($configParamPart);
  470. $configDb->setValue($configValue);
  471. if($adminDb)
  472. $configDb->setCreatedBy($adminDb->getName());
  473. $configDb->save();
  474. $command = new KalturaControlPanelCommand();
  475. $command->fromObject($commandDb);
  476. return $command;
  477. }
  478. /**
  479. * batch sets a configuration parameter to be loaded by a worker
  480. *
  481. * @action setWorkerConfig
  482. * @param int $workerId The id of the job to be configured
  483. * @param int $adminId The id of the admin that called the setConfig
  484. * @param string $configParam The parameter to be set
  485. * @param string $configValue The value to be set
  486. * @param string $configParamPart The parameter part to be set - for additional params
  487. * @param string $cause The reason it was changed
  488. * @return KalturaControlPanelCommand
  489. */
  490. function setWorkerConfigAction($workerId, $adminId, $configParam, $configValue, $configParamPart = null, $cause = null)
  491. {
  492. $adminDb = kuserPeer::retrieveByPK($adminId);
  493. $workerDb = SchedulerWorkerPeer::retrieveByPK($workerId);
  494. if(!$workerDb)
  495. throw new KalturaAPIException(KalturaErrors::WORKER_NOT_FOUND, $workerId);
  496. $workerName = $workerDb->getName();
  497. $schedulerId = $workerDb->getSchedulerId();
  498. $schedulerDb = SchedulerPeer::retrieveByPK($schedulerId);
  499. if(!$schedulerDb)
  500. throw new KalturaAPIException(KalturaErrors::SCHEDULER_NOT_FOUND, $schedulerId);
  501. $schedulerName = $schedulerDb->getName();
  502. $description = "Configure $configParam on $schedulerName";
  503. if(!is_null($configParamPart))
  504. $description = "Configure $configParam.$configParamPart on $schedulerName";
  505. // saves the command to the DB
  506. $commandDb = new ControlPanelCommand();
  507. $commandDb->setSchedulerId($schedulerId);
  508. $commandDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  509. $commandDb->setCreatedById($adminId);
  510. $commandDb->setType(KalturaControlPanelCommandType::CONFIG);
  511. $commandDb->setStatus(KalturaControlPanelCommandStatus::PENDING);
  512. $commandDb->setDescription($description);
  513. $commandDb->setWorkerId($workerId);
  514. $commandDb->setWorkerConfiguredId($workerDb->getConfiguredId());
  515. $commandDb->setWorkerName($workerName);
  516. $commandDb->setTargetType(KalturaControlPanelCommandTargetType::JOB);
  517. if($adminDb)
  518. $commandDb->setCreatedBy($adminDb->getName());
  519. $commandDb->setCause($cause);
  520. $commandDb->save();
  521. // saves the new config to the DB
  522. $configDb = new SchedulerConfig();
  523. $configDb->setSchedulerId($schedulerId);
  524. $configDb->setSchedulerConfiguredId($schedulerDb->getConfiguredId());
  525. $configDb->setCommandId($commandDb->getId());
  526. $configDb->setCommandStatus(KalturaControlPanelCommandStatus::PENDING);
  527. $configDb->setSchedulerName($schedulerName);
  528. $configDb->setVariable($configParam);
  529. $configDb->setVariablePart($configParamPart);
  530. $configDb->setValue($configValue);
  531. $configDb->setWorkerId($workerId);
  532. $configDb->setWorkerConfiguredId($workerDb->getConfiguredId());
  533. $configDb->setWorkerName($workerName);
  534. if($adminDb)
  535. $configDb->setCreatedBy($adminDb->getName());
  536. $configDb->save();
  537. $command = new KalturaControlPanelCommand();
  538. $command->fromObject($commandDb);
  539. return $command;
  540. }
  541. /**
  542. * batch setCommandResult action saves the results of a command as recieved from a remote scheduler
  543. *
  544. * @action setCommandResult
  545. * @param int $commandId The id of the command
  546. * @param KalturaControlPanelCommandStatus $status The status of the command
  547. * @param int $timestamp The time that the command performed
  548. * @param string $errorDescription The description, important for failed commands
  549. * @return KalturaControlPanelCommand
  550. */
  551. function setCommandResultAction($commandId, $status, $errorDescription = null)
  552. {
  553. // find the command
  554. $commandDb = ControlPanelCommandPeer::retrieveByPK($commandId);
  555. if (!$commandDb)
  556. throw new KalturaAPIException(KalturaErrors::COMMAND_NOT_FOUND, $commandId);
  557. // save the results to the DB
  558. $commandDb->setStatus($status);
  559. if(!is_null($errorDescription))
  560. $commandDb->setErrorDescription($errorDescription);
  561. $commandDb->save();
  562. // if is config, update the config status
  563. if($commandDb->getType() == KalturaControlPanelCommandType::CONFIG)
  564. {
  565. $c = new Criteria();
  566. $c->add ( SchedulerConfigPeer::COMMAND_ID, $commandId);
  567. $configDb = SchedulerConfigPeer::doSelectOne($c);
  568. if($configDb)
  569. {
  570. $configDb->setCommandStatus($status);
  571. $configDb->save();
  572. }
  573. }
  574. $command = new KalturaControlPanelCommand();
  575. $command->fromObject($commandDb);
  576. return $command;
  577. }
  578. /**
  579. * list batch control commands
  580. *
  581. * @action listCommands
  582. * @param KalturaControlPanelCommandFilter $filter
  583. * @param KalturaFilterPager $pager
  584. * @return KalturaControlPanelCommandListResponse
  585. */
  586. function listCommandsAction(KalturaControlPanelCommandFilter $filter = null, KalturaFilterPager $pager = null)
  587. {
  588. if (!$filter)
  589. $filter = new KalturaControlPanelCommandFilter();
  590. $controlPanelCommandFilter = new ControlPanelCommandFilter();
  591. $filter->toObject($controlPanelCommandFilter);
  592. $c = new Criteria();
  593. $controlPanelCommandFilter->attachToCriteria($c);
  594. if ($pager )
  595. $pager->attachToCriteria($c);
  596. $count = ControlPanelCommandPeer::doCount($c);
  597. $list = ControlPanelCommandPeer::doSelect($c);
  598. $newList = KalturaControlPanelCommandArray::fromControlPanelCommandArray($list);
  599. $response = new KalturaControlPanelCommandListResponse();
  600. $response->objects = $newList;
  601. $response->totalCount = $count;
  602. return $response;
  603. }
  604. /**
  605. * batch getCommand action returns the command with its current status
  606. *
  607. * @action getCommand
  608. * @param int $commandId The id of the command
  609. * @return KalturaControlPanelCommand
  610. */
  611. function getCommandAction($commandId)
  612. {
  613. // finds command in the DB
  614. $commandDb = ControlPanelCommandPeer::retrieveByPK($commandId);
  615. if (!$commandDb)
  616. throw new KalturaAPIException(KalturaErrors::COMMAND_NOT_FOUND, $commandId);
  617. // returns the command
  618. $command = new KalturaControlPanelCommand();
  619. $command->fromObject($commandDb);
  620. return $command;
  621. }
  622. /**
  623. * list all Schedulers
  624. *
  625. * @action listSchedulers
  626. * @return KalturaSchedulerListResponse
  627. */
  628. function listSchedulersAction()
  629. {
  630. $c = new Criteria();
  631. $count = SchedulerPeer::doCount($c, false, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  632. $list = SchedulerPeer::doSelect($c, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  633. $newList = KalturaSchedulerArray::fromSchedulerArray($list );
  634. $response = new KalturaSchedulerListResponse();
  635. $response->objects = $newList;
  636. $response->totalCount = $count;
  637. return $response;
  638. }
  639. /**
  640. * list all Workers
  641. *
  642. * @action listWorkers
  643. * @return KalturaSchedulerWorkerListResponse
  644. */
  645. function listWorkersAction()
  646. {
  647. $c = new Criteria();
  648. $count = SchedulerWorkerPeer::doCount($c, false, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  649. $list = SchedulerWorkerPeer::doSelect($c, myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  650. $newList = KalturaSchedulerWorkerArray::fromSchedulerWorkerArray($list);
  651. $response = new KalturaSchedulerWorkerListResponse();
  652. $response->objects = $newList;
  653. $response->totalCount = $count;
  654. return $response;
  655. }
  656. /**
  657. * batch getFullStatus action returns the status of all schedulers and queues
  658. *
  659. * @action getFullStatus
  660. * @return KalturaControlPanelCommand
  661. */
  662. function getFullStatusAction()
  663. {
  664. $response = new KalturaFullStatusResponse();
  665. // gets queues length
  666. // $c = new Criteria();
  667. // $c->add(BatchJobPeer::STATUS, array(KalturaBatchJobStatus::PENDING, KalturaBatchJobStatus::RETRY), Criteria::IN);
  668. // $c->addGroupByColumn(BatchJobPeer::JOB_TYPE);
  669. // $c->addSelectColumn('AVG(DATEDIFF(NOW(),' . BatchJobPeer::CREATED_AT . '))');
  670. $queueList = BatchJobPeer::doQueueStatus(myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2));
  671. $response->queuesStatus = KalturaBatchQueuesStatusArray::fromBatchQueuesStatusArray($queueList);
  672. $response->schedulers = KalturaSchedulerArray::statusFromSchedulerArray(SchedulerPeer::doSelect(new Criteria(), myDbHelper::getConnection(myDbHelper::DB_HELPER_CONN_PROPEL2)));
  673. return $response;
  674. }
  675. // --------------------------------- control panel functions --------------------------------- //
  676. }
  677. ?>