]> git.mxchange.org Git - friendica.git/blob - src/Console/Lock.php
API: Accept "redirect_uris" as both array and string
[friendica.git] / src / Console / Lock.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\App;
26 use Friendica\Core\Lock\Capability\ICanLock;
27 use RuntimeException;
28
29 /**
30  * tool to access the locks from the CLI
31  *
32  * With this script you can access the locks of your node from the CLI.
33  * You can read current locks and set/remove locks.
34  */
35 class Lock extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var App\Mode
41          */
42         private $appMode;
43
44         /**
45          * @var ICanLock
46          */
47         private $lock;
48
49         protected function getHelp()
50         {
51                 $help = <<<HELP
52 console lock - Manage node locks
53 Synopsis
54         bin/console lock list [<prefix>] [-h|--help|-?] [-v]
55         bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
56         bin/console lock del <lock> [-h|--help|-?] [-v]
57         bin/console lock clear [-h|--help|-?] [-v]
58
59 Description
60         bin/console lock list [<prefix>]
61                 List all locks, optionally filtered by a prefix
62
63         bin/console lock set <lock> [<timeout> [<ttl>]]
64                 Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
65
66         bin/console lock del <lock>
67                 Deletes a lock.
68
69         bin/console lock clear
70                 Clears all locks
71
72 Options
73     -h|--help|-? Show help information
74     -v           Show more debug information.
75 HELP;
76                 return $help;
77         }
78
79         public function __construct(App\Mode $appMode, ICanLock $lock, array $argv = null)
80         {
81                 parent::__construct($argv);
82
83                 $this->appMode = $appMode;
84                 $this->lock    = $lock;
85         }
86
87         protected function doExecute(): int
88         {
89                 if ($this->getOption('v')) {
90                         $this->out('Executable: ' . $this->executable);
91                         $this->out('Class: ' . __CLASS__);
92                         $this->out('Arguments: ' . var_export($this->args, true));
93                         $this->out('Options: ' . var_export($this->options, true));
94                 }
95
96                 if (!$this->appMode->has(App\Mode::DBAVAILABLE)) {
97                         $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
98                 }
99
100                 if ($this->getOption('v')) {
101                         $this->out('Lock Driver Name: ' . $this->lock->getName());
102                         $this->out('Lock Driver Class: ' . get_class($this->lock));
103                 }
104
105                 switch ($this->getArgument(0)) {
106                         case 'list':
107                                 $this->executeList();
108                                 break;
109                         case 'set':
110                                 $this->executeSet();
111                                 break;
112                         case 'del':
113                                 $this->executeDel();
114                                 break;
115                         case 'clear':
116                                 $this->executeClear();
117                                 break;
118                 }
119
120                 if (count($this->args) == 0) {
121                         $this->out($this->getHelp());
122                         return 0;
123                 }
124
125                 return 0;
126         }
127
128         private function executeList()
129         {
130                 $prefix = $this->getArgument(1, '');
131                 $keys   = $this->lock->getLocks($prefix);
132
133                 if (empty($prefix)) {
134                         $this->out('Listing all Locks:');
135                 } else {
136                         $this->out('Listing all Locks starting with "' . $prefix . '":');
137                 }
138
139                 $count = 0;
140                 foreach ($keys as $key) {
141                         $this->out($key);
142                         $count++;
143                 }
144
145                 $this->out($count . ' locks found');
146         }
147
148         private function executeDel()
149         {
150                 if (count($this->args) >= 2) {
151                         $lock = $this->getArgument(1);
152
153                         if ($this->lock->release($lock, true)) {
154                                 $this->out(sprintf('Lock \'%s\' released.', $lock));
155                         } else {
156                                 $this->out(sprintf('Couldn\'t release Lock \'%s\'', $lock));
157                         }
158
159                 } else {
160                         throw new CommandArgsException('Too few arguments for del.');
161                 }
162         }
163
164         private function executeSet()
165         {
166                 if (count($this->args) >= 2) {
167                         $lock    = $this->getArgument(1);
168                         $timeout = intval($this->getArgument(2, false));
169                         $ttl     = intval($this->getArgument(3, false));
170
171                         if ($this->lock->isLocked($lock)) {
172                                 throw new RuntimeException(sprintf('\'%s\' is already set.', $lock));
173                         }
174
175                         if (!empty($ttl) && !empty($timeout)) {
176                                 $result = $this->lock->acquire($lock, $timeout, $ttl);
177                         } elseif (!empty($timeout)) {
178                                 $result = $this->lock->acquire($lock, $timeout);
179                         } else {
180                                 $result = $this->lock->acquire($lock);
181                         }
182
183                         if ($result) {
184                                 $this->out(sprintf('Lock \'%s\' acquired.', $lock));
185                         } else {
186                                 throw new RuntimeException(sprintf('Unable to lock \'%s\'.', $lock));
187                         }
188                 } else {
189                         throw new CommandArgsException('Too few arguments for set.');
190                 }
191         }
192
193         private function executeClear()
194         {
195                 $result = $this->lock->releaseAll(true);
196                 if ($result) {
197                         $this->out('Locks successfully cleared.');
198                 } else {
199                         throw new RuntimeException('Unable to clear the locks.');
200                 }
201         }
202 }