From 2204bb3988ac0817f086b106bc33776328bdcbc6 Mon Sep 17 00:00:00 2001
From: Justus Dieckmann <justusdieckmann@wwu.de>
Date: Thu, 31 Oct 2019 11:14:46 +0100
Subject: [PATCH] Add task that cleans up old delays

---
 classes/task/lifecycle_cleanup_task.php | 56 +++++++++++++++++++++++++
 db/tasks.php                            |  9 ++++
 lang/en/tool_lifecycle.php              |  1 +
 version.php                             |  2 +-
 4 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 classes/task/lifecycle_cleanup_task.php

diff --git a/classes/task/lifecycle_cleanup_task.php b/classes/task/lifecycle_cleanup_task.php
new file mode 100644
index 0000000..7a8f6f9
--- /dev/null
+++ b/classes/task/lifecycle_cleanup_task.php
@@ -0,0 +1,56 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Scheduled task for cleanup past delays
+ *
+ * @package tool_lifecycle
+ * @copyright  2019 Justus Dieckmann WWU
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace tool_lifecycle\task;
+
+defined('MOODLE_INTERNAL') || die;
+
+/**
+ * Scheduled task for cleanup past delays
+ *
+ * @package tool_lifecycle
+ * @copyright  2019 Justus Dieckmann WWU
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class lifecycle_cleanup_task extends \core\task\scheduled_task {
+
+    /**
+     * Get a descriptive name for this task (shown to admins).
+     *
+     * @return string
+     * @throws \coding_exception
+     */
+    public function get_name() {
+        return get_string('lifecycle_cleanup_task', 'tool_lifecycle');
+    }
+
+    /**
+     * Do the job.
+     */
+    public function execute() {
+        global $DB;
+        $twomonthago = time() - 60 * 24 * 60 * 60;
+        $DB->delete_records_select('tool_lifecycle_delayed', 'delayeduntil <= :time', array('time' => $twomonthago));
+        $DB->delete_records_select('tool_lifecycle_delayed_workf', 'delayeduntil <= :time', array('time' => $twomonthago));
+    }
+}
\ No newline at end of file
diff --git a/db/tasks.php b/db/tasks.php
index 22611f5..227744d 100644
--- a/db/tasks.php
+++ b/db/tasks.php
@@ -35,5 +35,14 @@ $tasks = array(
         'month' => '*',
         'dayofweek' => '*',
         'faildelay' => 1,
+    ),
+    array(
+        'classname' => 'tool_lifecycle\task\lifecycle_cleanup_task',
+        'blocking' => 0,
+        'minute' => '0',
+        'hour' => '4',
+        'day' => '*',
+        'month' => '*',
+        'dayofweek' => '0',
     )
 );
\ No newline at end of file
diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php
index 875144a..f657848 100644
--- a/lang/en/tool_lifecycle.php
+++ b/lang/en/tool_lifecycle.php
@@ -60,6 +60,7 @@ $string['cannot_trigger_workflow_manually'] = 'The requested workflow could not
 $string['error_wrong_trigger_selected'] = 'You tried to request a non-manual trigger.';
 
 $string['lifecycle_task'] = 'Run the life cycle processes';
+$string['lifecycle_cleanup_task'] = 'Runs the life cycle cleanup';
 
 $string['trigger_subpluginname'] = 'Subplugin name';
 $string['trigger_subpluginname_help'] = 'Step subplugin/trigger title (visible for admins only).';
diff --git a/version.php b/version.php
index 03546b6..68e10b1 100644
--- a/version.php
+++ b/version.php
@@ -25,7 +25,7 @@
 defined('MOODLE_INTERNAL') || die;
 
 $plugin->maturity = MATURITY_BETA;
-$plugin->version  = 2019102900;
+$plugin->version  = 2019103100;
 $plugin->component = 'tool_lifecycle';
 $plugin->requires = 2017111300; // Require Moodle 3.4 (or above).
 $plugin->release = 'v3.7-r1';
-- 
GitLab