ytkns/cron.php

50 lines
1.2 KiB
PHP

<?php
namespace YTKNS;
// Prevent running cron script during maintenance
if(YTKNS_MAINTENANCE)
return;
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);
require_once __DIR__ . '/startup.php';
// Destroy old sessions
UserSession::purge();
// Resynchronise use counts
Upload::resync(EFFECT_UPLOADS);
// Destroy orphaned uploads
Upload::purgeOrphans();
// Get task queue
$taskQueue = ZoneTask::queue();
// Plow through tasks
// TODO: make task functions modular
while($task = array_shift($taskQueue)) {
if(!isset($zoneInfo) || $zoneInfo->getId() !== $task->getZoneId())
$zoneInfo = $task->getZone();
switch($task->getName()) {
case 'screenshot':
$zoneInfo->takeScreenshot();
break;
}
$task->delete();
}
sem_release($semaphore);