BxDolTranscoderQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_QUEUE_PRUNE_FAILED', 604800); ///< timeout in seconds when 'failed' items are deleted
  10. define('BX_DOL_QUEUE_PRUNE_PROCESSING', 86400); ///< timeout in seconds when 'processing' items are deleted, in case when something went wrong during processing
  11. /**
  12. * @see BxDolTranscoder
  13. */
  14. class BxDolTranscoderQuery extends BxDolDb
  15. {
  16. const TABLE_QUEUE = 'sys_transcoder_queue';
  17. protected $_aObject;
  18. protected $_sTableQueue;
  19. protected $_sTableFiles;
  20. protected $_sTableFilters;
  21. protected $_sHandlerPrefix;
  22. public function __construct($aObject, $bUseQueue = false)
  23. {
  24. parent::__construct();
  25. $this->_aObject = $aObject;
  26. $this->_sTableQueue = $bUseQueue ? self::TABLE_QUEUE : '';
  27. $this->_sTableFiles = '`sys_transcoder_images_files`';
  28. $this->_sTableFilters = '`sys_transcoder_filters`';
  29. $this->_sHandlerPrefix = 'sys_image_transcoder_';
  30. }
  31. static public function getTranscoderObject ($sObject)
  32. {
  33. $oDb = BxDolDb::getInstance();
  34. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder` WHERE `object` = ?", $sObject);
  35. return $oDb->getRow($sQuery);
  36. }
  37. static public function getTranscoderObjects ()
  38. {
  39. $oDb = BxDolDb::getInstance();
  40. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_transcoder`");
  41. return $oDb->getAll($sQuery);
  42. }
  43. public function getTranscoderFilters ($sFilterName = '')
  44. {
  45. $aBindings = array('object' => $this->_aObject['object']);
  46. $sWhere = '';
  47. if ($sFilterName) {
  48. $sWhere .= ' AND `filter` = :filter ';
  49. $aBindings['filter'] = $sFilterName;
  50. }
  51. $sQuery = "SELECT * FROM {$this->_sTableFilters} WHERE `transcoder_object` = :object $sWhere ORDER BY `order` ASC";
  52. return $this->fromMemory('BxDolTranscoderQuery::getTranscoderFilters' . $this->_aObject['object'] . $sFilterName, 'getAll', $sQuery, $aBindings);
  53. }
  54. public function updateHandler ($iFileId, $mixedHandler)
  55. {
  56. $aBindings = array(
  57. 'transcoder' => $this->_aObject['object'],
  58. 'file_id' => $iFileId,
  59. 'handler' => $mixedHandler
  60. );
  61. $sUpdateATime = '';
  62. if ($this->_aObject['atime_tracking']) {
  63. $aBindings['atime'] = time();
  64. $sUpdateATime = ", `atime` = :atime";
  65. }
  66. return $this->query("INSERT INTO {$this->_sTableFiles} SET `transcoder_object` = :transcoder, `file_id` = :file_id, `handler` = :handler $sUpdateATime ON DUPLICATE KEY UPDATE `file_id` = :file_id $sUpdateATime", $aBindings);
  67. }
  68. public function getFileIdByHandler ($mixedHandler)
  69. {
  70. $sQuery = $this->prepare("SELECT `file_id` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $this->_aObject['object'], (string)$mixedHandler);
  71. return $this->getOne($sQuery);
  72. }
  73. public function updateObjectModificationTime($iTime = 0)
  74. {
  75. $sQuery = $this->prepare("UPDATE `sys_objects_transcoder` SET `ts` = ? WHERE `object` = ?", $iTime ? $iTime : time(), $this->_aObject['object']);
  76. return $this->res($sQuery);
  77. }
  78. public function updateAccessTime($mixedHandler)
  79. {
  80. $iTime = time();
  81. if ($this->getOne("SELECT `atime` FROM {$this->_sTableFiles} WHERE `transcoder_object` = :obj AND `handler` = :hndl AND `atime` > :ts LIMIT 1", array('obj' => $this->_aObject['object'], 'hndl' => (string)$mixedHandler, 'ts' => $iTime - BX_DOL_SKIP_ATIME_UPDATE))) // avoid deadlocks by making changes less frequent
  82. return true;
  83. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `atime` = ? WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $iTime, $this->_aObject['object'], (string)$mixedHandler);
  84. $mixedRes = $this->res($sQuery);
  85. $this->setReadOnlyMode(true);
  86. return $mixedRes;
  87. }
  88. public function deleteFileTraces($iFileId)
  89. {
  90. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  91. return $this->res($sQuery);
  92. }
  93. public function getFilesForPruning ()
  94. {
  95. if (!$this->_aObject['atime_tracking'] || !$this->_aObject['atime_pruning'])
  96. return array();
  97. $sQuery = $this->prepare("SELECT * FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `atime` != 0 AND `atime` < ?", $this->_aObject['object'], time() - $this->_aObject['atime_pruning']);
  98. return $this->getAll($sQuery);
  99. }
  100. public function registerHandlers ()
  101. {
  102. if (!$this->registerHandler ('getAlertHandlerNameLocal', 'alert_response_file_delete_local', $this->_aObject['object'], $this->_aObject['storage_object']))
  103. return false;
  104. // add handler for original storage engine
  105. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  106. if (!$this->registerHandler ('getAlertHandlerNameOrig', 'alert_response_file_delete_orig', $this->_aObject['object'], $this->_aObject['source_params']['object']))
  107. return false;
  108. return true;
  109. }
  110. public function unregisterHandlers ()
  111. {
  112. if (!$this->unregisterHandler('getAlertHandlerNameLocal', $this->_aObject['storage_object']))
  113. return false;
  114. // remove handler for original storage engine
  115. if ('Storage' == $this->_aObject['source_type']) // if original storage is "Storage", not "Folder"
  116. if (!$this->unregisterHandler('getAlertHandlerNameOrig', $this->_aObject['source_params']['object']))
  117. return false;
  118. return true;
  119. }
  120. static public function getForDeletionFromQueue ($sServer, $iLimit = 10)
  121. {
  122. $oDb = BxDolDb::getInstance();
  123. // check if files which are processing are alive
  124. if (function_exists('posix_kill')) {
  125. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  126. if ($a = $oDb->getAll($sQuery)) {
  127. foreach ($a as $r) {
  128. $o = BxDolTranscoder::getObjectInstance($r['transcoder_object']);
  129. if ($o && $r['pid'] && !posix_kill($r['pid'], 0)) {
  130. $o->getDb()->updateQueueStatus($r['file_id_source'], BX_DOL_QUEUE_FAILED, "transcoding process died unexpectedly\n");
  131. }
  132. }
  133. }
  134. }
  135. // delete items which are stuck in processing status
  136. $sQuery = $oDb->prepare("DELETE FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = ? AND `changed` < ? - ?", $sServer, BX_DOL_QUEUE_PROCESSING, time(), BX_DOL_QUEUE_PRUNE_PROCESSING);
  137. $oDb->query($sQuery);
  138. // get files which are subject to delete: with status 'delete' and expired 'failed' items
  139. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND ((`status` = ? AND `changed` < ? - ?) OR `status` = ?)", $sServer, BX_DOL_QUEUE_FAILED, time(), BX_DOL_QUEUE_PRUNE_FAILED, BX_DOL_QUEUE_DELETE);
  140. return $oDb->getAll($sQuery);
  141. }
  142. static public function deleteFromQueueById ($iId)
  143. {
  144. $oDb = BxDolDb::getInstance();
  145. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `id` = ?", $iId);
  146. return $oDb->query($sQuery);
  147. }
  148. static public function getCompletedFromQueue ($iLimit = 10)
  149. {
  150. $oDb = BxDolDb::getInstance();
  151. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'complete' ORDER BY `added` ASC LIMIT ?", $iLimit);
  152. return $oDb->getAll($sQuery);
  153. }
  154. static public function getNextInQueue ($sServer, $iLimit = 5)
  155. {
  156. $oDb = BxDolDb::getInstance();
  157. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `" . self::TABLE_QUEUE . "` WHERE `server` = ? AND `status` = 'processing'", $sServer);
  158. if ($oDb->getOne($sQuery))
  159. return array();
  160. $sQuery = $oDb->prepare("SELECT * FROM `" . self::TABLE_QUEUE . "` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT ?", $iLimit);
  161. return $oDb->getAll($sQuery);
  162. }
  163. public function getQueueTable ()
  164. {
  165. return $this->_sTableQueue;
  166. }
  167. public function deleteFromQueue ($mixedId)
  168. {
  169. if (!$this->_sTableQueue)
  170. return false;
  171. $sQuery = $this->prepare("DELETE FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  172. return $this->query($sQuery);
  173. }
  174. public function getFromQueue ($mixedId)
  175. {
  176. if (!$this->_sTableQueue)
  177. return false;
  178. $sQuery = $this->prepare("SELECT * FROM `" . $this->_sTableQueue . "` WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  179. return $this->getRow($sQuery);
  180. }
  181. public function addToQueue ($mixedId, $sFileUrl, $iProfileId)
  182. {
  183. if (!$this->_sTableQueue)
  184. return false;
  185. $sQuery = $this->prepare("INSERT INTO `" . $this->_sTableQueue . "` SET `transcoder_object` = ?, `profile_id` = ?, `file_url_source` = ?, `file_id_source` = ?, `status` = ?, `added` = ?, changed = ?", $this->_aObject['object'], $iProfileId, $sFileUrl, $mixedId, BX_DOL_QUEUE_PENDING, time(), time());
  186. return $this->query($sQuery);
  187. }
  188. public function updateQueueStatus ($mixedId, $sStatus, $sLog = '', $sServer = '', $mixedIdResult = '', $sFileUrlResult = '', $sFileExtResult = '', $iPID = 0)
  189. {
  190. if (!$this->_sTableQueue)
  191. return false;
  192. $aSet = array(
  193. 'status' => $sStatus,
  194. 'changed' => time()
  195. );
  196. if($sLog)
  197. $aSet['log'] = $sLog;
  198. if($sServer)
  199. $aSet['server'] = $sServer;
  200. if($iPID)
  201. $aSet['pid'] = $iPID;
  202. if($mixedIdResult && $sFileUrlResult)
  203. $aSet = array_merge($aSet, array(
  204. 'file_id_result' => $mixedIdResult,
  205. 'file_url_result' => $sFileUrlResult,
  206. 'file_ext_result' => $sFileExtResult
  207. ));
  208. $sQuery = $this->prepare("UPDATE `" . $this->_sTableQueue . "` SET " . $this->arrayToSQL($aSet) . " WHERE `transcoder_object` = ? AND `file_id_source` = ?", $this->_aObject['object'], $mixedId);
  209. return $this->query($sQuery);
  210. }
  211. protected function registerHandler ($sHandlerNameFunc, $sServiceFunc, $sObject, $sUnit)
  212. {
  213. $sHandlerName = $this->$sHandlerNameFunc ();
  214. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  215. if ($iHandlerId) // if handler already exists, do nothing
  216. return true;
  217. $sServiceCall = serialize(array(
  218. 'module' => 'system',
  219. 'method' => $sServiceFunc,
  220. 'params' => array($sObject),
  221. 'class' => 'TemplImageServices'
  222. ));
  223. $sQuery = $this->prepare("INSERT INTO `sys_alerts_handlers` SET `name` = ?, `service_call` = ?", $sHandlerName, $sServiceCall);
  224. if (!$this->query($sQuery))
  225. return false;
  226. $iHandlerId = $this->lastId();
  227. $sQuery = $this->prepare("INSERT INTO `sys_alerts` SET `unit` = ?, `action` = ?, `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  228. if (!$this->query($sQuery)) {
  229. $this->unregisterHandlers ();
  230. return false;
  231. }
  232. return true;
  233. }
  234. protected function unregisterHandler ($sHandlerNameFunc, $sUnit)
  235. {
  236. $sHandlerName = $this->$sHandlerNameFunc ();
  237. $iHandlerId = $this->getAlertHandlerId ($sHandlerName);
  238. if (!$iHandlerId) // if handler is alrady missing, do nothing
  239. return true;
  240. $sQuery = $this->prepare("DELETE FROM `sys_alerts` WHERE `unit` = ? AND `action` = ? AND `handler_id` = ?", $sUnit, 'file_deleted', $iHandlerId);
  241. if (!$this->query($sQuery))
  242. return false;
  243. $sQuery = $this->prepare("DELETE FROM `sys_alerts_handlers` WHERE `id` = ?", $iHandlerId);
  244. return $this->query($sQuery);
  245. }
  246. protected function getAlertHandlerNameLocal ()
  247. {
  248. return $this->_sHandlerPrefix . 'local_file_delete_' . $this->_aObject['object'];
  249. }
  250. protected function getAlertHandlerNameOrig ()
  251. {
  252. return $this->_sHandlerPrefix . 'orig_file_delete_' . $this->_aObject['object'];
  253. }
  254. protected function getAlertHandlerId ($sHandlerName)
  255. {
  256. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = ?", $sHandlerName);
  257. return $this->getOne($sQuery);
  258. }
  259. public function getTranscodedFileData($mixedHandler)
  260. {
  261. $sQuery = $this->prepare("SELECT `data` FROM {$this->_sTableFiles} WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $this->_aObject['object'], (string)$mixedHandler);
  262. $s = $this->getOne($sQuery);
  263. if (!$s)
  264. return false;
  265. return unserialize($s);
  266. }
  267. public function updateTranscodedFileData($mixedHandler, $mixedData)
  268. {
  269. $sData = $mixedData ? serialize($mixedData) : '';
  270. $sQuery = $this->prepare("UPDATE {$this->_sTableFiles} SET `data` = ? WHERE `transcoder_object` = ? AND `handler` = ? LIMIT 1", $sData, $this->_aObject['object'], (string)$mixedHandler);
  271. return $this->res($sQuery);
  272. }
  273. }
  274. /** @} */