]> git.mxchange.org Git - friendica.git/blob - src/Console/Lock.php
The value is used twice, so use a variable
[friendica.git] / src / Console / Lock.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\App;
7 use Friendica\Core\Lock\ILock;
8 use RuntimeException;
9
10 /**
11  * @brief tool to access the locks from the CLI
12  *
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.
15  *
16  * @author Philipp Holzer <admin@philipp.info>, Hypolite Petovan <hypolite@mrpetovan.com>
17  */
18 class Lock extends \Asika\SimpleConsole\Console
19 {
20         protected $helpOptions = ['h', 'help', '?'];
21
22         /**
23          * @var App\Mode
24          */
25         private $appMode;
26
27         /**
28          * @var ILock
29          */
30         private $lock;
31
32         protected function getHelp()
33         {
34                 $help = <<<HELP
35 console lock - Manage node locks
36 Synopsis
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]
41
42 Description
43         bin/console lock list [<prefix>]
44                 List all locks, optionally filtered by a prefix
45
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.
48
49         bin/console lock del <lock>
50                 Deletes a lock.
51
52         bin/console lock clear
53                 Clears all locks
54
55 Options
56     -h|--help|-? Show help information
57     -v           Show more debug information.
58 HELP;
59                 return $help;
60         }
61
62         public function __construct(App\Mode $appMode, ILock $lock, array $argv = null)
63         {
64                 parent::__construct($argv);
65
66                 $this->appMode = $appMode;
67                 $this->lock    = $lock;
68         }
69
70         protected function doExecute()
71         {
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));
77                 }
78
79                 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
80                         $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
81                 }
82
83                 if ($this->getOption('v')) {
84                         $this->out('Lock Driver Name: ' . $this->lock->getName());
85                         $this->out('Lock Driver Class: ' . get_class($this->lock));
86                 }
87
88                 switch ($this->getArgument(0)) {
89                         case 'list':
90                                 $this->executeList();
91                                 break;
92                         case 'set':
93                                 $this->executeSet();
94                                 break;
95                         case 'del':
96                                 $this->executeDel();
97                                 break;
98                         case 'clear':
99                                 $this->executeClear();
100                                 break;
101                 }
102
103                 if (count($this->args) == 0) {
104                         $this->out($this->getHelp());
105                         return 0;
106                 }
107
108                 return 0;
109         }
110
111         private function executeList()
112         {
113                 $prefix = $this->getArgument(1, '');
114                 $keys   = $this->lock->getLocks($prefix);
115
116                 if (empty($prefix)) {
117                         $this->out('Listing all Locks:');
118                 } else {
119                         $this->out('Listing all Locks starting with "' . $prefix . '":');
120                 }
121
122                 $count = 0;
123                 foreach ($keys as $key) {
124                         $this->out($key);
125                         $count++;
126                 }
127
128                 $this->out($count . ' locks found');
129         }
130
131         private function executeDel()
132         {
133                 if (count($this->args) >= 2) {
134                         $lock = $this->getArgument(1);
135
136                         if ($this->lock->releaseLock($lock, true)) {
137                                 $this->out(sprintf('Lock \'%s\' released.', $lock));
138                         } else {
139                                 $this->out(sprintf('Couldn\'t release Lock \'%s\'', $lock));
140                         }
141
142                 } else {
143                         throw new CommandArgsException('Too few arguments for del.');
144                 }
145         }
146
147         private function executeSet()
148         {
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));
153
154                         if ($this->lock->isLocked($lock)) {
155                                 throw new RuntimeException(sprintf('\'%s\' is already set.', $lock));
156                         }
157
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);
162                         } else {
163                                 $result = $this->lock->acquireLock($lock);
164                         }
165
166                         if ($result) {
167                                 $this->out(sprintf('Lock \'%s\' acquired.', $lock));
168                         } else {
169                                 throw new RuntimeException(sprintf('Unable to lock \'%s\'.', $lock));
170                         }
171                 } else {
172                         throw new CommandArgsException('Too few arguments for set.');
173                 }
174         }
175
176         private function executeClear()
177         {
178                 $result = $this->lock->releaseAll(true);
179                 if ($result) {
180                         $this->out('Locks successfully cleared.');
181                 } else {
182                         throw new RuntimeException('Unable to clear the locks.');
183                 }
184         }
185 }