@@ -44,228 +44,228 @@ |
||
44 | 44 | use Symfony\Component\Console\Output\OutputInterface; |
45 | 45 | |
46 | 46 | class Notify extends Base { |
47 | - /** @var GlobalStoragesService */ |
|
48 | - private $globalService; |
|
49 | - /** @var IDBConnection */ |
|
50 | - private $connection; |
|
51 | - /** @var \OCP\DB\QueryBuilder\IQueryBuilder */ |
|
52 | - private $updateQuery; |
|
53 | - /** @var ILogger */ |
|
54 | - private $logger; |
|
47 | + /** @var GlobalStoragesService */ |
|
48 | + private $globalService; |
|
49 | + /** @var IDBConnection */ |
|
50 | + private $connection; |
|
51 | + /** @var \OCP\DB\QueryBuilder\IQueryBuilder */ |
|
52 | + private $updateQuery; |
|
53 | + /** @var ILogger */ |
|
54 | + private $logger; |
|
55 | 55 | |
56 | - function __construct(GlobalStoragesService $globalService, IDBConnection $connection, ILogger $logger) { |
|
57 | - parent::__construct(); |
|
58 | - $this->globalService = $globalService; |
|
59 | - $this->connection = $connection; |
|
60 | - $this->logger = $logger; |
|
61 | - $this->updateQuery = $this->getUpdateQuery($this->connection); |
|
62 | - } |
|
56 | + function __construct(GlobalStoragesService $globalService, IDBConnection $connection, ILogger $logger) { |
|
57 | + parent::__construct(); |
|
58 | + $this->globalService = $globalService; |
|
59 | + $this->connection = $connection; |
|
60 | + $this->logger = $logger; |
|
61 | + $this->updateQuery = $this->getUpdateQuery($this->connection); |
|
62 | + } |
|
63 | 63 | |
64 | - protected function configure() { |
|
65 | - $this |
|
66 | - ->setName('files_external:notify') |
|
67 | - ->setDescription('Listen for active update notifications for a configured external mount') |
|
68 | - ->addArgument( |
|
69 | - 'mount_id', |
|
70 | - InputArgument::REQUIRED, |
|
71 | - 'the mount id of the mount to listen to' |
|
72 | - )->addOption( |
|
73 | - 'user', |
|
74 | - 'u', |
|
75 | - InputOption::VALUE_REQUIRED, |
|
76 | - 'The username for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
77 | - )->addOption( |
|
78 | - 'password', |
|
79 | - 'p', |
|
80 | - InputOption::VALUE_REQUIRED, |
|
81 | - 'The password for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
82 | - )->addOption( |
|
83 | - 'path', |
|
84 | - '', |
|
85 | - InputOption::VALUE_REQUIRED, |
|
86 | - 'The directory in the storage to listen for updates in', |
|
87 | - '/' |
|
88 | - ); |
|
89 | - parent::configure(); |
|
90 | - } |
|
64 | + protected function configure() { |
|
65 | + $this |
|
66 | + ->setName('files_external:notify') |
|
67 | + ->setDescription('Listen for active update notifications for a configured external mount') |
|
68 | + ->addArgument( |
|
69 | + 'mount_id', |
|
70 | + InputArgument::REQUIRED, |
|
71 | + 'the mount id of the mount to listen to' |
|
72 | + )->addOption( |
|
73 | + 'user', |
|
74 | + 'u', |
|
75 | + InputOption::VALUE_REQUIRED, |
|
76 | + 'The username for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
77 | + )->addOption( |
|
78 | + 'password', |
|
79 | + 'p', |
|
80 | + InputOption::VALUE_REQUIRED, |
|
81 | + 'The password for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
82 | + )->addOption( |
|
83 | + 'path', |
|
84 | + '', |
|
85 | + InputOption::VALUE_REQUIRED, |
|
86 | + 'The directory in the storage to listen for updates in', |
|
87 | + '/' |
|
88 | + ); |
|
89 | + parent::configure(); |
|
90 | + } |
|
91 | 91 | |
92 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
93 | - $mount = $this->globalService->getStorage($input->getArgument('mount_id')); |
|
94 | - if (is_null($mount)) { |
|
95 | - $output->writeln('<error>Mount not found</error>'); |
|
96 | - return 1; |
|
97 | - } |
|
98 | - $noAuth = false; |
|
99 | - try { |
|
100 | - $authBackend = $mount->getAuthMechanism(); |
|
101 | - $authBackend->manipulateStorageConfig($mount); |
|
102 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
103 | - $noAuth = true; |
|
104 | - } catch (StorageNotAvailableException $e) { |
|
105 | - $noAuth = true; |
|
106 | - } |
|
92 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
93 | + $mount = $this->globalService->getStorage($input->getArgument('mount_id')); |
|
94 | + if (is_null($mount)) { |
|
95 | + $output->writeln('<error>Mount not found</error>'); |
|
96 | + return 1; |
|
97 | + } |
|
98 | + $noAuth = false; |
|
99 | + try { |
|
100 | + $authBackend = $mount->getAuthMechanism(); |
|
101 | + $authBackend->manipulateStorageConfig($mount); |
|
102 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
103 | + $noAuth = true; |
|
104 | + } catch (StorageNotAvailableException $e) { |
|
105 | + $noAuth = true; |
|
106 | + } |
|
107 | 107 | |
108 | - if ($input->getOption('user')) { |
|
109 | - $mount->setBackendOption('user', $input->getOption('user')); |
|
110 | - } else if (isset($_ENV['NOTIFY_USER'])) { |
|
111 | - $mount->setBackendOption('user', $_ENV['NOTIFY_USER']); |
|
112 | - } else if (isset($_SERVER['NOTIFY_USER'])) { |
|
113 | - $mount->setBackendOption('user', $_SERVER['NOTIFY_USER']); |
|
114 | - } |
|
115 | - if ($input->getOption('password')) { |
|
116 | - $mount->setBackendOption('password', $input->getOption('password')); |
|
117 | - } else if (isset($_ENV['NOTIFY_PASSWORD'])) { |
|
118 | - $mount->setBackendOption('password', $_ENV['NOTIFY_PASSWORD']); |
|
119 | - } else if (isset($_SERVER['NOTIFY_PASSWORD'])) { |
|
120 | - $mount->setBackendOption('password', $_SERVER['NOTIFY_PASSWORD']); |
|
121 | - } |
|
108 | + if ($input->getOption('user')) { |
|
109 | + $mount->setBackendOption('user', $input->getOption('user')); |
|
110 | + } else if (isset($_ENV['NOTIFY_USER'])) { |
|
111 | + $mount->setBackendOption('user', $_ENV['NOTIFY_USER']); |
|
112 | + } else if (isset($_SERVER['NOTIFY_USER'])) { |
|
113 | + $mount->setBackendOption('user', $_SERVER['NOTIFY_USER']); |
|
114 | + } |
|
115 | + if ($input->getOption('password')) { |
|
116 | + $mount->setBackendOption('password', $input->getOption('password')); |
|
117 | + } else if (isset($_ENV['NOTIFY_PASSWORD'])) { |
|
118 | + $mount->setBackendOption('password', $_ENV['NOTIFY_PASSWORD']); |
|
119 | + } else if (isset($_SERVER['NOTIFY_PASSWORD'])) { |
|
120 | + $mount->setBackendOption('password', $_SERVER['NOTIFY_PASSWORD']); |
|
121 | + } |
|
122 | 122 | |
123 | - try { |
|
124 | - $storage = $this->createStorage($mount); |
|
125 | - } catch (\Exception $e) { |
|
126 | - $output->writeln('<error>Error while trying to create storage</error>'); |
|
127 | - if ($noAuth) { |
|
128 | - $output->writeln('<error>Username and/or password required</error>'); |
|
129 | - } |
|
130 | - return 1; |
|
131 | - } |
|
132 | - if (!$storage instanceof INotifyStorage) { |
|
133 | - $output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>'); |
|
134 | - return 1; |
|
135 | - } |
|
123 | + try { |
|
124 | + $storage = $this->createStorage($mount); |
|
125 | + } catch (\Exception $e) { |
|
126 | + $output->writeln('<error>Error while trying to create storage</error>'); |
|
127 | + if ($noAuth) { |
|
128 | + $output->writeln('<error>Username and/or password required</error>'); |
|
129 | + } |
|
130 | + return 1; |
|
131 | + } |
|
132 | + if (!$storage instanceof INotifyStorage) { |
|
133 | + $output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>'); |
|
134 | + return 1; |
|
135 | + } |
|
136 | 136 | |
137 | - $verbose = $input->getOption('verbose'); |
|
137 | + $verbose = $input->getOption('verbose'); |
|
138 | 138 | |
139 | - $path = trim($input->getOption('path'), '/'); |
|
140 | - $notifyHandler = $storage->notify($path); |
|
141 | - $this->selfTest($storage, $notifyHandler, $verbose, $output); |
|
142 | - $notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) { |
|
143 | - if ($verbose) { |
|
144 | - $this->logUpdate($change, $output); |
|
145 | - } |
|
146 | - if ($change instanceof IRenameChange) { |
|
147 | - $this->markParentAsOutdated($mount->getId(), $change->getTargetPath(), $output); |
|
148 | - } |
|
149 | - $this->markParentAsOutdated($mount->getId(), $change->getPath(), $output); |
|
150 | - }); |
|
151 | - } |
|
139 | + $path = trim($input->getOption('path'), '/'); |
|
140 | + $notifyHandler = $storage->notify($path); |
|
141 | + $this->selfTest($storage, $notifyHandler, $verbose, $output); |
|
142 | + $notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) { |
|
143 | + if ($verbose) { |
|
144 | + $this->logUpdate($change, $output); |
|
145 | + } |
|
146 | + if ($change instanceof IRenameChange) { |
|
147 | + $this->markParentAsOutdated($mount->getId(), $change->getTargetPath(), $output); |
|
148 | + } |
|
149 | + $this->markParentAsOutdated($mount->getId(), $change->getPath(), $output); |
|
150 | + }); |
|
151 | + } |
|
152 | 152 | |
153 | - private function createStorage(StorageConfig $mount) { |
|
154 | - $class = $mount->getBackend()->getStorageClass(); |
|
155 | - return new $class($mount->getBackendOptions()); |
|
156 | - } |
|
153 | + private function createStorage(StorageConfig $mount) { |
|
154 | + $class = $mount->getBackend()->getStorageClass(); |
|
155 | + return new $class($mount->getBackendOptions()); |
|
156 | + } |
|
157 | 157 | |
158 | - private function markParentAsOutdated($mountId, $path, OutputInterface $output) { |
|
159 | - $parent = ltrim(dirname($path), '/'); |
|
160 | - if ($parent === '.') { |
|
161 | - $parent = ''; |
|
162 | - } |
|
158 | + private function markParentAsOutdated($mountId, $path, OutputInterface $output) { |
|
159 | + $parent = ltrim(dirname($path), '/'); |
|
160 | + if ($parent === '.') { |
|
161 | + $parent = ''; |
|
162 | + } |
|
163 | 163 | |
164 | - try { |
|
165 | - $this->updateQuery->execute([$parent, $mountId]); |
|
166 | - } catch (DriverException $ex) { |
|
167 | - $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while trying to mark folder as outdated', 'level' => ILogger::WARN]); |
|
168 | - $this->connection = $this->reconnectToDatabase($this->connection, $output); |
|
169 | - $output->writeln('<info>Needed to reconnect to the database</info>'); |
|
170 | - $this->updateQuery = $this->getUpdateQuery($this->connection); |
|
171 | - $this->updateQuery->execute([$parent, $mountId]); |
|
172 | - } |
|
173 | - } |
|
164 | + try { |
|
165 | + $this->updateQuery->execute([$parent, $mountId]); |
|
166 | + } catch (DriverException $ex) { |
|
167 | + $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while trying to mark folder as outdated', 'level' => ILogger::WARN]); |
|
168 | + $this->connection = $this->reconnectToDatabase($this->connection, $output); |
|
169 | + $output->writeln('<info>Needed to reconnect to the database</info>'); |
|
170 | + $this->updateQuery = $this->getUpdateQuery($this->connection); |
|
171 | + $this->updateQuery->execute([$parent, $mountId]); |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - private function logUpdate(IChange $change, OutputInterface $output) { |
|
176 | - switch ($change->getType()) { |
|
177 | - case INotifyStorage::NOTIFY_ADDED: |
|
178 | - $text = 'added'; |
|
179 | - break; |
|
180 | - case INotifyStorage::NOTIFY_MODIFIED: |
|
181 | - $text = 'modified'; |
|
182 | - break; |
|
183 | - case INotifyStorage::NOTIFY_REMOVED: |
|
184 | - $text = 'removed'; |
|
185 | - break; |
|
186 | - case INotifyStorage::NOTIFY_RENAMED: |
|
187 | - $text = 'renamed'; |
|
188 | - break; |
|
189 | - default: |
|
190 | - return; |
|
191 | - } |
|
175 | + private function logUpdate(IChange $change, OutputInterface $output) { |
|
176 | + switch ($change->getType()) { |
|
177 | + case INotifyStorage::NOTIFY_ADDED: |
|
178 | + $text = 'added'; |
|
179 | + break; |
|
180 | + case INotifyStorage::NOTIFY_MODIFIED: |
|
181 | + $text = 'modified'; |
|
182 | + break; |
|
183 | + case INotifyStorage::NOTIFY_REMOVED: |
|
184 | + $text = 'removed'; |
|
185 | + break; |
|
186 | + case INotifyStorage::NOTIFY_RENAMED: |
|
187 | + $text = 'renamed'; |
|
188 | + break; |
|
189 | + default: |
|
190 | + return; |
|
191 | + } |
|
192 | 192 | |
193 | - $text .= ' ' . $change->getPath(); |
|
194 | - if ($change instanceof IRenameChange) { |
|
195 | - $text .= ' to ' . $change->getTargetPath(); |
|
196 | - } |
|
193 | + $text .= ' ' . $change->getPath(); |
|
194 | + if ($change instanceof IRenameChange) { |
|
195 | + $text .= ' to ' . $change->getTargetPath(); |
|
196 | + } |
|
197 | 197 | |
198 | - $output->writeln($text); |
|
199 | - } |
|
198 | + $output->writeln($text); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @return \Doctrine\DBAL\Statement |
|
203 | - */ |
|
204 | - private function getUpdateQuery(IDBConnection $connection) { |
|
205 | - // the query builder doesn't really like subqueries with parameters |
|
206 | - return $connection->prepare( |
|
207 | - 'UPDATE *PREFIX*filecache SET size = -1 |
|
201 | + /** |
|
202 | + * @return \Doctrine\DBAL\Statement |
|
203 | + */ |
|
204 | + private function getUpdateQuery(IDBConnection $connection) { |
|
205 | + // the query builder doesn't really like subqueries with parameters |
|
206 | + return $connection->prepare( |
|
207 | + 'UPDATE *PREFIX*filecache SET size = -1 |
|
208 | 208 | WHERE `path` = ? |
209 | 209 | AND `storage` IN (SELECT storage_id FROM *PREFIX*mounts WHERE mount_id = ?)' |
210 | - ); |
|
211 | - } |
|
210 | + ); |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * @return \OCP\IDBConnection |
|
215 | - */ |
|
216 | - private function reconnectToDatabase(IDBConnection $connection, OutputInterface $output) { |
|
217 | - try { |
|
218 | - $connection->close(); |
|
219 | - } catch (\Exception $ex) { |
|
220 | - $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while disconnecting from DB', 'level' => ILogger::WARN]); |
|
221 | - $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
222 | - } |
|
223 | - while (!$connection->isConnected()) { |
|
224 | - try { |
|
225 | - $connection->connect(); |
|
226 | - } catch (\Exception $ex) { |
|
227 | - $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while re-connecting to database', 'level' => ILogger::WARN]); |
|
228 | - $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
229 | - sleep(60); |
|
230 | - } |
|
231 | - } |
|
232 | - return $connection; |
|
233 | - } |
|
213 | + /** |
|
214 | + * @return \OCP\IDBConnection |
|
215 | + */ |
|
216 | + private function reconnectToDatabase(IDBConnection $connection, OutputInterface $output) { |
|
217 | + try { |
|
218 | + $connection->close(); |
|
219 | + } catch (\Exception $ex) { |
|
220 | + $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while disconnecting from DB', 'level' => ILogger::WARN]); |
|
221 | + $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>"); |
|
222 | + } |
|
223 | + while (!$connection->isConnected()) { |
|
224 | + try { |
|
225 | + $connection->connect(); |
|
226 | + } catch (\Exception $ex) { |
|
227 | + $this->logger->logException($ex, ['app' => 'files_external', 'message' => 'Error while re-connecting to database', 'level' => ILogger::WARN]); |
|
228 | + $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>"); |
|
229 | + sleep(60); |
|
230 | + } |
|
231 | + } |
|
232 | + return $connection; |
|
233 | + } |
|
234 | 234 | |
235 | 235 | |
236 | - private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, $verbose, OutputInterface $output) { |
|
237 | - usleep(100 * 1000); //give time for the notify to start |
|
238 | - $storage->file_put_contents('/.nc_test_file.txt', 'test content'); |
|
239 | - $storage->mkdir('/.nc_test_folder'); |
|
240 | - $storage->file_put_contents('/.nc_test_folder/subfile.txt', 'test content'); |
|
236 | + private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, $verbose, OutputInterface $output) { |
|
237 | + usleep(100 * 1000); //give time for the notify to start |
|
238 | + $storage->file_put_contents('/.nc_test_file.txt', 'test content'); |
|
239 | + $storage->mkdir('/.nc_test_folder'); |
|
240 | + $storage->file_put_contents('/.nc_test_folder/subfile.txt', 'test content'); |
|
241 | 241 | |
242 | - usleep(100 * 1000); //time for all changes to be processed |
|
243 | - $changes = $notifyHandler->getChanges(); |
|
242 | + usleep(100 * 1000); //time for all changes to be processed |
|
243 | + $changes = $notifyHandler->getChanges(); |
|
244 | 244 | |
245 | - $storage->unlink('/.nc_test_file.txt'); |
|
246 | - $storage->unlink('/.nc_test_folder/subfile.txt'); |
|
247 | - $storage->rmdir('/.nc_test_folder'); |
|
245 | + $storage->unlink('/.nc_test_file.txt'); |
|
246 | + $storage->unlink('/.nc_test_folder/subfile.txt'); |
|
247 | + $storage->rmdir('/.nc_test_folder'); |
|
248 | 248 | |
249 | - usleep(100 * 1000); //time for all changes to be processed |
|
250 | - $notifyHandler->getChanges(); // flush |
|
249 | + usleep(100 * 1000); //time for all changes to be processed |
|
250 | + $notifyHandler->getChanges(); // flush |
|
251 | 251 | |
252 | - $foundRootChange = false; |
|
253 | - $foundSubfolderChange = false; |
|
252 | + $foundRootChange = false; |
|
253 | + $foundSubfolderChange = false; |
|
254 | 254 | |
255 | - foreach ($changes as $change) { |
|
256 | - if ($change->getPath() === '/.nc_test_file.txt' || $change->getPath() === '.nc_test_file.txt') { |
|
257 | - $foundRootChange = true; |
|
258 | - } else if ($change->getPath() === '/.nc_test_folder/subfile.txt' || $change->getPath() === '.nc_test_folder/subfile.txt') { |
|
259 | - $foundSubfolderChange = true; |
|
260 | - } |
|
261 | - } |
|
255 | + foreach ($changes as $change) { |
|
256 | + if ($change->getPath() === '/.nc_test_file.txt' || $change->getPath() === '.nc_test_file.txt') { |
|
257 | + $foundRootChange = true; |
|
258 | + } else if ($change->getPath() === '/.nc_test_folder/subfile.txt' || $change->getPath() === '.nc_test_folder/subfile.txt') { |
|
259 | + $foundSubfolderChange = true; |
|
260 | + } |
|
261 | + } |
|
262 | 262 | |
263 | - if ($foundRootChange && $foundSubfolderChange && $verbose) { |
|
264 | - $output->writeln('<info>Self-test successful</info>'); |
|
265 | - } else if ($foundRootChange && !$foundSubfolderChange) { |
|
266 | - $output->writeln('<error>Error while running self-test, change is subfolder not detected</error>'); |
|
267 | - } else if (!$foundRootChange) { |
|
268 | - $output->writeln('<error>Error while running self-test, no changes detected</error>'); |
|
269 | - } |
|
270 | - } |
|
263 | + if ($foundRootChange && $foundSubfolderChange && $verbose) { |
|
264 | + $output->writeln('<info>Self-test successful</info>'); |
|
265 | + } else if ($foundRootChange && !$foundSubfolderChange) { |
|
266 | + $output->writeln('<error>Error while running self-test, change is subfolder not detected</error>'); |
|
267 | + } else if (!$foundRootChange) { |
|
268 | + $output->writeln('<error>Error while running self-test, no changes detected</error>'); |
|
269 | + } |
|
270 | + } |
|
271 | 271 | } |