ytkns/cron.php

52 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2020-06-10 16:03:13 +00:00
<?php
namespace YTKNS;
if(!defined('YTKNS_SEM_NAME'))
define('YTKNS_SEM_NAME', 'b');
if(!defined('YTKNS_SFM_PATH'))
define('YTKNS_SFM_PATH', sys_get_temp_dir() . DIRECTORY_SEPARATOR . '{04884071-d334-4388-947a-4fd8c4d4f4ea}');
if(!is_file(YTKNS_SFM_PATH))
touch(YTKNS_SFM_PATH);
$ftok = ftok(YTKNS_SFM_PATH, YTKNS_SEM_NAME);
$semaphore = sem_get($ftok, 1);
if(!sem_acquire($semaphore))
die('Failed to acquire semaphore.' . PHP_EOL);
2021-07-28 22:10:57 +00:00
try {
require_once __DIR__ . '/startup.php';
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
// Prevent running cron script during maintenance
if(!YTKNS_MAINTENANCE) {
// Destroy old sessions
UserSession::purge();
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
// Resynchronise use counts
Upload::resync(EFFECT_UPLOADS);
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
// Destroy orphaned uploads
Upload::purgeOrphans();
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
// Get task queue
$taskQueue = ZoneTask::queue();
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
// Plow through tasks
// TODO: make task functions modular
while($task = array_shift($taskQueue)) {
if(!isset($zoneInfo) || $zoneInfo->getId() !== $task->getZoneId())
$zoneInfo = $task->getZone();
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
switch($task->getName()) {
case 'screenshot':
$zoneInfo->takeScreenshot();
break;
}
2020-06-10 16:03:13 +00:00
2021-07-28 22:10:57 +00:00
$task->delete();
}
}
} finally {
sem_release($semaphore);
2020-06-10 16:03:13 +00:00
}