actualize_script.php 3.8 KB

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