From a315f430391e04b8757a34dae4dc97b6b421341d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sun, 11 Sep 2022 12:20:26 +0000 Subject: [PATCH 1/3] Fix functon scheduling loop logic, and double-loop prevention --- app/config/collections.php | 11 +++++++++++ app/controllers/api/functions.php | 4 ++++ app/workers/functions.php | 12 ++++++++---- composer.lock | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 633e2c33b9..40710f1e3d 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2149,6 +2149,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('scheduleUpdatedAt'), + 'type' => Database::VAR_DATETIME, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['datetime'], + ], [ '$id' => ID::custom('schedulePrevious'), 'type' => Database::VAR_DATETIME, diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 57928f5f1c..bb491bd73f 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -82,6 +82,7 @@ App::post('/v1/functions') 'deployment' => '', 'events' => $events, 'schedule' => $schedule, + 'scheduleUpdatedAt' => DateTime::now(), 'schedulePrevious' => null, 'scheduleNext' => null, 'timeout' => $timeout, @@ -441,11 +442,14 @@ App::put('/v1/functions/:functionId') $cron = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null; $next = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null; + $scheduleUpdatedAt = $schedule !== $original ? DateTime::now() : $function->getAttribute('scheduleUpdatedAt'); + $function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [ 'execute' => $execute, 'name' => $name, 'events' => $events, 'schedule' => $schedule, + 'scheduleUpdatedAt' => $scheduleUpdatedAt, 'scheduleNext' => $next, 'timeout' => $timeout, 'search' => implode(' ', [$functionId, $name, $function->getAttribute('runtime')]), diff --git a/app/workers/functions.php b/app/workers/functions.php index bcc8cb8dd3..e6012a845e 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -129,7 +129,7 @@ class FunctionsV1 extends Worker break; case 'schedule': - $scheduleOriginal = $execution->getAttribute('scheduleOriginal', ''); + $functionOriginal = $function; /* * 1. Get Original Task * 2. Check for updates @@ -150,21 +150,25 @@ class FunctionsV1 extends Worker throw new Exception('Function not found (' . $function->getId() . ')'); } - if ($scheduleOriginal && $scheduleOriginal !== $function->getAttribute('schedule')) { // Schedule has changed from previous run, ignore this run. + if ($functionOriginal->getAttribute('schedule') !== $function->getAttribute('schedule')) { // Schedule has changed from previous run, ignore this run. + return; + } + + if ($functionOriginal->getAttribute('scheduleUpdatedAt') !== $function->getAttribute('scheduleUpdatedAt')) { // Double execution due to rapid cron changes, ignore this run. return; } $cron = new CronExpression($function->getAttribute('schedule')); $next = DateTime::format($cron->getNextRunDate()); - $function + $function = $function ->setAttribute('scheduleNext', $next) ->setAttribute('schedulePrevious', DateTime::now()); $function = $database->updateDocument( 'functions', $function->getId(), - $function->setAttribute('scheduleNext', $next) + $function ); $reschedule = new Func(); diff --git a/composer.lock b/composer.lock index 1f61b8aef6..c57136587e 100644 --- a/composer.lock +++ b/composer.lock @@ -5384,5 +5384,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } From 640b5abe1ee1b4b1a0dccba901a033733549c1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 13 Sep 2022 20:13:48 +0000 Subject: [PATCH 2/3] PR review changes --- app/config/collections.php | 2 +- composer.lock | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 40710f1e3d..d935d72785 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2150,7 +2150,7 @@ $collections = [ 'filters' => [], ], [ - '$id' => ID::custom('scheduleUpdatedAt'), + '$id' => ID::custom('scheduleUpdatedAt'), // Used to fix duplicate executions bug. Can be removed once new queue library is used 'type' => Database::VAR_DATETIME, 'format' => '', 'size' => 0, diff --git a/composer.lock b/composer.lock index c57136587e..5ddf3f956e 100644 --- a/composer.lock +++ b/composer.lock @@ -2060,16 +2060,16 @@ }, { "name": "utopia-php/database", - "version": "0.25.2", + "version": "0.25.3", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "140bbedf1c4d622990fb94d26681fcca235cd5b9" + "reference": "40c5cd4762b0c647df8fdb21813392b8ef5d211b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/140bbedf1c4d622990fb94d26681fcca235cd5b9", - "reference": "140bbedf1c4d622990fb94d26681fcca235cd5b9", + "url": "https://api.github.com/repos/utopia-php/database/zipball/40c5cd4762b0c647df8fdb21813392b8ef5d211b", + "reference": "40c5cd4762b0c647df8fdb21813392b8ef5d211b", "shasum": "" }, "require": { @@ -2118,9 +2118,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.25.2" + "source": "https://github.com/utopia-php/database/tree/0.25.3" }, - "time": "2022-09-09T03:58:01+00:00" + "time": "2022-09-13T18:37:36+00:00" }, { "name": "utopia-php/domains", @@ -4812,16 +4812,16 @@ }, { "name": "sebastian/type", - "version": "3.1.0", + "version": "3.2.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/type.git", - "reference": "fb44e1cc6e557418387ad815780360057e40753e" + "reference": "fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/fb44e1cc6e557418387ad815780360057e40753e", - "reference": "fb44e1cc6e557418387ad815780360057e40753e", + "url": "https://api.github.com/repos/sebastianbergmann/type/zipball/fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e", + "reference": "fb3fe09c5f0bae6bc27ef3ce933a1e0ed9464b6e", "shasum": "" }, "require": { @@ -4833,7 +4833,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.1-dev" + "dev-master": "3.2-dev" } }, "autoload": { @@ -4856,7 +4856,7 @@ "homepage": "https://github.com/sebastianbergmann/type", "support": { "issues": "https://github.com/sebastianbergmann/type/issues", - "source": "https://github.com/sebastianbergmann/type/tree/3.1.0" + "source": "https://github.com/sebastianbergmann/type/tree/3.2.0" }, "funding": [ { @@ -4864,7 +4864,7 @@ "type": "github" } ], - "time": "2022-08-29T06:55:37+00:00" + "time": "2022-09-12T14:47:03+00:00" }, { "name": "sebastian/version", @@ -5384,5 +5384,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } From 4a73b26821ba1f0cd2b70baa46a3c0d3bde3eb65 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 14 Sep 2022 10:19:56 +0200 Subject: [PATCH 3/3] chore: composer update --- composer.lock | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 5ddf3f956e..85801ee5f1 100644 --- a/composer.lock +++ b/composer.lock @@ -2060,16 +2060,16 @@ }, { "name": "utopia-php/database", - "version": "0.25.3", + "version": "0.25.4", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "40c5cd4762b0c647df8fdb21813392b8ef5d211b" + "reference": "2883de82eee99e5744bf6e4123095a530c48a194" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/40c5cd4762b0c647df8fdb21813392b8ef5d211b", - "reference": "40c5cd4762b0c647df8fdb21813392b8ef5d211b", + "url": "https://api.github.com/repos/utopia-php/database/zipball/2883de82eee99e5744bf6e4123095a530c48a194", + "reference": "2883de82eee99e5744bf6e4123095a530c48a194", "shasum": "" }, "require": { @@ -2118,9 +2118,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.25.3" + "source": "https://github.com/utopia-php/database/tree/0.25.4" }, - "time": "2022-09-13T18:37:36+00:00" + "time": "2022-09-14T06:22:33+00:00" }, { "name": "utopia-php/domains", @@ -4124,16 +4124,16 @@ }, { "name": "sebastian/comparator", - "version": "4.0.6", + "version": "4.0.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "55f4261989e546dc112258c7a75935a81a7ce382" + "reference": "7fa545db548c90bdebeb9da0583001a252be5578" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/55f4261989e546dc112258c7a75935a81a7ce382", - "reference": "55f4261989e546dc112258c7a75935a81a7ce382", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/7fa545db548c90bdebeb9da0583001a252be5578", + "reference": "7fa545db548c90bdebeb9da0583001a252be5578", "shasum": "" }, "require": { @@ -4186,7 +4186,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", - "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.6" + "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.7" }, "funding": [ { @@ -4194,7 +4194,7 @@ "type": "github" } ], - "time": "2020-10-26T15:49:45+00:00" + "time": "2022-09-14T06:33:43+00:00" }, { "name": "sebastian/complexity", @@ -4384,16 +4384,16 @@ }, { "name": "sebastian/exporter", - "version": "4.0.4", + "version": "4.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "65e8b7db476c5dd267e65eea9cab77584d3cfff9" + "reference": "ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/65e8b7db476c5dd267e65eea9cab77584d3cfff9", - "reference": "65e8b7db476c5dd267e65eea9cab77584d3cfff9", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d", + "reference": "ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d", "shasum": "" }, "require": { @@ -4449,7 +4449,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", - "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.4" + "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.5" }, "funding": [ { @@ -4457,7 +4457,7 @@ "type": "github" } ], - "time": "2021-11-11T14:18:36+00:00" + "time": "2022-09-14T06:03:37+00:00" }, { "name": "sebastian/global-state",