3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
26 use Friendica\Core\Lock\ILock;
30 * tool to access the locks from the CLI
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.
35 class Lock extends \Asika\SimpleConsole\Console
37 protected $helpOptions = ['h', 'help', '?'];
49 protected function getHelp()
52 console lock - Manage node locks
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]
60 bin/console lock list [<prefix>]
61 List all locks, optionally filtered by a prefix
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.
66 bin/console lock del <lock>
69 bin/console lock clear
73 -h|--help|-? Show help information
74 -v Show more debug information.
79 public function __construct(App\Mode $appMode, ILock $lock, array $argv = null)
81 parent::__construct($argv);
83 $this->appMode = $appMode;
87 protected function doExecute()
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));
96 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
97 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
100 if ($this->getOption('v')) {
101 $this->out('Lock Driver Name: ' . $this->lock->getName());
102 $this->out('Lock Driver Class: ' . get_class($this->lock));
105 switch ($this->getArgument(0)) {
107 $this->executeList();
116 $this->executeClear();
120 if (count($this->args) == 0) {
121 $this->out($this->getHelp());
128 private function executeList()
130 $prefix = $this->getArgument(1, '');
131 $keys = $this->lock->getLocks($prefix);
133 if (empty($prefix)) {
134 $this->out('Listing all Locks:');
136 $this->out('Listing all Locks starting with "' . $prefix . '":');
140 foreach ($keys as $key) {
145 $this->out($count . ' locks found');
148 private function executeDel()
150 if (count($this->args) >= 2) {
151 $lock = $this->getArgument(1);
153 if ($this->lock->release($lock, true)) {
154 $this->out(sprintf('Lock \'%s\' released.', $lock));
156 $this->out(sprintf('Couldn\'t release Lock \'%s\'', $lock));
160 throw new CommandArgsException('Too few arguments for del.');
164 private function executeSet()
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));
171 if ($this->lock->isLocked($lock)) {
172 throw new RuntimeException(sprintf('\'%s\' is already set.', $lock));
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);
180 $result = $this->lock->acquire($lock);
184 $this->out(sprintf('Lock \'%s\' acquired.', $lock));
186 throw new RuntimeException(sprintf('Unable to lock \'%s\'.', $lock));
189 throw new CommandArgsException('Too few arguments for set.');
193 private function executeClear()
195 $result = $this->lock->releaseAll(true);
197 $this->out('Locks successfully cleared.');
199 throw new RuntimeException('Unable to clear the locks.');