From 2951243b079499e3a70e855a8b1d9b078cbfbfd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 28 Jul 2021 22:22:00 +0000 Subject: [PATCH] Preparation for delayed posts --- database.sql | 58 +++++++++++++++++--------------- doc/database/db_delayed-post.md | 6 ++++ src/Core/Worker.php | 18 +++++----- src/Model/Post/Delayed.php | 15 +++++++-- static/dbstructure.config.php | 59 ++++++++++++++++++--------------- 5 files changed, 92 insertions(+), 64 deletions(-) diff --git a/database.sql b/database.sql index 8ae1b913b5..da39341f15 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.09-dev (Siberian Iris) --- DB_UPDATE_VERSION 1430 +-- DB_UPDATE_VERSION 1431 -- ------------------------------------------ @@ -492,16 +492,47 @@ CREATE TABLE IF NOT EXISTS `conversation` ( INDEX `received` (`received`) ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Raw data and structure information for messages'; +-- +-- TABLE workerqueue +-- +CREATE TABLE IF NOT EXISTS `workerqueue` ( + `id` int unsigned NOT NULL auto_increment COMMENT 'Auto incremented worker task id', + `command` varchar(100) COMMENT 'Task command', + `parameter` mediumtext COMMENT 'Task parameter', + `priority` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'Task priority', + `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Creation date', + `pid` int unsigned NOT NULL DEFAULT 0 COMMENT 'Process id of the worker', + `executed` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Execution date', + `next_try` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Next retrial date', + `retrial` tinyint NOT NULL DEFAULT 0 COMMENT 'Retrial counter', + `done` boolean NOT NULL DEFAULT '0' COMMENT 'Marked 1 when the task was done - will be deleted later', + PRIMARY KEY(`id`), + INDEX `command` (`command`), + INDEX `done_command_parameter` (`done`,`command`,`parameter`(64)), + INDEX `done_executed` (`done`,`executed`), + INDEX `done_priority_retrial_created` (`done`,`priority`,`retrial`,`created`), + INDEX `done_priority_next_try` (`done`,`priority`,`next_try`), + INDEX `done_pid_next_try` (`done`,`pid`,`next_try`), + INDEX `done_pid_retrial` (`done`,`pid`,`retrial`), + INDEX `done_pid_priority_created` (`done`,`pid`,`priority`,`created`) +) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Background tasks queue entries'; + -- -- TABLE delayed-post -- CREATE TABLE IF NOT EXISTS `delayed-post` ( `id` int unsigned NOT NULL auto_increment, `uri` varchar(255) COMMENT 'URI of the post that will be distributed later', + `title` varchar(255) COMMENT 'post title', + `body` mediumtext COMMENT 'post body content', + `private` tinyint unsigned COMMENT '0=public, 1=private, 2=unlisted', + `wid` int unsigned COMMENT 'Workerqueue id', `uid` mediumint unsigned COMMENT 'Owner User id', `delayed` datetime COMMENT 'delay time', PRIMARY KEY(`id`), UNIQUE INDEX `uid_uri` (`uid`,`uri`(190)), + INDEX `wid` (`wid`), + FOREIGN KEY (`wid`) REFERENCES `workerqueue` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Posts that are about to be distributed at a later time'; @@ -1474,31 +1505,6 @@ CREATE TABLE IF NOT EXISTS `worker-ipc` ( PRIMARY KEY(`key`) ) ENGINE=MEMORY DEFAULT COLLATE utf8mb4_general_ci COMMENT='Inter process communication between the frontend and the worker'; --- --- TABLE workerqueue --- -CREATE TABLE IF NOT EXISTS `workerqueue` ( - `id` int unsigned NOT NULL auto_increment COMMENT 'Auto incremented worker task id', - `command` varchar(100) COMMENT 'Task command', - `parameter` mediumtext COMMENT 'Task parameter', - `priority` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'Task priority', - `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Creation date', - `pid` int unsigned NOT NULL DEFAULT 0 COMMENT 'Process id of the worker', - `executed` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Execution date', - `next_try` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Next retrial date', - `retrial` tinyint NOT NULL DEFAULT 0 COMMENT 'Retrial counter', - `done` boolean NOT NULL DEFAULT '0' COMMENT 'Marked 1 when the task was done - will be deleted later', - PRIMARY KEY(`id`), - INDEX `command` (`command`), - INDEX `done_command_parameter` (`done`,`command`,`parameter`(64)), - INDEX `done_executed` (`done`,`executed`), - INDEX `done_priority_retrial_created` (`done`,`priority`,`retrial`,`created`), - INDEX `done_priority_next_try` (`done`,`priority`,`next_try`), - INDEX `done_pid_next_try` (`done`,`pid`,`next_try`), - INDEX `done_pid_retrial` (`done`,`pid`,`retrial`), - INDEX `done_pid_priority_created` (`done`,`pid`,`priority`,`created`) -) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Background tasks queue entries'; - -- -- VIEW application-view -- diff --git a/doc/database/db_delayed-post.md b/doc/database/db_delayed-post.md index fe3251f70a..77cd677f65 100644 --- a/doc/database/db_delayed-post.md +++ b/doc/database/db_delayed-post.md @@ -10,6 +10,10 @@ Fields | ------- | ---------------------------------------------- | ------------------ | ---- | --- | ------- | -------------- | | id | | int unsigned | NO | PRI | NULL | auto_increment | | uri | URI of the post that will be distributed later | varchar(255) | YES | | NULL | | +| title | post title | varchar(255) | YES | | NULL | | +| body | post body content | mediumtext | YES | | NULL | | +| private | 0=public, 1=private, 2=unlisted | tinyint unsigned | YES | | NULL | | +| wid | Workerqueue id | int unsigned | YES | | NULL | | | uid | Owner User id | mediumint unsigned | YES | | NULL | | | delayed | delay time | datetime | YES | | NULL | | @@ -20,12 +24,14 @@ Indexes | ------- | --------------------- | | PRIMARY | id | | uid_uri | UNIQUE, uid, uri(190) | +| wid | wid | Foreign Keys ------------ | Field | Target Table | Target Field | |-------|--------------|--------------| +| wid | [workerqueue](help/database/db_workerqueue) | id | | uid | [user](help/database/db_user) | uid | Return to [database documentation](help/database) diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 824275fa74..307451c052 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -1200,7 +1200,7 @@ class Worker * or: Worker::add(PRIORITY_HIGH, "Notifier", Delivery::DELETION, $drop_id); * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "Delivery", $post_id); * - * @return boolean "false" if worker queue entry already existed or there had been an error + * @return int "0" if worker queue entry already existed or there had been an error, otherwise the ID of the worker task * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @note $cmd and string args are surrounded with "" * @@ -1213,14 +1213,14 @@ class Worker $args = func_get_args(); if (!count($args)) { - return false; + return 0; } $arr = ['args' => $args, 'run_cmd' => true]; Hook::callAll("proc_run", $arr); if (!$arr['run_cmd'] || !count($args)) { - return true; + return 1; } $priority = PRIORITY_MEDIUM; @@ -1255,7 +1255,7 @@ class Worker $command = array_shift($args); $parameters = json_encode($args); $found = DBA::exists('workerqueue', ['command' => $command, 'parameter' => $parameters, 'done' => false]); - $added = false; + $added = 0; if (!in_array($priority, PRIORITIES)) { Logger::warning('Invalid priority', ['priority' => $priority, 'command' => $command, 'callstack' => System::callstack(20)]); @@ -1264,15 +1264,15 @@ class Worker // Quit if there was a database error - a precaution for the update process to 3.5.3 if (DBA::errorNo() != 0) { - return false; + return 0; } if (!$found) { - $added = DBA::insert('workerqueue', ['command' => $command, 'parameter' => $parameters, 'created' => $created, - 'priority' => $priority, 'next_try' => $delayed]); - if (!$added) { - return false; + if (!DBA::insert('workerqueue', ['command' => $command, 'parameter' => $parameters, 'created' => $created, + 'priority' => $priority, 'next_try' => $delayed])) { + return 0; } + $added = DBA::lastInsertId(); } elseif ($force_priority) { DBA::update('workerqueue', ['priority' => $priority], ['command' => $command, 'parameter' => $parameters, 'done' => false, 'pid' => 0]); } diff --git a/src/Model/Post/Delayed.php b/src/Model/Post/Delayed.php index 2097f0f537..8d631a285d 100644 --- a/src/Model/Post/Delayed.php +++ b/src/Model/Post/Delayed.php @@ -64,13 +64,24 @@ class Delayed Logger::notice('Adding post for delayed publishing', ['uid' => $item['uid'], 'delayed' => $delayed, 'uri' => $uri]); - if (!Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri)) { + $wid = Worker::add(['priority' => PRIORITY_HIGH, 'delayed' => $delayed], 'DelayedPublish', $item, $notify, $taglist, $attachments, $unprepared, $uri); + if (!$wid) { return false; } DI::pConfig()->set($item['uid'], 'system', 'last_publish', $next_publish); - return DBA::insert('delayed-post', ['uri' => $uri, 'uid' => $item['uid'], 'delayed' => $delayed], Database::INSERT_IGNORE); + $delayed_post = [ + 'uri' => $uri, + 'title' => $item['title'], + 'body' => $item['body'], + 'private' => $item['private'], + 'wid' => $item['wid'], + 'uid' => $item['uid'], + 'delayed' => $delayed, + ]; + + return DBA::insert('delayed-post', $delayed_post, Database::INSERT_IGNORE); } /** diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index ded539af50..0376315e3f 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1430); + define('DB_UPDATE_VERSION', 1431); } return [ @@ -554,17 +554,48 @@ return [ "received" => ["received"], ] ], + "workerqueue" => [ + "comment" => "Background tasks queue entries", + "fields" => [ + "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented worker task id"], + "command" => ["type" => "varchar(100)", "comment" => "Task command"], + "parameter" => ["type" => "mediumtext", "comment" => "Task parameter"], + "priority" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Task priority"], + "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Creation date"], + "pid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => "Process id of the worker"], + "executed" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Execution date"], + "next_try" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Next retrial date"], + "retrial" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => "Retrial counter"], + "done" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Marked 1 when the task was done - will be deleted later"], + ], + "indexes" => [ + "PRIMARY" => ["id"], + "command" => ["command"], + "done_command_parameter" => ["done", "command", "parameter(64)"], + "done_executed" => ["done", "executed"], + "done_priority_retrial_created" => ["done", "priority", "retrial", "created"], + "done_priority_next_try" => ["done", "priority", "next_try"], + "done_pid_next_try" => ["done", "pid", "next_try"], + "done_pid_retrial" => ["done", "pid", "retrial"], + "done_pid_priority_created" => ["done", "pid", "priority", "created"] + ] + ], "delayed-post" => [ "comment" => "Posts that are about to be distributed at a later time", "fields" => [ "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"], "uri" => ["type" => "varchar(255)", "comment" => "URI of the post that will be distributed later"], + "title" => ["type" => "varchar(255)", "comment" => "post title"], + "body" => ["type" => "mediumtext", "comment" => "post body content"], + "private" => ["type" => "tinyint unsigned", "comment" => "0=public, 1=private, 2=unlisted"], + "wid" => ["type" => "int unsigned", "foreign" => ["workerqueue" => "id"], "comment" => "Workerqueue id"], "uid" => ["type" => "mediumint unsigned", "foreign" => ["user" => "uid"], "comment" => "Owner User id"], "delayed" => ["type" => "datetime", "comment" => "delay time"], ], "indexes" => [ "PRIMARY" => ["id"], "uid_uri" => ["UNIQUE", "uid", "uri(190)"], + "wid" => ["wid"], ] ], "diaspora-interaction" => [ @@ -1496,30 +1527,4 @@ return [ ], "engine" => "MEMORY", ], - "workerqueue" => [ - "comment" => "Background tasks queue entries", - "fields" => [ - "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented worker task id"], - "command" => ["type" => "varchar(100)", "comment" => "Task command"], - "parameter" => ["type" => "mediumtext", "comment" => "Task parameter"], - "priority" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Task priority"], - "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Creation date"], - "pid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => "Process id of the worker"], - "executed" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Execution date"], - "next_try" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Next retrial date"], - "retrial" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => "Retrial counter"], - "done" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Marked 1 when the task was done - will be deleted later"], - ], - "indexes" => [ - "PRIMARY" => ["id"], - "command" => ["command"], - "done_command_parameter" => ["done", "command", "parameter(64)"], - "done_executed" => ["done", "executed"], - "done_priority_retrial_created" => ["done", "priority", "retrial", "created"], - "done_priority_next_try" => ["done", "priority", "next_try"], - "done_pid_next_try" => ["done", "pid", "next_try"], - "done_pid_retrial" => ["done", "pid", "retrial"], - "done_pid_priority_created" => ["done", "pid", "priority", "created"] - ] - ], ];