manual timeouts

This commit is contained in:
Shish 2023-06-25 14:19:02 +01:00
parent 1558318283
commit c5e2353447
9 changed files with 30 additions and 47 deletions

View file

@ -4,6 +4,11 @@ declare(strict_types=1);
namespace Shimmie2;
class TimeoutException extends \RuntimeException
{
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Event API *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -93,6 +98,18 @@ function _dump_event_listeners(array $event_listeners, string $path): void
/** @private */
global $_shm_event_count;
$_shm_event_count = 0;
$_shm_timeout = null;
function shm_set_timeout(?int $timeout=null): void
{
global $_shm_timeout;
$_shm_timeout = ftime() + $timeout;
set_time_limit(is_null($timeout) ? 0 : $timeout);
}
if(ini_get('max_execution_time')) {
shm_set_timeout((int)ini_get('max_execution_time') - 3);
}
/**
* Send an event to all registered Extensions.
@ -105,7 +122,7 @@ function send_event(Event $event): Event
{
global $tracer_enabled;
global $_shm_event_listeners, $_shm_event_count, $_tracer;
global $_shm_event_listeners, $_shm_event_count, $_tracer, $_shm_timeout;
$event_name = _namespaced_class_name(get_class($event));
if (!isset($_shm_event_listeners[$event_name])) {
return $event;
@ -122,6 +139,9 @@ function send_event(Event $event): Event
ksort($my_event_listeners);
foreach ($my_event_listeners as $listener) {
if($_shm_timeout && ftime() > $_shm_timeout) {
throw new TimeoutException();
}
if ($tracer_enabled) {
$_tracer->begin(get_class($listener));
}

View file

@ -1,32 +0,0 @@
<?php
class TimeoutException extends RuntimeException
{
}
class Timeout
{
private $active;
public function set($seconds)
{
$this->active = true;
// declare(ticks = 1);
// pcntl_signal(SIGALRM, [$this, 'handle'], true);
// pcntl_alarm($seconds);
set_time_limit($seconds + 5);
}
public function clear()
{
set_time_limit(0);
$this->active = false;
}
public function handle($signal)
{
if ($this->active) {
throw new TimeoutException();
}
}
}

View file

@ -37,7 +37,7 @@ class AdminPage extends Extension
public function onPageRequest(PageRequestEvent $event)
{
global $database, $page, $user, $_shm_timeout;
global $database, $page, $user;
if ($event->page_matches("admin")) {
if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) {
@ -51,7 +51,7 @@ class AdminPage extends Extension
if ($user->check_auth_token()) {
log_info("admin", "Util: $action");
$_shm_timeout->clear();
shm_set_timeout(null);
$database->set_timeout(null);
send_event($aae);
}

View file

@ -24,10 +24,10 @@ class BulkAdd extends Extension
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user, $_shm_timeout;
global $page, $user;
if ($event->page_matches("bulk_add")) {
if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['dir'])) {
$_shm_timeout->clear();
shm_set_timeout(null);
$bae = send_event(new BulkAddEvent($_POST['dir']));
foreach ($bae->results as $result) {
$this->theme->add_status("Adding files", $result);

View file

@ -11,10 +11,10 @@ class BulkAddCSV extends Extension
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user, $_shm_timeout;
global $page, $user;
if ($event->page_matches("bulk_add_csv")) {
if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['csv'])) {
$_shm_timeout->clear();
shm_set_timeout(null);
$this->add_csv($_POST['csv']);
$this->theme->display_upload_results($page);
}

View file

@ -350,7 +350,7 @@ class CronUploader extends Extension
self::$IMPORT_RUNNING = true;
try {
//$_shm_timeout->clear();
//shm_set_timeout(null);
$output_subdir = date('Ymd-His', time());
$image_queue = $this->generate_image_queue();

View file

@ -180,7 +180,6 @@ class SourceHistory extends Extension
protected function process_bulk_revert_request()
{
global $_shm_timeout;
if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) {
$revert_name = $_POST['revert_name'];
} else {
@ -210,7 +209,7 @@ class SourceHistory extends Extension
$revert_date = null;
}
$_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible.
shm_set_timeout(null); // reverting changes can take a long time, disable php's timelimit if possible.
// Call the revert function.
$this->process_revert_all_changes($revert_name, $revert_ip, $revert_date);

View file

@ -178,8 +178,6 @@ class TagHistory extends Extension
protected function process_bulk_revert_request()
{
global $_shm_timeout;
if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) {
$revert_name = $_POST['revert_name'];
} else {
@ -209,7 +207,7 @@ class TagHistory extends Extension
$revert_date = null;
}
$_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible.
shm_set_timeout(null); // reverting changes can take a long time, disable php's timelimit if possible.
// Call the revert function.
$this->process_revert_all_changes($revert_name, $revert_ip, $revert_date);

View file

@ -46,8 +46,6 @@ _set_up_shimmie_environment();
$_tracer = new \EventTracer();
$_tracer->begin("Bootstrap");
_load_core_files();
$_shm_timeout = new \Timeout();
$_shm_timeout->set(((int)ini_get('max_execution_time')) || 30);
$cache = loadCache(CACHE_DSN);
$database = new Database(DATABASE_DSN);
$config = new DatabaseConfig($database);