Throw if lockfile can't be created for 20 seconds.

Also make create_lockfile() boolean.
This commit is contained in:
tastytea 2020-10-14 02:29:09 +02:00
parent 4a88652435
commit 2502081574
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 16 additions and 20 deletions

View File

@ -219,22 +219,20 @@ fs::path get_lockfile()
return path / "fediblock.lock"; return path / "fediblock.lock";
} }
lock_result create_lockfile() bool create_lockfile()
{ {
const string lockfile{get_lockfile()}; const string lockfile{get_lockfile()};
if (fs::exists(lockfile)) if (fs::exists(lockfile))
{ {
return lock_result::already_locked; return false;
} }
ofstream file{lockfile}; ofstream file;
if (file.good()) file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
{ file.open(lockfile);
file << getpid(); file << getpid();
return lock_result::created;
}
return lock_result::error; return true;
} }
void remove_lockfile() void remove_lockfile()

View File

@ -27,13 +27,6 @@ namespace FediBlock::files
using std::string; using std::string;
enum class lock_result
{
created,
already_locked,
error
};
// Create and return the temporary directory. // Create and return the temporary directory.
[[nodiscard]] fs::path get_tmpdir(); [[nodiscard]] fs::path get_tmpdir();
@ -73,8 +66,8 @@ void remove_tmpdir();
// Return path of lockfile. // Return path of lockfile.
[[nodiscard]] fs::path get_lockfile(); [[nodiscard]] fs::path get_lockfile();
// Attempt to create lockfile. // Attempt to create lockfile. Return true if created, false if already locked.
[[nodiscard]] lock_result create_lockfile(); [[nodiscard]] bool create_lockfile();
// Remove lockfile if it exists. // Remove lockfile if it exists.
void remove_lockfile(); void remove_lockfile();

View File

@ -220,13 +220,18 @@ string get_branch_name()
void update_cached_repo() void update_cached_repo()
{ {
bool lock_created{false};
for (uint8_t counter{0}; counter < 20; ++counter) for (uint8_t counter{0}; counter < 20; ++counter)
{ {
if (files::create_lockfile() == files::lock_result::created) if ((lock_created = files::create_lockfile()))
{ {
break; break;
} }
sleep_for(2s); sleep_for(1s);
}
if (!lock_created)
{
throw runtime_error{"Repository was locked for too long, giving up."};
} }
if (!fs::exists(_repo_dir / ".git")) if (!fs::exists(_repo_dir / ".git"))