1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 *
12 */
13
14use ComboStrap\Event;
15use ComboStrap\ExceptionTimeOut;
16use ComboStrap\Lock;
17
18
19/**
20 * Just start our own event system
21 */
22class action_plugin_combo_eventsystem extends DokuWiki_Action_Plugin
23{
24
25
26    const CANONICAL = "event";
27    private static Lock $taskRunnerlock;
28
29    public function register(Doku_Event_Handler $controller)
30    {
31
32        /**
33         * Process the event table
34         *
35         * We do it after because if there is an error
36         * It will not stop the Dokuwiki Processing
37         */
38        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'processEventTable', array());
39
40        /**
41         * https://forum.dokuwiki.org/d/21044-taskrunner-running-multiple-times-eating-the-memory-lock/5
42         */
43        $controller->register_hook('INDEXER_TASKS_RUN', 'BEFORE', $this, 'lockSystemBefore', array());
44        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'lockSystemAfter', array());
45
46
47    }
48
49    public function lockSystemBefore(Doku_Event $event, $param)
50    {
51        print 'ComboLockTaskRunner(): Trying to get a lock' . NL;
52        self::$taskRunnerlock = Lock::create("combo-taskrunner");
53        try {
54            self::$taskRunnerlock->acquire();
55        } catch (ExceptionTimeOut $e) {
56            // process running
57            print 'ComboLockTaskRunner(): Already running, not acquired' . NL;
58            exit;
59        }
60        print 'ComboLockTaskRunner(): Locked' . NL;
61    }
62
63    public function lockSystemAfter(Doku_Event $event, $param)
64    {
65        self::$taskRunnerlock->release();
66        print 'ComboLockTaskRunner(): Lock Released' . NL;
67    }
68
69    /**
70     */
71    public function processEventTable(Doku_Event $event, $param)
72    {
73
74
75        /**
76         * Process the async event
77         */
78        Event::dispatchEvent();
79
80
81    }
82
83
84}
85