actualize_script.php 4.0 KB

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