ultimate_cron.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Installation file for Ultimate Cron
  6. */
  7. /**
  8. * Implements hook_schema().
  9. */
  10. function ultimate_cron_schema() {
  11. $schema = array();
  12. $schema['ultimate_cron'] = array(
  13. 'description' => 'Cron job function list',
  14. 'export' => array(
  15. 'key' => 'name',
  16. 'key name' => 'Function name',
  17. 'primary key' => 'fid',
  18. 'identifier' => 'name',
  19. 'default hook' => 'default_ultimate_cron_function',
  20. 'save callback' => 'ultimate_cron_crud_save',
  21. 'api' => array(
  22. 'owner' => 'ultimate_cron',
  23. 'api' => 'default_ultimate_cron_functions',
  24. 'minimum_version' => 1,
  25. 'current_version' => 1,
  26. ),
  27. ),
  28. 'fields' => array(
  29. 'fid' => array(
  30. 'description' => 'Function ID',
  31. 'type' => 'serial',
  32. 'size' => 'normal',
  33. 'not null' => TRUE,
  34. 'no export' => TRUE,
  35. ),
  36. 'name' => array(
  37. 'description' => 'Function name',
  38. 'type' => 'varchar',
  39. 'length' => 127,
  40. 'not null' => TRUE,
  41. 'default' => '',
  42. ),
  43. 'settings' => array(
  44. 'description' => 'Settings',
  45. 'type' => 'text',
  46. 'serialize' => TRUE,
  47. 'not null' => FALSE,
  48. ),
  49. ),
  50. 'primary key' => array('fid'),
  51. 'unique keys' => array(
  52. 'uniq_name' => array('name')
  53. ),
  54. );
  55. $schema['ultimate_cron_log'] = array(
  56. 'description' => 'Logs',
  57. 'fields' => array(
  58. 'lid' => array(
  59. 'description' => 'Log ID',
  60. 'type' => 'serial',
  61. 'size' => 'normal',
  62. 'not null' => TRUE,
  63. ),
  64. 'name' => array(
  65. 'description' => 'Function name',
  66. 'type' => 'varchar',
  67. 'length' => 127,
  68. 'not null' => TRUE,
  69. 'default' => '',
  70. ),
  71. 'start_stamp' => array(
  72. 'description' => 'Timstamp of execution start',
  73. 'type' => 'float',
  74. 'size' => 'big',
  75. 'not null' => TRUE,
  76. 'default' => 0,
  77. ),
  78. 'end_stamp' => array(
  79. 'description' => 'Timstamp of execution end',
  80. 'type' => 'float',
  81. 'size' => 'big',
  82. 'not null' => TRUE,
  83. 'default' => 0,
  84. ),
  85. 'service_host' => array(
  86. 'description' => 'Service host',
  87. 'type' => 'varchar',
  88. 'length' => 127,
  89. 'not null' => TRUE,
  90. 'default' => '',
  91. ),
  92. 'exec_status' => array(
  93. 'description' => 'Status of the execution',
  94. 'type' => 'int',
  95. 'size' => 'normal',
  96. 'not null' => FALSE,
  97. 'default' => NULL,
  98. ),
  99. 'severity' => array(
  100. 'description' => 'Log level severity',
  101. 'type' => 'int',
  102. 'size' => 'normal',
  103. 'not null' => TRUE,
  104. 'default' => -1,
  105. ),
  106. 'msg' => array(
  107. 'description' => 'Message',
  108. 'type' => 'text',
  109. 'not null' => FALSE,
  110. ),
  111. ),
  112. 'primary key' => array('lid'),
  113. 'indexes' => array(
  114. 'idx_last' => array('name', 'start_stamp'),
  115. 'idx_count' => array('name', 'end_stamp'),
  116. ),
  117. );
  118. return $schema;
  119. }
  120. /**
  121. * Implements hook_enable().
  122. */
  123. function ultimate_cron_enable() {
  124. // Disable built-in poor mans cron
  125. variable_set('cron_safe_threshold', 0);
  126. }
  127. /**
  128. * Implements hook_uninstall().
  129. */
  130. function ultimate_cron_uninstall() {
  131. variable_del('ultimate_cron_simultaneous_connections');
  132. variable_del('ultimate_cron_rule');
  133. variable_del('ultimate_cron_cleanup_log');
  134. variable_del('ultimate_cron_catch_up');
  135. variable_del('ultimate_cron_queue_polling_latency');
  136. variable_del('ultimate_cron_queue_lease_time');
  137. variable_del('ultimate_cron_poorman');
  138. variable_del('ultimate_cron_launch_window');
  139. variable_del('ultimate_cron_max_execution_time');
  140. }
  141. /**
  142. * Implements hook_requirements().
  143. */
  144. function ultimate_cron_requirements($phase) {
  145. $response = array();
  146. switch ($phase) {
  147. case 'install':
  148. return $response;
  149. case 'runtime':
  150. $t = get_t();
  151. $response['title'] = 'Ultimate Cron';
  152. $response['value'] = $t('OK');
  153. $response['severity'] = REQUIREMENT_OK;
  154. if ($modules = _ultimate_cron_incompatible_modules()) {
  155. $response['severity'] = REQUIREMENT_ERROR;
  156. $response['value'] = $t('Ultimate Cron is DISABLED!');
  157. $response['description'] = $t('%modules is installed on the system, but is incompatible with Ultimate Cron.<br/>Please disable the modules %modules.<br/>You might want to !url settings first.', array('%modules' => join(', ', $modules), '!url' => l(t('import'), 'admin/settings/cron/import')));
  158. }
  159. if (ini_get('safe_mode')) {
  160. $desc = $t('Safe mode enabled. Ultimate Cron is unable to control maximum execution time for cron jobs. This may cause cron jobs to end prematurely.');
  161. if ($response['severity'] < REQUIREMENT_WARNING) {
  162. $response['severity'] = REQUIREMENT_WARNING;
  163. $response['value'] = $t('Safe mode enabled');
  164. $response['description'] = $desc;
  165. }
  166. else {
  167. $response['description'] .= '<br/>' . $desc;
  168. }
  169. }
  170. $result = array();
  171. $result['ultimate_cron'] = $response;
  172. return $result;
  173. }
  174. }
  175. /**
  176. * Major version upgrade of Drupal
  177. */
  178. function ultimate_cron_update_7000(&$context) {
  179. $context['sandbox']['major_version_upgrade'] = array(
  180. 7101 => TRUE,
  181. 7102 => TRUE,
  182. 7103 => TRUE,
  183. 7104 => TRUE,
  184. 7105 => TRUE,
  185. 7106 => TRUE,
  186. 7107 => TRUE,
  187. 7108 => FALSE,
  188. );
  189. }
  190. /**
  191. * Move messages to log table.
  192. */
  193. function ultimate_cron_update_7101(&$context) {
  194. if (!empty($context['sandbox']['major_version_upgrade'][7101])) {
  195. // This udate is already part of latest 6.x
  196. return;
  197. }
  198. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  199. if ($schema_version >= 7000) {
  200. // Old hook_update_N was 7000
  201. return;
  202. }
  203. db_add_field('ultimate_cron_log', 'msg', array(
  204. 'description' => 'Message',
  205. 'type' => 'text',
  206. 'not null' => FALSE,
  207. ));
  208. db_query("UPDATE {ultimate_cron_log} l JOIN {ultimate_cron_log_message} m ON l.lid = m.lid SET l.msg = m.msg");
  209. db_drop_table('ultimate_cron_log_message');
  210. }
  211. /**
  212. * Convert polling latenct from microseconds to miliseconds.
  213. */
  214. function ultimate_cron_update_7102(&$context) {
  215. if (!empty($context['sandbox']['major_version_upgrade'][7102])) {
  216. // This udate is already part of latest 6.x
  217. return;
  218. }
  219. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  220. if ($schema_version >= 7001) {
  221. // Old hook_update_N was 7001
  222. return;
  223. }
  224. // Convert polling latency from microseconds to miliseconds.
  225. $polling_latency = variable_get('ultimate_cron_queue_polling_latency', NULL);
  226. if ($polling_latency) {
  227. $polling_latency /= 1000;
  228. variable_set('ultimate_cron_queue_polling_latency', $polling_latency);
  229. }
  230. }
  231. /**
  232. * Merge ultimate_cron_function and ultimate_cron_configuration into ultimate_cron
  233. */
  234. function ultimate_cron_update_7103(&$context) {
  235. if (!empty($context['sandbox']['major_version_upgrade'][7103])) {
  236. // This udate is already part of latest 6.x
  237. return;
  238. }
  239. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  240. if ($schema_version >= 7002) {
  241. // Old hook_update_N was 7002
  242. return;
  243. }
  244. $schema = ultimate_cron_schema();
  245. db_create_table('ultimate_cron', $schema['ultimate_cron']);
  246. db_query("INSERT INTO {ultimate_cron} SELECT f.fid, f.function, c.configuration AS settings FROM ultimate_cron_function f LEFT JOIN {ultimate_cron_configuration} c ON f.fid = c.fid");
  247. db_drop_table('ultimate_cron_function');
  248. db_drop_table('ultimate_cron_configuration');
  249. }
  250. /**
  251. * Switch from fid to function name in ultimate_cron_log
  252. */
  253. function ultimate_cron_update_7104(&$context) {
  254. if (!empty($context['sandbox']['major_version_upgrade'][7104])) {
  255. // This udate is already part of latest 6.x
  256. return;
  257. }
  258. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  259. if ($schema_version >= 7003) {
  260. // Old hook_update_N was 7003
  261. return;
  262. }
  263. db_add_field('ultimate_cron_log', 'function', array(
  264. 'description' => 'Function name',
  265. 'type' => 'varchar',
  266. 'length' => 127,
  267. 'not null' => TRUE,
  268. 'default' => '',
  269. 'initial' => 'function',
  270. ));
  271. $fids = db_select('ultimate_cron_log', 'u')->fields('u', array('fid'))->distinct()->execute();
  272. while ($fid = $fids->fetchObject()) {
  273. $function = db_select('ultimate_cron', 'u')->fields('u', array('function'))->condition('fid', $fid->fid, '=')->execute()->fetchObject();
  274. db_update('ultimate_cron_log')->fields(array('function' => $function->function))->condition('fid', $fid->fid, '=')->execute();
  275. }
  276. db_drop_field('ultimate_cron_log', 'fid');
  277. db_drop_index('ultimate_cron_log', 'idx_last');
  278. db_drop_index('ultimate_cron_log', 'idx_count');
  279. db_add_index('ultimate_cron_log', 'idx_last', array('function', 'start'));
  280. db_add_index('ultimate_cron_log', 'idx_count', array('function', 'end'));
  281. }
  282. /**
  283. * Add missing index to database
  284. */
  285. function ultimate_cron_update_7105(&$context) {
  286. if (!empty($context['sandbox']['major_version_upgrade'][7105])) {
  287. // This udate is already part of latest 6.x
  288. return;
  289. }
  290. $items = db_select('ultimate_cron')
  291. ->fields('ultimate_cron', array('fid', 'function'))
  292. ->orderBy('fid', 'ASC')
  293. ->execute()
  294. ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
  295. $fids = array();
  296. $keep = array();
  297. foreach ($items as $item) {
  298. if (isset($keep[$item['function']])) {
  299. $fids[] = $keep[$item['function']];
  300. }
  301. $keep[$item['function']] = $item['fid'];
  302. }
  303. if ($fids) {
  304. db_delete('ultimate_cron')
  305. ->condition('fid', $fids)
  306. ->execute();
  307. }
  308. db_add_unique_key('ultimate_cron', 'uniq_function', array('function'));
  309. }
  310. /**
  311. * Change schema to SQL 99 compliance
  312. */
  313. function ultimate_cron_update_7106(&$context) {
  314. if (!empty($context['sandbox']['major_version_upgrade'][7106])) {
  315. // This udate is already part of latest 6.x
  316. return;
  317. }
  318. db_drop_unique_key('ultimate_cron', 'idx_function');
  319. db_change_field('ultimate_cron', 'function', 'name', array(
  320. 'description' => 'Function name',
  321. 'type' => 'varchar',
  322. 'length' => 127,
  323. 'not null' => TRUE,
  324. 'default' => '',
  325. ));
  326. db_add_unique_key('ultimate_cron', 'idx_name', array('name'));
  327. db_change_field('ultimate_cron_log', 'function', 'name', array(
  328. 'description' => 'Function name',
  329. 'type' => 'varchar',
  330. 'length' => 127,
  331. 'not null' => TRUE,
  332. 'default' => '',
  333. ));
  334. db_change_field('ultimate_cron_log', 'start', 'start_stamp', array(
  335. 'description' => 'Timstamp of execution start',
  336. 'type' => 'float',
  337. 'size' => 'big',
  338. 'not null' => TRUE,
  339. 'default' => 0,
  340. ));
  341. db_change_field('ultimate_cron_log', 'end', 'end_stamp', array(
  342. 'description' => 'Timstamp of execution end',
  343. 'type' => 'float',
  344. 'size' => 'big',
  345. 'not null' => TRUE,
  346. 'default' => 0,
  347. ));
  348. db_change_field('ultimate_cron_log', 'status', 'exec_status', array(
  349. 'description' => 'Status of the execution',
  350. 'type' => 'int',
  351. 'size' => 'normal',
  352. 'not null' => FALSE,
  353. 'default' => NULL,
  354. ));
  355. }
  356. /**
  357. * Add service host to log.
  358. */
  359. function ultimate_cron_update_7107(&$context) {
  360. if (!empty($context['sandbox']['major_version_upgrade'][7107])) {
  361. // This udate is already part of latest 6.x
  362. return;
  363. }
  364. db_add_field('ultimate_cron_log', 'service_host', array(
  365. 'description' => 'Service host',
  366. 'type' => 'varchar',
  367. 'length' => 127,
  368. 'not null' => TRUE,
  369. 'default' => '',
  370. ));
  371. }
  372. /**
  373. * Add severity level to log.
  374. */
  375. function ultimate_cron_update_7108(&$context) {
  376. if (!empty($context['sandbox']['major_version_upgrade'][7108])) {
  377. // This udate is already part of latest 6.x
  378. return;
  379. }
  380. db_add_field('ultimate_cron_log', 'severity', array(
  381. 'description' => 'Log level severity',
  382. 'type' => 'int',
  383. 'size' => 'normal',
  384. 'not null' => TRUE,
  385. 'default' => -1,
  386. ));
  387. }