aboutsummaryrefslogtreecommitdiff
path: root/app/actualize_script.php
blob: 4a38c13f788b1d07270332b2a73c80d1ddcccd4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env php
<?php
require(__DIR__ . '/../cli/_cli.php');

session_cache_limiter('');
ob_implicit_flush(false);
ob_start();

$begin_date = date_create('now');

// Set the header params ($_GET) to call the FRSS application.
$_GET['c'] = 'feed';
$_GET['a'] = 'actualize';
$_GET['ajax'] = 1;
$_GET['force'] = true;
$_SERVER['HTTP_HOST'] = '';

$app = new FreshRSS();

FreshRSS_Context::initSystem();
if (FreshRSS_Context::$system_conf === null) {
	throw new FreshRSS_Context_Exception('System configuration not initialised!');
}
FreshRSS_Context::$system_conf->auth_type = 'none';  // avoid necessity to be logged in (not saved!)
define('SIMPLEPIE_SYSLOG_ENABLED', FreshRSS_Context::$system_conf->simplepie_syslog_enabled);

/**
 * Writes to FreshRSS admin log, and if it is not already done by default,
 * writes to syslog (only if simplepie_syslog_enabled in FreshRSS configuration) and to STDOUT
 */
function notice(string $message): void {
	Minz_Log::notice($message, ADMIN_LOG);
	// @phpstan-ignore-next-line
	if (!COPY_LOG_TO_SYSLOG && SIMPLEPIE_SYSLOG_ENABLED) {
		syslog(LOG_NOTICE, $message);
	}
	// @phpstan-ignore-next-line
	if (defined('STDOUT') && !COPY_SYSLOG_TO_STDERR) {
		fwrite(STDOUT, $message . "\n");	//Unbuffered
	}
}

// <Mutex>
// Avoid having multiple actualization processes at the same time
$mutexFile = TMP_PATH . '/actualize.freshrss.lock';
$mutexTtl = 900; // seconds (refreshed before each new feed)
if (file_exists($mutexFile) && ((time() - @filemtime($mutexFile)) > $mutexTtl)) {
	unlink($mutexFile);
}

if (($handle = @fopen($mutexFile, 'x')) === false) {
	notice('FreshRSS feeds actualization was already running, so aborting new run at ' . $begin_date->format('c'));
	die();
}
fclose($handle);

register_shutdown_function(function () use ($mutexFile) {
	unlink($mutexFile);
});
// </Mutex>

notice('FreshRSS starting feeds actualization at ' . $begin_date->format('c'));

// make sure the PHP setup of the CLI environment is compatible with FreshRSS as well
echo 'Failed requirements!', "\n";
performRequirementCheck(FreshRSS_Context::$system_conf->db['type']);
ob_clean();

echo 'Results: ', "\n";	//Buffered

// Create the list of users to actualize.
// Users are processed in a random order but always start with default user
$users = listUsers();
shuffle($users);
if (FreshRSS_Context::$system_conf->default_user !== '') {
	array_unshift($users, FreshRSS_Context::$system_conf->default_user);
	$users = array_unique($users);
}

$limits = FreshRSS_Context::$system_conf->limits;
$min_last_activity = time() - $limits['max_inactivity'];
foreach ($users as $user) {
	FreshRSS_Context::initUser($user);
	if (FreshRSS_Context::$user_conf == null) {
		notice('Invalid user ' . $user);
		continue;
	}
	if (!FreshRSS_Context::$user_conf->enabled) {
		notice('FreshRSS skip disabled user ' . $user);
		continue;
	}
	if (($user !== FreshRSS_Context::$system_conf->default_user) &&
			(FreshRSS_UserDAO::mtime($user) < $min_last_activity)) {
		notice('FreshRSS skip inactive user ' . $user);
		continue;
	}

	FreshRSS_Auth::giveAccess();

	// NB: Extensions and hooks are reinitialised there
	$app->init();

	Minz_ExtensionManager::addHook('feed_before_actualize', function ($feed) use ($mutexFile) {
		touch($mutexFile);
		return $feed;
	});

	notice('FreshRSS actualize ' . $user . '…');
	echo $user, ' ';	//Buffered
	Minz_ExtensionManager::callHook('freshrss_user_maintenance');
	$app->run();

	if (!invalidateHttpCache()) {
		Minz_Log::warning('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME), ADMIN_LOG);
		if (defined('STDERR')) {
			fwrite(STDERR, 'FreshRSS write access problem in ' . join_path(USERS_PATH, $user, LOG_FILENAME) . "\n");
		}
	}

	gc_collect_cycles();
}

$end_date = date_create('now');
$duration = date_diff($end_date, $begin_date);
notice('FreshRSS actualization done for ' . count($users) .
	' users, using ' . format_bytes(memory_get_peak_usage(true)) . ' of memory, in ' .
	$duration->format('%a day(s), %h hour(s), %i minute(s) and %s seconds.'));

echo 'End.', "\n";
ob_end_flush();