Skip to content

Commit

Permalink
Merge pull request #20 from zauberzeug/ignore_mutex
Browse files Browse the repository at this point in the history
Introduce --ignore-mutex option
  • Loading branch information
falkoschindler authored Jan 1, 2024
2 parents f654d8b + a0f7429 commit e8f3a0d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Options:
command to be executed on remote host after any file change (default: None)
- `--mutex-interval MUTEX_INTERVAL`
interval in which mutex is updated (default: 10 seconds)
- `--ignore-mutex`
ignore mutex (use with caution) (default: False)

### Python

Expand Down
3 changes: 2 additions & 1 deletion livesync/livesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ def main():
parser.add_argument('--ssh-port', type=int, default=22, help='SSH port on target')
parser.add_argument('--on-change', type=str, help='command to be executed on remote host after any file change')
parser.add_argument('--mutex-interval', type=int, default=10, help='interval in which mutex is updated')
parser.add_argument('--ignore-mutex', action='store_true', help='ignore mutex (use with caution)')
parser.add_argument('rsync_args', nargs=argparse.REMAINDER, help='arbitrary rsync parameters after "--"')
args = parser.parse_args()

folder = Folder(args.source, args.target, ssh_port=args.ssh_port, on_change=args.on_change)
folder.rsync_args(' '.join(args.rsync_args))
sync(folder, mutex_interval=args.mutex_interval)
sync(folder, mutex_interval=args.mutex_interval, ignore_mutex=args.ignore_mutex)


if __name__ == '__main__':
Expand Down
30 changes: 16 additions & 14 deletions livesync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ def get_summary(folders: Iterable[Folder]) -> str:
return '\n'.join(folder.get_summary() for folder in folders).replace('"', '\'')


async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float) -> None:
async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float, ignore_mutex: bool = False) -> None:
try:
summary = get_summary(folders)
mutexes = {folder.host: Mutex(folder.host, folder.ssh_port) for folder in folders}
for mutex in mutexes.values():
print(f'Checking mutex on {mutex.host}', flush=True)
if not mutex.set(summary):
print(f'Target is in use by {mutex.occupant}')
sys.exit(1)
if not ignore_mutex:
summary = get_summary(folders)
mutexes = {folder.host: Mutex(folder.host, folder.ssh_port) for folder in folders}
for mutex in mutexes.values():
print(f'Checking mutex on {mutex.host}', flush=True)
if not mutex.set(summary):
print(f'Target is in use by {mutex.occupant}')
sys.exit(1)

for folder in folders:
print(f' {folder.source_path} --> {folder.target}', flush=True)
Expand All @@ -29,17 +30,18 @@ async def run_folder_tasks(folders: Iterable[Folder], mutex_interval: float) ->
asyncio.create_task(folder.watch())

while True:
summary = get_summary(folders)
for mutex in mutexes.values():
if not mutex.set(summary):
break
if not ignore_mutex:
summary = get_summary(folders)
for mutex in mutexes.values():
if not mutex.set(summary):
break
await asyncio.sleep(mutex_interval)
except Exception as e:
print(e)


def sync(*folders: Folder, mutex_interval: float = 10) -> None:
def sync(*folders: Folder, mutex_interval: float = 10, ignore_mutex: bool = False) -> None:
try:
asyncio.run(run_folder_tasks(folders, mutex_interval))
asyncio.run(run_folder_tasks(folders, mutex_interval, ignore_mutex=ignore_mutex))
except KeyboardInterrupt:
print('Bye!')

0 comments on commit e8f3a0d

Please sign in to comment.