Execute a worker task when there hadn't one for some seconds
authorMichael <heluecht@pirati.ca>
Wed, 28 Feb 2024 23:33:46 +0000 (23:33 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 21 Mar 2024 09:10:07 +0000 (09:10 +0000)
database.sql
src/Core/Worker.php
static/defaults.config.php

index 99b78f4..16b5803 100644 (file)
@@ -2000,7 +2000,7 @@ CREATE TABLE IF NOT EXISTS `worker-ipc` (
 -- VIEW application-view
 --
 DROP VIEW IF EXISTS `application-view`;
-CREATE VIEW `application-view` AS SELECT
+CREATE VIEW `application-view` AS SELECT 
        `application`.`id` AS `id`,
        `application-token`.`uid` AS `uid`,
        `application`.`name` AS `name`,
@@ -2024,7 +2024,7 @@ CREATE VIEW `application-view` AS SELECT
 -- VIEW circle-member-view
 --
 DROP VIEW IF EXISTS `circle-member-view`;
-CREATE VIEW `circle-member-view` AS SELECT
+CREATE VIEW `circle-member-view` AS SELECT 
        `group_member`.`id` AS `id`,
        `group`.`uid` AS `uid`,
        `group_member`.`contact-id` AS `contact-id`,
@@ -2055,7 +2055,7 @@ CREATE VIEW `circle-member-view` AS SELECT
 -- VIEW post-counts-view
 --
 DROP VIEW IF EXISTS `post-counts-view`;
-CREATE VIEW `post-counts-view` AS SELECT
+CREATE VIEW `post-counts-view` AS SELECT 
        `post-counts`.`uri-id` AS `uri-id`,
        `post-counts`.`vid` AS `vid`,
        `verb`.`name` AS `verb`,
@@ -2069,7 +2069,7 @@ CREATE VIEW `post-counts-view` AS SELECT
 -- VIEW post-timeline-view
 --
 DROP VIEW IF EXISTS `post-timeline-view`;
-CREATE VIEW `post-timeline-view` AS SELECT
+CREATE VIEW `post-timeline-view` AS SELECT 
        `post-user`.`uid` AS `uid`,
        `post-user`.`uri-id` AS `uri-id`,
        `post-user`.`gravity` AS `gravity`,
@@ -2114,7 +2114,7 @@ CREATE VIEW `post-timeline-view` AS SELECT
 -- VIEW post-searchindex-user-view
 --
 DROP VIEW IF EXISTS `post-searchindex-user-view`;
-CREATE VIEW `post-searchindex-user-view` AS SELECT
+CREATE VIEW `post-searchindex-user-view` AS SELECT 
        `post-thread-user`.`uid` AS `uid`,
        `post-searchindex`.`uri-id` AS `uri-id`,
        `post-searchindex`.`owner-id` AS `owner-id`,
@@ -2146,7 +2146,7 @@ CREATE VIEW `post-searchindex-user-view` AS SELECT
 -- VIEW post-user-view
 --
 DROP VIEW IF EXISTS `post-user-view`;
-CREATE VIEW `post-user-view` AS SELECT
+CREATE VIEW `post-user-view` AS SELECT 
        `post-user`.`id` AS `id`,
        `post-user`.`id` AS `post-user-id`,
        `post-user`.`uid` AS `uid`,
@@ -2332,7 +2332,7 @@ CREATE VIEW `post-user-view` AS SELECT
 -- VIEW post-thread-user-view
 --
 DROP VIEW IF EXISTS `post-thread-user-view`;
-CREATE VIEW `post-thread-user-view` AS SELECT
+CREATE VIEW `post-thread-user-view` AS SELECT 
        `post-user`.`id` AS `id`,
        `post-user`.`id` AS `post-user-id`,
        `post-thread-user`.`uid` AS `uid`,
@@ -2516,7 +2516,7 @@ CREATE VIEW `post-thread-user-view` AS SELECT
 -- VIEW post-view
 --
 DROP VIEW IF EXISTS `post-view`;
-CREATE VIEW `post-view` AS SELECT
+CREATE VIEW `post-view` AS SELECT 
        `item-uri`.`uri` AS `uri`,
        `post`.`uri-id` AS `uri-id`,
        `parent-item-uri`.`uri` AS `parent-uri`,
@@ -2663,7 +2663,7 @@ CREATE VIEW `post-view` AS SELECT
 -- VIEW post-thread-view
 --
 DROP VIEW IF EXISTS `post-thread-view`;
-CREATE VIEW `post-thread-view` AS SELECT
+CREATE VIEW `post-thread-view` AS SELECT 
        `item-uri`.`uri` AS `uri`,
        `post-thread`.`uri-id` AS `uri-id`,
        `parent-item-uri`.`uri` AS `parent-uri`,
@@ -2811,7 +2811,7 @@ CREATE VIEW `post-thread-view` AS SELECT
 -- VIEW category-view
 --
 DROP VIEW IF EXISTS `category-view`;
-CREATE VIEW `category-view` AS SELECT
+CREATE VIEW `category-view` AS SELECT 
        `post-category`.`uri-id` AS `uri-id`,
        `post-category`.`uid` AS `uid`,
        `post-category`.`type` AS `type`,
@@ -2825,7 +2825,7 @@ CREATE VIEW `category-view` AS SELECT
 -- VIEW collection-view
 --
 DROP VIEW IF EXISTS `collection-view`;
-CREATE VIEW `collection-view` AS SELECT
+CREATE VIEW `collection-view` AS SELECT 
        `post-collection`.`uri-id` AS `uri-id`,
        `post-collection`.`type` AS `type`,
        `post-collection`.`author-id` AS `cid`,
@@ -2846,7 +2846,7 @@ CREATE VIEW `collection-view` AS SELECT
 -- VIEW media-view
 --
 DROP VIEW IF EXISTS `media-view`;
-CREATE VIEW `media-view` AS SELECT
+CREATE VIEW `media-view` AS SELECT 
        `post-media`.`uri-id` AS `uri-id`,
        `post-media`.`type` AS `type`,
        `post`.`received` AS `received`,
@@ -2864,7 +2864,7 @@ CREATE VIEW `media-view` AS SELECT
 -- VIEW tag-view
 --
 DROP VIEW IF EXISTS `tag-view`;
-CREATE VIEW `tag-view` AS SELECT
+CREATE VIEW `tag-view` AS SELECT 
        `post-tag`.`uri-id` AS `uri-id`,
        `post-tag`.`type` AS `type`,
        `post-tag`.`tid` AS `tid`,
@@ -2880,7 +2880,7 @@ CREATE VIEW `tag-view` AS SELECT
 -- VIEW network-item-view
 --
 DROP VIEW IF EXISTS `network-item-view`;
-CREATE VIEW `network-item-view` AS SELECT
+CREATE VIEW `network-item-view` AS SELECT 
        `post-user`.`uri-id` AS `uri-id`,
        `post-thread-user`.`post-user-id` AS `parent`,
        `post-user`.`received` AS `received`,
@@ -2910,7 +2910,7 @@ CREATE VIEW `network-item-view` AS SELECT
 -- VIEW network-thread-view
 --
 DROP VIEW IF EXISTS `network-thread-view`;
-CREATE VIEW `network-thread-view` AS SELECT
+CREATE VIEW `network-thread-view` AS SELECT 
        `post-thread-user`.`uri-id` AS `uri-id`,
        `post-thread-user`.`post-user-id` AS `parent`,
        `post-thread-user`.`received` AS `received`,
@@ -2938,7 +2938,7 @@ CREATE VIEW `network-thread-view` AS SELECT
 -- VIEW owner-view
 --
 DROP VIEW IF EXISTS `owner-view`;
-CREATE VIEW `owner-view` AS SELECT
+CREATE VIEW `owner-view` AS SELECT 
        `contact`.`id` AS `id`,
        `contact`.`uid` AS `uid`,
        `contact`.`created` AS `created`,
@@ -3066,7 +3066,7 @@ CREATE VIEW `owner-view` AS SELECT
 -- VIEW account-view
 --
 DROP VIEW IF EXISTS `account-view`;
-CREATE VIEW `account-view` AS SELECT
+CREATE VIEW `account-view` AS SELECT 
        `contact`.`id` AS `id`,
        `contact`.`url` AS `url`,
        `contact`.`nurl` AS `nurl`,
@@ -3154,7 +3154,7 @@ CREATE VIEW `account-view` AS SELECT
 -- VIEW account-user-view
 --
 DROP VIEW IF EXISTS `account-user-view`;
-CREATE VIEW `account-user-view` AS SELECT
+CREATE VIEW `account-user-view` AS SELECT 
        `ucontact`.`id` AS `id`,
        `contact`.`id` AS `pid`,
        `ucontact`.`uid` AS `uid`,
@@ -3260,7 +3260,7 @@ CREATE VIEW `account-user-view` AS SELECT
 -- VIEW pending-view
 --
 DROP VIEW IF EXISTS `pending-view`;
-CREATE VIEW `pending-view` AS SELECT
+CREATE VIEW `pending-view` AS SELECT 
        `register`.`id` AS `id`,
        `register`.`hash` AS `hash`,
        `register`.`created` AS `created`,
@@ -3282,7 +3282,7 @@ CREATE VIEW `pending-view` AS SELECT
 -- VIEW tag-search-view
 --
 DROP VIEW IF EXISTS `tag-search-view`;
-CREATE VIEW `tag-search-view` AS SELECT
+CREATE VIEW `tag-search-view` AS SELECT 
        `post-tag`.`uri-id` AS `uri-id`,
        `post-user`.`uid` AS `uid`,
        `post-user`.`id` AS `iid`,
@@ -3304,7 +3304,7 @@ CREATE VIEW `tag-search-view` AS SELECT
 -- VIEW workerqueue-view
 --
 DROP VIEW IF EXISTS `workerqueue-view`;
-CREATE VIEW `workerqueue-view` AS SELECT
+CREATE VIEW `workerqueue-view` AS SELECT 
        `process`.`pid` AS `pid`,
        `workerqueue`.`priority` AS `priority`
        FROM `process`
@@ -3315,7 +3315,7 @@ CREATE VIEW `workerqueue-view` AS SELECT
 -- VIEW profile_field-view
 --
 DROP VIEW IF EXISTS `profile_field-view`;
-CREATE VIEW `profile_field-view` AS SELECT
+CREATE VIEW `profile_field-view` AS SELECT 
        `profile_field`.`id` AS `id`,
        `profile_field`.`uid` AS `uid`,
        `profile_field`.`label` AS `label`,
@@ -3335,7 +3335,7 @@ CREATE VIEW `profile_field-view` AS SELECT
 -- VIEW diaspora-contact-view
 --
 DROP VIEW IF EXISTS `diaspora-contact-view`;
-CREATE VIEW `diaspora-contact-view` AS SELECT
+CREATE VIEW `diaspora-contact-view` AS SELECT 
        `diaspora-contact`.`uri-id` AS `uri-id`,
        `item-uri`.`uri` AS `url`,
        `item-uri`.`guid` AS `guid`,
index 7f06c12..65772f0 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Core;
 
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Worker\Entity\Process;
 use Friendica\Database\DBA;
 use Friendica\DI;
@@ -54,7 +55,8 @@ class Worker
        const FAST_COMMANDS = ['APDelivery', 'Delivery'];
 
        const LOCK_PROCESS = 'worker_process';
-       const LOCK_WORKER = 'worker';
+       const LOCK_WORKER  = 'worker';
+       const LAST_CHECK   = 'worker::check';
 
        private static $up_start;
        private static $db_duration = 0;
@@ -832,6 +834,17 @@ class Worker
                                } else {
                                        self::spawnWorker();
                                }
+                       } elseif (($active > $queues) && ($active < $maxqueues) && ($load < $maxsysload)) {
+                               $max_idletime = DI::config()->get('system', 'worker_max_idletime');
+                               $last_check   = DI::cache()->get(self::LAST_CHECK);
+                               $last_date    = $last_check ? date('c', $last_check) : '';
+                               if (($max_idletime > 0) && (time() > $last_check + $max_idletime) && !DBA::exists('workerqueue', ["`done` AND `executed` > ?", DateTimeFormat::utc('now - ' . $max_idletime . ' second')])) {
+                                       DI::cache()->set(self::LAST_CHECK, time(), Duration::HOUR);
+                                       Logger::info('The last worker execution had been too long ago.', ['last' => $last_check, 'last-check' => $last_date, 'seconds' => $max_idletime, 'load' => $load, 'max_load' => $maxsysload, 'active_worker' => $active, 'max_worker' => $maxqueues]);
+                                       return false;
+                               } elseif ($max_idletime > 0) {
+                                       Logger::debug('Maximum idletime not reached.', ['last' => $last_check, 'last-check' => $last_date, 'seconds' => $max_idletime, 'load' => $load, 'max_load' => $maxsysload, 'active_worker' => $active, 'max_worker' => $maxqueues]);
+                               }       
                        }
                }
 
index 7c17dce..431d577 100644 (file)
@@ -600,6 +600,10 @@ return [
                        Friendica\Core\Worker::PRIORITY_NEGLIGIBLE => 720
                ],
 
+               // worker_max_idletime (Integer)
+               // Higly experimental! Maximum number of seconds after the last worker execution to enforce a new worker process.
+               'worker_max_idletime' => 0,
+
                // worker_processes_cooldown (Integer)
                // Maximum number per processes that causes a cooldown before each worker function call.
                'worker_processes_cooldown' => 0,