Skip to content

Commit

Permalink
Merge pull request #84 from tekktrik/dev/fix-fast-error
Browse files Browse the repository at this point in the history
Improve start speed, fix quick stop error
  • Loading branch information
tekktrik authored Oct 29, 2022
2 parents 4a27a25 + 3ed6b22 commit 887c099
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 3 additions & 7 deletions circlink/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import os
import signal
import time
from datetime import datetime, timedelta
from typing import Iterable, List

Expand Down Expand Up @@ -101,9 +100,8 @@ def start_backend(
# Attempt to wait for spawned process to be confirmed
start_time = datetime.now()
error_time = start_time + timedelta(seconds=5)
while not link.confirmed:
while not link or not link.confirmed:
link = CircuitPythonLink.load_link_by_num(link_id)
time.sleep(0.5) # Slight delay
if datetime.now() >= error_time:
try:
os.kill(pid, signal.SIGTERM)
Expand All @@ -116,9 +114,8 @@ def start_backend(
else: # Spawned process, PID is 0

# Wait for the process ID to be avaiable
while not link.process_id:
while not link or not link.process_id:
link = CircuitPythonLink.load_link_by_num(link_id)
time.sleep(0.3)

# Mark the link is confirmed and save it
link.confirmed = True
Expand Down Expand Up @@ -177,9 +174,8 @@ def stop_backend(link_id: int, *, hard_fault: bool = True) -> bool:
# Wait for confirmation that the link has stopped
start_time = datetime.now()
error_time = start_time + timedelta(seconds=5)
while not link.stopped:
while not link or not link.stopped:
link = CircuitPythonLink.load_link_by_num(link_id)
time.sleep(0.1) # Slight delay
if datetime.now() >= error_time:
print(f"Link #{link.link_id} could not be stopped!")
if hard_fault:
Expand Down
16 changes: 10 additions & 6 deletions circlink/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
Author(s): Alec Delaney (Tekktrik)
"""

import fcntl
import json
import os
import pathlib
import shutil
import time
from typing import Dict, List, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

from typer import Exit
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -144,16 +144,21 @@ def save_link(self, *, save_directory: str = LINKS_DIRECTORY) -> pathlib.Path:

# Save the object as a JSON file
with open(save_filepath, mode="w", encoding="utf-8") as linkfile:
fcntl.lockf(linkfile, fcntl.LOCK_EX)
json.dump(link_obj, linkfile, indent=4)
fcntl.lockf(linkfile, fcntl.LOCK_UN)

# Return the save filepath
return pathlib.Path(save_filepath)

@classmethod
def load_link_by_filepath(cls, link_filepath: str) -> "CircuitPythonLink":
def load_link_by_filepath(cls, link_filepath: str) -> Optional["CircuitPythonLink"]:
"""Create a CircuitPythonLink from a JSON file, by filepath."""
with open(link_filepath, encoding="utf-8") as linkfile:
link_obj = json.load(linkfile)
link_contents = linkfile.read()
if not link_contents:
return None
link_obj = json.loads(link_contents)

link = cls(
name=link_obj["name"],
Expand Down Expand Up @@ -237,11 +242,10 @@ def begin_monitoring(self) -> None:

# Load the link and repeatedly load while not flagged to stop
temp_link = self.load_link_by_num(self._link_id)
while not temp_link.end_flag:
while not temp_link or not temp_link.end_flag:

# Load the link
temp_link = self.load_link_by_num(self._link_id)
time.sleep(0.1)

# Detect new files
read_files = self.get_files_monitored()
Expand Down

0 comments on commit 887c099

Please sign in to comment.