Create cron.php system for executing background jobs
* Adapt PgDatabase class for cron (called by "php cron.php")
This commit is contained in:
parent
f64bd4bc36
commit
eb180cdcdf
|
@ -1,3 +1,4 @@
|
|||
config/usr/*
|
||||
!.gitkeep
|
||||
.*.swp
|
||||
.*.lock
|
||||
|
|
35
base.php
35
base.php
|
@ -1,22 +1,24 @@
|
|||
<?php
|
||||
require 'config/application.php';
|
||||
|
||||
if (!isset($_COOKIE['_session']) || !file_exists('/tmp/apcontrol-sessions')) {
|
||||
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
||||
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
||||
$session = null;
|
||||
if (isset($_SERVER['REQUEST_URI'])) {
|
||||
if (!isset($_COOKIE['_session']) || !file_exists('/tmp/apcontrol-sessions')) {
|
||||
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
||||
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
||||
}
|
||||
header('Location: login.php'); die;
|
||||
}
|
||||
header('Location: login.php'); die;
|
||||
}
|
||||
|
||||
$session = trim($_COOKIE['_session']);
|
||||
$sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions')));
|
||||
if (!in_array($session, $sessions)) {
|
||||
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
||||
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
||||
$session = trim($_COOKIE['_session']);
|
||||
$sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions')));
|
||||
if (!in_array($session, $sessions)) {
|
||||
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
||||
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
||||
}
|
||||
header('Location: login.php'); die;
|
||||
}
|
||||
header('Location: login.php'); die;
|
||||
unset($sessions);
|
||||
}
|
||||
unset($sessions);
|
||||
|
||||
if (!file_exists($GLOBALS['appconf']['data_dir']))
|
||||
mkdir($GLOBALS['appconf']['data_dir']);
|
||||
|
@ -315,8 +317,13 @@ function content_cache__put($key, $config, $data) {
|
|||
$cache_dir = $GLOBALS['appconf']['data_dir'].'/cache';
|
||||
if (!file_exists($cache_dir))
|
||||
mkdir($cache_dir);
|
||||
$ps = explode(',', trim($config));
|
||||
$cache_file = $cache_dir.'/'.md5($key);
|
||||
file_put_contents($cache_file.','.$config, serialize($data));
|
||||
if ($ps[0] === 'always')
|
||||
$cache_file .= ','.$ps[0];
|
||||
else if ($ps[0] === 'ondemand')
|
||||
$cache_file .= $config;
|
||||
file_put_contents($cache_file, serialize($data));
|
||||
}
|
||||
|
||||
function cronjob_db_create__check_cachecfg($software, $instance, $sql, $cache=null) {
|
||||
|
|
|
@ -3,14 +3,19 @@
|
|||
class PgDatabase {
|
||||
private $db = null;
|
||||
|
||||
public function __construct() {
|
||||
$url = trim($_SERVER['REQUEST_URI'], '/');
|
||||
$url = substr($url, 7); // api/vX/
|
||||
$ps = explode('/', $url);
|
||||
if ($ps[0] !== 'database') return;
|
||||
$software = $ps[1];
|
||||
public function __construct($software=null, $instance=null) {
|
||||
if ($software === null) {
|
||||
$url = trim($_SERVER['REQUEST_URI'], '/');
|
||||
$url = substr($url, 7); // api/vX/
|
||||
$ps = explode('/', $url);
|
||||
if ($ps[0] !== 'database') return;
|
||||
$software = $ps[1];
|
||||
}
|
||||
|
||||
$config = instance_config($software);
|
||||
$config = null;
|
||||
if ($instance === null)
|
||||
$config = instance_config($software);
|
||||
else $config = instance_config($software, $instance);
|
||||
if ($config === null) return;
|
||||
|
||||
$this->db = pg_connect(
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?php chdir(__DIR__) ?>
|
||||
<?php require 'base.php' ?>
|
||||
<?php
|
||||
|
||||
if (file_exists('.cron.lock'))
|
||||
die('cron.php is locked by ".cron.lock"');
|
||||
touch('.cron.lock');
|
||||
|
||||
function logi($thing) { echo date('Y/m/d H:i:s').' INFO | '.strval($thing)."\n"; }
|
||||
function loge($thing) { echo date('Y/m/d H:i:s').' ERROR | '.strval($thing)."\n"; }
|
||||
|
||||
function end_cronjobs() {
|
||||
unlink('.cron.lock');
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
////////////////
|
||||
// INITIAL SETUP
|
||||
//
|
||||
$GLOBALS['_crondir'] = $GLOBALS['appconf']['data_dir'].'/cron';
|
||||
if (!file_exists($GLOBALS['_crondir'])) {
|
||||
mkdir($GLOBALS['_crondir']);
|
||||
end_cronjobs();
|
||||
}
|
||||
//
|
||||
////////////////
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// CRONJOBS for database caching
|
||||
//
|
||||
function run__cronjobs_db() {
|
||||
$dir = $GLOBALS['_crondir'].'/db';
|
||||
if (!file_exists($dir)) {
|
||||
mkdir($dir);
|
||||
return false;
|
||||
}
|
||||
$jobs = [];
|
||||
foreach (scandir($dir) as $jf) {
|
||||
if (in_array($jf,['.','..']))
|
||||
continue;
|
||||
$ps = explode(',',$jf);
|
||||
$jobs []= (object)[
|
||||
'id' => $ps[2],
|
||||
'file' => $dir.'/'.$jf,
|
||||
'software' => $ps[0],
|
||||
'instance' => $ps[1],
|
||||
'time' => intval($ps[3]),
|
||||
];
|
||||
}
|
||||
foreach ($jobs as $job) {
|
||||
$ms = time() - filemtime($job->file);
|
||||
$sql = file_get_contents($job->file);
|
||||
if ($ms < $job->time) continue;
|
||||
|
||||
logi("Processing job: {$job->id}, software={$job->software}, instance={$job->instance}");
|
||||
$ok = false;
|
||||
switch ($job->software) {
|
||||
case 'mastodon':
|
||||
$pg = new PgDatabase($job->software, $job->instance);
|
||||
$result = $pg->fetch_all($sql);
|
||||
content_cache__put($job->software.$job->instance.$sql, 'always,'.$job->time, $result);
|
||||
$pg->close();
|
||||
$ok = true;
|
||||
break;
|
||||
}
|
||||
if ($ok)
|
||||
logi("Job done: {$job->id}");
|
||||
}
|
||||
}
|
||||
run__cronjobs_db();
|
||||
//
|
||||
////////////////////////////////
|
||||
|
||||
logi('Cron has finished!');
|
||||
|
||||
end_cronjobs();
|
Loading…
Reference in New Issue