job_scheduler.install 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Schema definitions install/update/uninstall hooks.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function job_scheduler_schema() {
  10. $schema = array();
  11. $schema['job_schedule'] = array(
  12. 'description' => 'Schedule of jobs to be executed.',
  13. 'fields' => array(
  14. 'item_id' => array(
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. 'description' => 'Primary Key: Unique item ID.',
  19. ),
  20. 'name' => array(
  21. 'type' => 'varchar',
  22. 'length' => 128,
  23. 'not null' => TRUE,
  24. 'default' => '',
  25. 'description' => 'Name of the schedule.',
  26. ),
  27. 'type' => array(
  28. 'type' => 'varchar',
  29. 'length' => 128,
  30. 'not null' => TRUE,
  31. 'default' => '',
  32. 'description' => 'Type identifier of the job.',
  33. ),
  34. 'id' => array(
  35. 'type' => 'int',
  36. 'not null' => TRUE,
  37. 'default' => 0,
  38. 'unsigned' => TRUE,
  39. 'description' => 'Numeric identifier of the job.',
  40. ),
  41. 'period' => array(
  42. 'type' => 'int',
  43. 'unsigned' => TRUE,
  44. 'default' => 0,
  45. 'not null' => TRUE,
  46. 'description' => 'Time period after which job is to be executed.',
  47. ),
  48. 'crontab' => array(
  49. 'type' => 'varchar',
  50. 'length' => 255,
  51. 'not null' => TRUE,
  52. 'default' => '',
  53. 'description' => 'Crontab line in *NIX format.',
  54. ),
  55. 'data' => array(
  56. 'type' => 'blob',
  57. 'not null' => FALSE,
  58. 'size' => 'big',
  59. 'serialize' => TRUE,
  60. 'description' => 'The arbitrary data for the item.',
  61. ),
  62. 'expire' => array(
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. 'description' => 'Timestamp when job expires.',
  67. ),
  68. 'created' => array(
  69. 'type' => 'int',
  70. 'not null' => TRUE,
  71. 'default' => 0,
  72. 'description' => 'Timestamp when the item was created.',
  73. ),
  74. 'last' => array(
  75. 'type' => 'int',
  76. 'unsigned' => TRUE,
  77. 'default' => 0,
  78. 'not null' => TRUE,
  79. 'description' => 'Timestamp when a job was last executed.',
  80. ),
  81. 'periodic' => array(
  82. 'type' => 'int',
  83. 'size' => 'small',
  84. 'unsigned' => TRUE,
  85. 'default' => 0,
  86. 'not null' => TRUE,
  87. 'description' => 'If true job will be automatically rescheduled.',
  88. ),
  89. 'next' => array(
  90. 'type' => 'int',
  91. 'unsigned' => TRUE,
  92. 'default' => 0,
  93. 'not null' => TRUE,
  94. 'description' => 'Timestamp when a job is to be executed (next = last + period), used for fast ordering.',
  95. ),
  96. 'scheduled' => array(
  97. 'type' => 'int',
  98. 'unsigned' => TRUE,
  99. 'default' => 0,
  100. 'not null' => TRUE,
  101. 'description' => 'Timestamp when a job was scheduled. 0 if a job is currently not scheduled.',
  102. ),
  103. ),
  104. 'primary key' => array('item_id'),
  105. 'indexes' => array(
  106. 'name_type_id' => array('name', 'type', 'id'),
  107. 'name_type' => array('name', 'type'),
  108. 'next' => array('next'),
  109. 'scheduled' => array('scheduled'),
  110. ),
  111. );
  112. return $schema;
  113. }
  114. /**
  115. * Fix indexes on next.
  116. */
  117. function job_scheduler_update_6101() {
  118. $ret = array();
  119. db_drop_index($ret, 'job_schedule', 'last_period');
  120. db_drop_index($ret, 'job_schedule', 'periodic');
  121. db_add_index($ret, 'job_schedule', 'next', array('next'));
  122. return $ret;
  123. }
  124. /**
  125. * Rename 'callback' to 'name' field.
  126. */
  127. function job_scheduler_update_7100() {
  128. db_drop_index('job_schedule', 'callback_type_id');
  129. db_drop_index('job_schedule', 'callback_type');
  130. $spec = array(
  131. 'type' => 'varchar',
  132. 'length' => 128,
  133. 'not null' => TRUE,
  134. 'default' => '',
  135. 'description' => 'Name of the schedule.',
  136. );
  137. db_change_field('job_schedule', 'callback', 'name', $spec);
  138. db_add_index('job_schedule', 'name_type_id', array('name', 'type', 'id'));
  139. db_add_index('job_schedule', 'name_type', array('name', 'type'));
  140. }
  141. /**
  142. * Add fields: item_id, crontab, data, expire
  143. */
  144. function job_scheduler_update_7101() {
  145. $spec = array(
  146. 'type' => 'varchar',
  147. 'length' => 255,
  148. 'not null' => TRUE,
  149. 'default' => '',
  150. 'description' => 'Crontab line in *NIX format.',
  151. );
  152. db_add_field('job_schedule', 'crontab', $spec);
  153. $spec = array(
  154. 'type' => 'blob',
  155. 'not null' => FALSE,
  156. 'size' => 'big',
  157. 'serialize' => TRUE,
  158. 'description' => 'The arbitrary data for the item.',
  159. );
  160. db_add_field('job_schedule', 'data', $spec);
  161. $spec = array(
  162. 'type' => 'int',
  163. 'not null' => TRUE,
  164. 'default' => 0,
  165. 'description' => 'Timestamp when job expires.',
  166. );
  167. db_add_field('job_schedule', 'expire', $spec);
  168. $spec = array(
  169. 'type' => 'serial',
  170. 'unsigned' => TRUE,
  171. 'not null' => TRUE,
  172. 'description' => 'Primary Key: Unique item ID.',
  173. );
  174. db_add_field('job_schedule', 'item_id', $spec, array('primary key' => array('item_id')));
  175. }