actualize_script.php 4.0 KB

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