3 namespace Friendica\Console;
5 use Asika\SimpleConsole\CommandArgsException;
7 use Friendica\Core\Lock\ILock;
11 * @brief tool to access the locks from the CLI
13 * With this script you can access the locks of your node from the CLI.
14 * You can read current locks and set/remove locks.
16 * @author Philipp Holzer <admin@philipp.info>, Hypolite Petovan <hypolite@mrpetovan.com>
18 class Lock extends \Asika\SimpleConsole\Console
20 protected $helpOptions = ['h', 'help', '?'];
32 protected function getHelp()
35 console lock - Manage node locks
37 bin/console lock list [<prefix>] [-h|--help|-?] [-v]
38 bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
39 bin/console lock del <lock> [-h|--help|-?] [-v]
40 bin/console lock clear [-h|--help|-?] [-v]
43 bin/console lock list [<prefix>]
44 List all locks, optionally filtered by a prefix
46 bin/console lock set <lock> [<timeout> [<ttl>]]
47 Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
49 bin/console lock del <lock>
52 bin/console lock clear
56 -h|--help|-? Show help information
57 -v Show more debug information.
62 public function __construct(App\Mode $appMode, ILock $lock, array $argv = null)
64 parent::__construct($argv);
66 $this->appMode = $appMode;
70 protected function doExecute()
72 if ($this->getOption('v')) {
73 $this->out('Executable: ' . $this->executable);
74 $this->out('Class: ' . __CLASS__);
75 $this->out('Arguments: ' . var_export($this->args, true));
76 $this->out('Options: ' . var_export($this->options, true));
79 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
80 $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
83 if ($this->getOption('v')) {
84 $this->out('Lock Driver Name: ' . $this->lock->getName());
85 $this->out('Lock Driver Class: ' . get_class($this->lock));
88 switch ($this->getArgument(0)) {
99 $this->executeClear();
103 if (count($this->args) == 0) {
104 $this->out($this->getHelp());
111 private function executeList()
113 $prefix = $this->getArgument(1, '');
114 $keys = $this->lock->getLocks($prefix);
116 if (empty($prefix)) {
117 $this->out('Listing all Locks:');
119 $this->out('Listing all Locks starting with "' . $prefix . '":');
123 foreach ($keys as $key) {
128 $this->out($count . ' locks found');
131 private function executeDel()
133 if (count($this->args) >= 2) {
134 $lock = $this->getArgument(1);
136 if ($this->lock->releaseLock($lock, true)) {
137 $this->out(sprintf('Lock \'%s\' released.', $lock));
139 $this->out(sprintf('Couldn\'t release Lock \'%s\'', $lock));
143 throw new CommandArgsException('Too few arguments for del.');
147 private function executeSet()
149 if (count($this->args) >= 2) {
150 $lock = $this->getArgument(1);
151 $timeout = intval($this->getArgument(2, false));
152 $ttl = intval($this->getArgument(3, false));
154 if ($this->lock->isLocked($lock)) {
155 throw new RuntimeException(sprintf('\'%s\' is already set.', $lock));
158 if (!empty($ttl) && !empty($timeout)) {
159 $result = $this->lock->acquireLock($lock, $timeout, $ttl);
160 } elseif (!empty($timeout)) {
161 $result = $this->lock->acquireLock($lock, $timeout);
163 $result = $this->lock->acquireLock($lock);
167 $this->out(sprintf('Lock \'%s\' acquired.', $lock));
169 throw new RuntimeException(sprintf('Unable to lock \'%s\'.', $lock));
172 throw new CommandArgsException('Too few arguments for set.');
176 private function executeClear()
178 $result = $this->lock->releaseAll(true);
180 $this->out('Locks successfully cleared.');
182 throw new RuntimeException('Unable to clear the locks.');