ultimate_cron.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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' => t('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. 'msg' => array(
  100. 'description' => 'Message',
  101. 'type' => 'text',
  102. 'not null' => FALSE,
  103. ),
  104. ),
  105. 'primary key' => array('lid'),
  106. 'indexes' => array(
  107. 'idx_last' => array('name', 'start_stamp'),
  108. 'idx_count' => array('name', 'end_stamp'),
  109. ),
  110. );
  111. return $schema;
  112. }
  113. /**
  114. * Implements hook_enable().
  115. */
  116. function ultimate_cron_enable() {
  117. // Disable built-in poor mans cron
  118. variable_set('cron_safe_threshold', 0);
  119. }
  120. /**
  121. * Implements hook_uninstall().
  122. */
  123. function ultimate_cron_uninstall() {
  124. variable_del('ultimate_cron_simultaneous_connections');
  125. variable_del('ultimate_cron_rule');
  126. variable_del('ultimate_cron_cleanup_log');
  127. variable_del('ultimate_cron_catch_up');
  128. variable_del('ultimate_cron_queue_polling_latency');
  129. variable_del('ultimate_cron_queue_lease_time');
  130. variable_del('ultimate_cron_poorman');
  131. variable_del('ultimate_cron_launch_window');
  132. variable_del('ultimate_cron_max_execution_time');
  133. }
  134. /**
  135. * Implements hook_requirements().
  136. */
  137. function ultimate_cron_requirements($phase) {
  138. $response = array();
  139. switch ($phase) {
  140. case 'install':
  141. return $response;
  142. case 'runtime':
  143. $response['title'] = 'Ultimate Cron';
  144. $response['value'] = 'OK';
  145. $response['severity'] = REQUIREMENT_OK;
  146. if ($modules = _ultimate_cron_incompatible_modules()) {
  147. $response['severity'] = REQUIREMENT_ERROR;
  148. $response['value'] = t('Ultimate Cron is DISABLED!');
  149. $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')));
  150. }
  151. $result = array();
  152. $result['ultimate_cron'] = $response;
  153. return $result;
  154. }
  155. }
  156. /**
  157. * Major version upgrade of Drupal
  158. */
  159. function ultimate_cron_update_7000(&$context) {
  160. $context['sandbox']['major_version_upgrade'] = array(
  161. 7101 => TRUE,
  162. 7102 => TRUE,
  163. 7103 => TRUE,
  164. 7104 => TRUE,
  165. 7105 => TRUE,
  166. 7106 => TRUE,
  167. 7107 => TRUE,
  168. );
  169. }
  170. /**
  171. * Move messages to log table.
  172. */
  173. function ultimate_cron_update_7101(&$context) {
  174. if (!empty($context['sandbox']['major_version_upgrade'][7101])) {
  175. // This udate is already part of latest 6.x
  176. return;
  177. }
  178. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  179. if ($schema_version >= 7000) {
  180. // Old hook_update_N was 7000
  181. return;
  182. }
  183. db_add_field('ultimate_cron_log', 'msg', array(
  184. 'description' => 'Message',
  185. 'type' => 'text',
  186. 'not null' => FALSE,
  187. ));
  188. db_query("UPDATE {ultimate_cron_log} l JOIN {ultimate_cron_log_message} m ON l.lid = m.lid SET l.msg = m.msg");
  189. db_drop_table('ultimate_cron_log_message');
  190. }
  191. /**
  192. * Convert polling latenct from microseconds to miliseconds.
  193. */
  194. function ultimate_cron_update_7102(&$context) {
  195. if (!empty($context['sandbox']['major_version_upgrade'][7102])) {
  196. // This udate is already part of latest 6.x
  197. return;
  198. }
  199. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  200. if ($schema_version >= 7001) {
  201. // Old hook_update_N was 7001
  202. return;
  203. }
  204. // Convert polling latency from microseconds to miliseconds.
  205. $polling_latency = variable_get('ultimate_cron_queue_polling_latency', NULL);
  206. if ($polling_latency) {
  207. $polling_latency /= 1000;
  208. variable_set('ultimate_cron_queue_polling_latency', $polling_latency);
  209. }
  210. }
  211. /**
  212. * Merge ultimate_cron_function and ultimate_cron_configuration into ultimate_cron
  213. */
  214. function ultimate_cron_update_7103(&$context) {
  215. if (!empty($context['sandbox']['major_version_upgrade'][7103])) {
  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 >= 7002) {
  221. // Old hook_update_N was 7002
  222. return;
  223. }
  224. $schema = ultimate_cron_schema();
  225. db_create_table('ultimate_cron', $schema['ultimate_cron']);
  226. 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");
  227. db_drop_table('ultimate_cron_function');
  228. db_drop_table('ultimate_cron_configuration');
  229. }
  230. /**
  231. * Switch from fid to function name in ultimate_cron_log
  232. */
  233. function ultimate_cron_update_7104(&$context) {
  234. if (!empty($context['sandbox']['major_version_upgrade'][7104])) {
  235. // This udate is already part of latest 6.x
  236. return;
  237. }
  238. $schema_version = (int)(db_query("SELECT schema_version FROM {system} WHERE name = 'ultimate_cron'")->fetchField());
  239. if ($schema_version >= 7003) {
  240. // Old hook_update_N was 7003
  241. return;
  242. }
  243. db_add_field('ultimate_cron_log', 'function', array(
  244. 'description' => 'Function name',
  245. 'type' => 'varchar',
  246. 'length' => 127,
  247. 'not null' => TRUE,
  248. 'default' => '',
  249. 'initial' => 'function',
  250. ));
  251. $fids = db_select('ultimate_cron_log', 'u')->fields('u', array('fid'))->distinct()->execute();
  252. while ($fid = $fids->fetchObject()) {
  253. $function = db_select('ultimate_cron', 'u')->fields('u', array('function'))->condition('fid', $fid->fid, '=')->execute()->fetchObject();
  254. db_update('ultimate_cron_log')->fields(array('function' => $function->function))->condition('fid', $fid->fid, '=')->execute();
  255. }
  256. db_drop_field('ultimate_cron_log', 'fid');
  257. db_drop_index('ultimate_cron_log', 'idx_last');
  258. db_drop_index('ultimate_cron_log', 'idx_count');
  259. db_add_index('ultimate_cron_log', 'idx_last', array('function', 'start'));
  260. db_add_index('ultimate_cron_log', 'idx_count', array('function', 'end'));
  261. }
  262. /**
  263. * Add missing index to database
  264. */
  265. function ultimate_cron_update_7105(&$context) {
  266. if (!empty($context['sandbox']['major_version_upgrade'][7105])) {
  267. // This udate is already part of latest 6.x
  268. return;
  269. }
  270. $items = db_select('ultimate_cron')
  271. ->fields('ultimate_cron', array('fid', 'function'))
  272. ->orderBy('fid', 'ASC')
  273. ->execute()
  274. ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
  275. $fids = array();
  276. $keep = array();
  277. foreach ($items as $item) {
  278. if (isset($keep[$item['function']])) {
  279. $fids[] = $keep[$item['function']];
  280. }
  281. $keep[$item['function']] = $item['fid'];
  282. }
  283. if ($fids) {
  284. db_delete('ultimate_cron')
  285. ->condition('fid', $fids)
  286. ->execute();
  287. }
  288. db_add_unique_key('ultimate_cron', 'uniq_function', array('function'));
  289. }
  290. /**
  291. * Change schema to SQL 99 compliance
  292. */
  293. function ultimate_cron_update_7106(&$context) {
  294. if (!empty($context['sandbox']['major_version_upgrade'][7106])) {
  295. // This udate is already part of latest 6.x
  296. return;
  297. }
  298. db_drop_unique_key('ultimate_cron', 'idx_function');
  299. db_change_field('ultimate_cron', 'function', 'name', array(
  300. 'description' => 'Function name',
  301. 'type' => 'varchar',
  302. 'length' => 127,
  303. 'not null' => TRUE,
  304. 'default' => '',
  305. ));
  306. db_add_unique_key('ultimate_cron', 'idx_name', array('name'));
  307. db_change_field('ultimate_cron_log', 'function', 'name', array(
  308. 'description' => 'Function name',
  309. 'type' => 'varchar',
  310. 'length' => 127,
  311. 'not null' => TRUE,
  312. 'default' => '',
  313. ));
  314. db_change_field('ultimate_cron_log', 'start', 'start_stamp', array(
  315. 'description' => 'Timstamp of execution start',
  316. 'type' => 'float',
  317. 'size' => 'big',
  318. 'not null' => TRUE,
  319. 'default' => 0,
  320. ));
  321. db_change_field('ultimate_cron_log', 'end', 'end_stamp', array(
  322. 'description' => 'Timstamp of execution end',
  323. 'type' => 'float',
  324. 'size' => 'big',
  325. 'not null' => TRUE,
  326. 'default' => 0,
  327. ));
  328. db_change_field('ultimate_cron_log', 'status', 'exec_status', array(
  329. 'description' => 'Status of the execution',
  330. 'type' => 'int',
  331. 'size' => 'normal',
  332. 'not null' => FALSE,
  333. 'default' => NULL,
  334. ));
  335. }
  336. /**
  337. * Add service host to log.
  338. */
  339. function ultimate_cron_update_7107(&$context) {
  340. if (!empty($context['sandbox']['major_version_upgrade'][7107])) {
  341. // This udate is already part of latest 6.x
  342. return;
  343. }
  344. db_add_field('ultimate_cron_log', 'service_host', array(
  345. 'description' => 'Service host',
  346. 'type' => 'varchar',
  347. 'length' => 127,
  348. 'not null' => TRUE,
  349. 'default' => '',
  350. ));
  351. }