WIP: Add locking of borgmatic config file #254
|
@ -42,26 +42,30 @@ def run_configuration(config_filename, config, arguments):
|
||||||
* JSON output strings from successfully executing any actions that produce JSON
|
* JSON output strings from successfully executing any actions that produce JSON
|
||||||
* logging.LogRecord instances containing errors from any actions or backup hooks that fail
|
* logging.LogRecord instances containing errors from any actions or backup hooks that fail
|
||||||
'''
|
'''
|
||||||
|
|
||||||
(location, storage, retention, consistency, hooks) = (
|
(location, storage, retention, consistency, hooks) = (
|
||||||
config.get(section_name, {})
|
config.get(section_name, {})
|
||||||
for section_name in ('location', 'storage', 'retention', 'consistency', 'hooks')
|
for section_name in ('location', 'storage', 'retention', 'consistency', 'hooks')
|
||||||
)
|
)
|
||||||
global_arguments = arguments['global']
|
global_arguments = arguments['global']
|
||||||
|
|
||||||
if location.get("lock_client",False):
|
|
||||||
lock_f = open(config_filename)
|
|
||||||
try:
|
|
||||||
fcntl.flock(lock_f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
||||||
except IOError:
|
|
||||||
logger.critical("Failed to acquire lock for {}".format(config_filename))
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
local_path = location.get('local_path', 'borg')
|
local_path = location.get('local_path', 'borg')
|
||||||
remote_path = location.get('remote_path')
|
remote_path = location.get('remote_path')
|
||||||
borg_environment.initialize(storage)
|
borg_environment.initialize(storage)
|
||||||
|
|||||||
encountered_error = None
|
encountered_error = None
|
||||||
error_repository = ''
|
error_repository = ''
|
||||||
|
|
||||||
witten
commented
Given that this is in
... and then this would go through the rest of the borgmatic logging/error/summary machinery. Another option would be this:
Which would fall through and trigger the Given that this is in `run_configuration()` now, you can just:
```
except IOError as error:
yield from make_error_log_records(
'{}: Failed to acquire lock'.format(config_filename), error
)
return
```
... and then this would go through the rest of the borgmatic logging/error/summary machinery.
Another option would be this:
```
except IOError as error:
encountered_error = error
yield from make_error_log_records(
'{}: Failed to acquire lock'.format(config_filename), error
)
```
Which would fall through and trigger the `on_error` hook, if that's what you want to do in this case.
|
|||||||
|
if location.get("lock_client",False):
|
||||||
witten
commented
Make sure you run tests with Make sure you run tests with `tox`, as the code formatter will probably complain about this. To have it reformat things for you: `tox -e black` is all you need to do.
|
|||||||
|
lock_f = open(config_filename)
|
||||||
|
try:
|
||||||
|
fcntl.flock(lock_f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
except IOError as error:
|
||||||
|
encountered_error = error
|
||||||
|
yield from make_error_log_records(
|
||||||
|
'{}: Failed to acquire lock'.format(config_filename), error
|
||||||
|
)
|
||||||
|
|
||||||
|
if not encountered_error:
|
||||||
if 'create' in arguments:
|
if 'create' in arguments:
|
||||||
witten
commented
These can be combined into a single These can be combined into a single `if` statement.
|
|||||||
try:
|
try:
|
||||||
dispatch.call_hooks(
|
dispatch.call_hooks(
|
||||||
|
|
Loading…
Reference in New Issue
Forgive me if this is a duplicate comment.. Is there a reason this is non-blocking? Seems like we'd want it to synchronously lock to be absolutely sure that no other borgmatic is running.
Non blocking here indicates that we don’t want to block the borgmatic and wait for a lock to be freed if the file is currently locked. I think you can do blocking with a timeout but it doesn’t seem that beneficial to me since borgmatic is typically a long running operation so the lock is unlikely to be free in a short amount of time. I think borg uses a timeout so I guess we could replicate that behavior.
Oh, gotcha. I originally misunderstood what
LOCK_NB
did. I see now that you're using it here to error immediately if a lock is in place, which makes sense. Carry on!