Skip to content

Commit

Permalink
feat: Hook up back sync functionality on a background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
nickderobertis committed Jul 17, 2022
1 parent 7418fd7 commit 4b31e08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
21 changes: 21 additions & 0 deletions flexlate_dev/server/back_sync.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
import time
from pathlib import Path
from typing import Optional, Union
Expand Down Expand Up @@ -143,8 +144,21 @@ def __init__(
self.template_path, search_parent_directories=True
)
self.last_commit = get_last_commit_sha(self.project_repo)
self.thread: Optional[threading.Thread] = None

def start(self):
if self.thread is not None:
raise RuntimeError("Already started")
# Run start_sync on a background thread
self.thread = threading.Thread(target=self.start_sync)
self.thread.start()

def stop(self):
if self.thread is not None:
self.thread.join()
self.thread = None

def start_sync(self):
while True:
new_commit = get_last_commit_sha(self.project_repo)
if self.last_commit == new_commit:
Expand All @@ -165,3 +179,10 @@ def sync(self, new_commit: Optional[str] = None):
commit_in_one_repo_with_another_repo_commit_message(
self.project_repo, self.template_repo, new_commit
)

def __enter__(self) -> "BackSyncServer":
self.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
19 changes: 17 additions & 2 deletions flexlate_dev/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from watchdog.observers import Observer

from flexlate_dev.config import FlexlateDevConfig, load_config
from flexlate_dev.server.back_sync import BackSyncServer
from flexlate_dev.server.sync import ServerEventHandler, create_sync_server
from flexlate_dev.styles import INFO_STYLE, SUCCESS_STYLE, print_styled

Expand Down Expand Up @@ -54,6 +55,8 @@ def run_server(
back_sync: bool = False,
no_input: bool = False,
auto_commit: bool = True,
back_sync_auto_commit: bool = True,
back_sync_check_interval_seconds: int = 1,
save: bool = False,
data: Optional[TemplateData] = None,
folder_name: Optional[str] = None,
Expand All @@ -78,12 +81,24 @@ def run_server(
folder_name=folder_name,
) as sync_manager:

out_folder = sync_manager.handler.out_path

print_styled(
f"Running auto-reloader, updating {sync_manager.handler.out_path} with changes to {template_path}",
f"Running auto-reloader, updating {out_folder} with changes to {template_path}",
SUCCESS_STYLE,
)

yield
if back_sync:
with BackSyncServer(
template_path,
out_folder,
sync_manager,
auto_commit=back_sync_auto_commit,
check_interval_seconds=back_sync_check_interval_seconds,
) as back_sync_manager:
yield
else:
yield

if temp_file is not None:
temp_file.cleanup()

0 comments on commit 4b31e08

Please sign in to comment.