Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow infinite lock through configuration #55

Merged
merged 1 commit into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ By default, files locked using this app will be unlocked after 30 minutes.

### Settings

Administrators can change the time of the maximum lock time (30) using the command:
Administrators can change the time of the maximum lock time in minutes (30) using the command:

`./occ config:app:set --value '30' files_lock lock_timeout`

Locks have no expiry by default.


### More commands
Expand Down
15 changes: 13 additions & 2 deletions lib/Command/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCA\FilesLock\Exceptions\NotFileException;
use OCA\FilesLock\Exceptions\SuccessException;
use OCA\FilesLock\Exceptions\UnauthorizedUnlockException;
use OCA\FilesLock\Model\FileLock;
use OCA\FilesLock\Service\ConfigService;
use OCA\FilesLock\Service\FileService;
use OCA\FilesLock\Service\LockService;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected function configure() {
* @throws NotFileException
* @throws InvalidPathException
*/
protected function execute(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output): int {
$fileId = (int)$input->getArgument('file_id');
$userId = $input->getArgument('user_id');

Expand All @@ -136,14 +137,16 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->getStatus($input, $output, $fileId);
$this->unlockFile($input, $output, $fileId);
} catch (SuccessException $e) {
return;
return 0;
}

if ($userId === '') {
throw new InvalidArgumentException('\'Not enough arguments (missing: "user_id")');
}

$this->lockFile($input, $output, $fileId, $userId);

return 0;
}


Expand All @@ -164,6 +167,14 @@ private function getStatus(InputInterface $input, OutputInterface $output, int $
$output->writeln(
'File #' . $fileId . ' is <comment>locked</comment> by ' . $lock->getUserId()
);
$output->writeln(
' - Locked at: ' . date('c', $lock->getCreation())
);
if ($lock->getETA() !== FileLock::ETA_INFINITE) {
$output->writeln(
' - Expiry in seconds: ' . $lock->getETA()
);
}
} catch (LockNotFoundException $e) {
$output->writeln('File #' . $fileId . ' is <info>not locked<info>');
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Model/FileLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class FileLock implements IQueryRow, JsonSerializable {

use TArrayTools;

public const ETA_INFINITE = -1;


/** @var int */
private $id = 0;
Expand Down Expand Up @@ -196,6 +198,9 @@ public function setTimeout(int $timeout): self {
* @return int
*/
public function getETA(): int {
if ($this->getTimeout() <= 0) {
return self::ETA_INFINITE;
}
$end = $this->getCreation() + $this->getTimeout();
$eta = $end - time();

Expand Down
2 changes: 1 addition & 1 deletion lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConfigService {
const LOCK_TIMEOUT = 'lock_timeout';

public $defaults = [
self::LOCK_TIMEOUT => '30'
self::LOCK_TIMEOUT => '0'
];

/** @var string */
Expand Down