elysia_cron.api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Elysia cron module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * You can extend cron functionality in you modules by using elysia_cron api.
  12. *
  13. * With it you can:
  14. * - have more than one cron job per module
  15. * - have a different schedule rule for each cron job defined
  16. * - set a description for each cron job.
  17. *
  18. * To do this you should add in you module a new hook. This is the syntax:
  19. *
  20. * - 'key' is the identifier for the task you are defining.
  21. * You can define a timing for the standard cron hook of the module by using
  22. * the "MODULENAME_cron" key. (See examples).
  23. *
  24. * - description:
  25. * a textual description of the job, used in elysia cron's status
  26. * page. Use the untranslated string, without the "t()" wrapper (elysia_cron
  27. * will apply it)
  28. *
  29. * - rule:
  30. * the crontab rule. For example: "0 * * * *" to execute the task every hour.
  31. *
  32. * - weight (optional):
  33. * a numerical value to define order of execution. (Default:0)
  34. *
  35. * - callback (optional):
  36. * you can define here a name of a PHP function that should
  37. * by called to execute the task. This is not mandatory: if you don't specify
  38. * it Elysia cron will search for a function called like the task KEY.
  39. * If this function is not found, Elysia cron will call the "hook_cronapi"
  40. * function with $op = 'execute' and $job = 'KEY' (the key of the task).
  41. * (See examples)
  42. *
  43. * - arguments (optional):
  44. * an array of arguments passed to callback (only if callback is defined).
  45. *
  46. * - file/file path:
  47. * the PHP file that contains the callback (hook_menu's syntax).
  48. *
  49. * @param string $op
  50. * Operation: "list" or "execute".
  51. * @param string|null $job
  52. * Name of current job or it is NULL if we define job list.
  53. *
  54. * @return array
  55. * Job list.
  56. */
  57. function hook_cronapi($op, $job = NULL) {
  58. // General example of all parameters.
  59. $items['key'] = array(
  60. 'description' => 'string',
  61. 'rule' => 'string',
  62. 'weight' => 1234,
  63. 'callback' => 'function_name',
  64. 'arguments' => array('first', 'second', 3),
  65. // External file, like in hook_menu.
  66. 'file' => 'string',
  67. 'file path' => 'string',
  68. );
  69. // Run function example_sendmail_cron() every 2 hours.
  70. // Note: i don't need to define a callback, i'll use "example_sendmail_cron"
  71. // function.
  72. $items['example_sendmail_cron'] = array(
  73. 'description' => 'Send mail with news',
  74. 'rule' => '0 */2 * * *',
  75. );
  76. // Run example_news_fetch('all') every 5 minutes.
  77. // Note: this function has argument.
  78. $items['example_news_cron'] = array(
  79. 'description' => 'Send mail with news',
  80. 'rule' => '*/5 * * * *',
  81. 'callback' => 'example_news_fetch',
  82. 'arguments' => array('all'),
  83. );
  84. // Definition of rules list and embedded code.
  85. if ($op == 'list') {
  86. // Rules list.
  87. $items['job1'] = array(
  88. 'description' => 'Send mail with news',
  89. 'rule' => '0 */2 * * *',
  90. );
  91. $items['job2'] = array(
  92. 'description' => 'Send mail with news',
  93. 'rule' => '*/5 * * * *',
  94. );
  95. }
  96. elseif ($op == 'execute') {
  97. // Embedded code.
  98. switch ($job) {
  99. case 'job1':
  100. // ... job1 code.
  101. break;
  102. case 'job2':
  103. // ... job2 code.
  104. break;
  105. }
  106. }
  107. return $items;
  108. }
  109. /**
  110. * Altering hook_cron definition.
  111. *
  112. * You can use the "hook_cron_alter" function to edit cronapi data of other
  113. * modules.
  114. *
  115. * @param array $data
  116. * Array of cron rules.
  117. */
  118. function hook_cron_alter(&$data) {
  119. $data['key']['rule'] = '0 */6 * * *';
  120. }
  121. /**
  122. * @} End of "addtogroup hooks".
  123. */