/upload/includes/cron/cron_init.php

http://torrentpier2.googlecode.com/ · PHP · 67 lines · 46 code · 13 blank · 8 comment · 8 complexity · a7ad820fda9f9804fb61284a00401a18 MD5 · raw file

  1. <?php
  2. if (!defined('BB_ROOT')) die(basename(__FILE__));
  3. //
  4. // Functions
  5. //
  6. function cron_get_file_lock ()
  7. {
  8. $lock_obtained = false;
  9. if (file_exists(CRON_ALLOWED))
  10. {
  11. # bb_log(date('H:i:s - ') . getmypid() .' -x-- FILE-LOCK try'. LOG_LF, CRON_LOG_DIR .'cron_check');
  12. $lock_obtained = @rename(CRON_ALLOWED, CRON_RUNNING);
  13. }
  14. elseif (file_exists(CRON_RUNNING))
  15. {
  16. cron_release_deadlock();
  17. }
  18. elseif (!file_exists(CRON_ALLOWED) && !file_exists(CRON_RUNNING))
  19. {
  20. file_write('', CRON_ALLOWED);
  21. $lock_obtained = @rename(CRON_ALLOWED, CRON_RUNNING);
  22. }
  23. return $lock_obtained;
  24. }
  25. function cron_track_running ($mode)
  26. {
  27. @define('CRON_STARTMARK', TRIGGERS_DIR .'cron_started_at_'. date('Y-m-d_H-i-s') .'_by_pid_'. getmypid());
  28. if ($mode == 'start')
  29. {
  30. cron_touch_lock_file(CRON_RUNNING);
  31. file_write('', CRON_STARTMARK);
  32. }
  33. elseif ($mode == 'end')
  34. {
  35. @unlink(CRON_STARTMARK);
  36. }
  37. }
  38. //
  39. // Run cron
  40. //
  41. if (cron_get_file_lock())
  42. {
  43. ignore_user_abort(true);
  44. register_shutdown_function('cron_release_file_lock');
  45. register_shutdown_function('cron_enable_board');
  46. # bb_log(date('H:i:s - ') . getmypid() .' --x- FILE-LOCK OBTAINED ###############'. LOG_LF, CRON_LOG_DIR .'cron_check');
  47. cron_track_running('start');
  48. require(CRON_DIR .'cron_check.php');
  49. cron_track_running('end');
  50. }
  51. if (defined('IN_CRON'))
  52. {
  53. bb_log(date('H:i:s - ') . getmypid() .' --x- ALL jobs FINISHED *************************************************'. LOG_LF, CRON_LOG_DIR .'cron_check');
  54. }