actualize_script.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env php
  2. <?php
  3. require(__DIR__ . '/../cli/_cli.php');
  4. session_cache_limiter('');
  5. ob_implicit_flush(false);
  6. ob_start();
  7. $begin_date = date_create('now');
  8. // Set the header params ($_GET) to call the FRSS application.
  9. $_GET['c'] = 'feed';
  10. $_GET['a'] = 'actualize';
  11. $_GET['ajax'] = 1;
  12. $_GET['force'] = true;
  13. $_SERVER['HTTP_HOST'] = '';
  14. $app = new FreshRSS();
  15. FreshRSS_Context::initSystem();
  16. FreshRSS_Context::$system_conf->auth_type = 'none'; // avoid necessity to be logged in (not saved!)
  17. define('SIMPLEPIE_SYSLOG_ENABLED', FreshRSS_Context::$system_conf->simplepie_syslog_enabled);
  18. /**
  19. * Writes to FreshRSS admin log, and if it is not already done by default,
  20. * writes to syslog (only if simplepie_syslog_enabled in FreshRSS configuration) and to STDOUT
  21. */
  22. function notice(string $message): void {
  23. Minz_Log::notice($message, ADMIN_LOG);
  24. // @phpstan-ignore-next-line
  25. if (!COPY_LOG_TO_SYSLOG && SIMPLEPIE_SYSLOG_ENABLED) {
  26. syslog(LOG_NOTICE, $message);
  27. }
  28. // @phpstan-ignore-next-line
  29. if (defined('STDOUT') && !COPY_SYSLOG_TO_STDERR) {
  30. fwrite(STDOUT, $message . "\n"); //Unbuffered
  31. }
  32. }
  33. // <Mutex>
  34. // Avoid having multiple actualization processes at the same time
  35. $mutexFile = TMP_PATH . '/actualize.freshrss.lock';
  36. $mutexTtl = 900; // seconds (refreshed before each new feed)
  37. if (file_exists($mutexFile) && ((time() - @filemtime($mutexFile)) > $mutexTtl)) {
  38. unlink($mutexFile);
  39. }
  40. if (($handle = @fopen($mutexFile, 'x')) === false) {
  41. notice('FreshRSS feeds actualization was already running, so aborting new run at ' . $begin_date->format('c'));
  42. die();
  43. }
  44. fclose($handle);
  45. register_shutdown_function(function () use ($mutexFile) {
  46. unlink($mutexFile);
  47. });
  48. Minz_ExtensionManager::addHook('feed_before_actualize', function ($feed) use ($mutexFile) {
  49. touch($mutexFile);
  50. return $feed;
  51. });
  52. // </Mutex>
  53. notice('FreshRSS starting feeds actualization at ' . $begin_date->format('c'));
  54. // make sure the PHP setup of the CLI environment is compatible with FreshRSS as well
  55. echo 'Failed requirements!', "\n";
  56. performRequirementCheck(FreshRSS_Context::$system_conf->db['type']);
  57. ob_clean();
  58. echo 'Results: ', "\n"; //Buffered
  59. // Create the list of users to actualize.
  60. // Users are processed in a random order but always start with default user
  61. $users = listUsers();
  62. shuffle($users);
  63. if (FreshRSS_Context::$system_conf->default_user !== '') {
  64. array_unshift($users, FreshRSS_Context::$system_conf->default_user);
  65. $users = array_unique($users);
  66. }
  67. $limits = FreshRSS_Context::$system_conf->limits;
  68. $min_last_activity = time() - $limits['max_inactivity'];
  69. foreach ($users as $user) {
  70. FreshRSS_Context::initUser($user);
  71. if (FreshRSS_Context::$user_conf == null) {
  72. notice('Invalid user ' . $user);
  73. continue;
  74. }
  75. if (!FreshRSS_Context::$user_conf->enabled) {
  76. notice('FreshRSS skip disabled user ' . $user);
  77. continue;
  78. }
  79. if (($user !== FreshRSS_Context::$system_conf->default_user) &&
  80. (FreshRSS_UserDAO::mtime($user) < $min_last_activity)) {
  81. notice('FreshRSS skip inactive user ' . $user);
  82. continue;
  83. }
  84. FreshRSS_Auth::giveAccess();
  85. Minz_ExtensionManager::callHook('freshrss_user_maintenance');
  86. $app->init();
  87. notice('FreshRSS actualize ' . $user . '…');
  88. echo $user, ' '; //Buffered
  89. $app->run();
  90. if (!invalidateHttpCache()) {
  91. Minz_Log::warning('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME), ADMIN_LOG);
  92. if (defined('STDERR')) {
  93. fwrite(STDERR, 'FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME) . "\n");
  94. }
  95. }
  96. gc_collect_cycles();
  97. }
  98. $end_date = date_create('now');
  99. $duration = date_diff($end_date, $begin_date);
  100. notice('FreshRSS actualization done for ' . count($users) .
  101. ' users, using ' . format_bytes(memory_get_peak_usage(true)) . ' of memory, in ' .
  102. $duration->format('%a day(s), %h hour(s), %i minute(s) and %s seconds.'));
  103. echo 'End.', "\n";
  104. ob_end_flush();