Skip to content

Commit

Permalink
[DSW-2102] fix(tdk): Fix TDK watch mode termination
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Oct 6, 2023
1 parent f83fb1e commit f436799
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ if make verify; then
echo "## Verify: passed"
else
echo "## Verify: failed"
echo "VERIFY_FAILS=command-queue:$VERIFY_FAILS" >> $GITHUB_ENV
echo "VERIFY_FAILS=$1:$VERIFY_FAILS" >> $GITHUB_ENV
fi
if make test; then
echo "## Test: passed"
else
echo "## Test: failed"
echo "TEST_FAILS=command-queue:$TEST_FAILS" >> $GITHUB_ENV
echo "TEST_FAILS=$1:$TEST_FAILS" >> $GITHUB_ENV
fi
25 changes: 18 additions & 7 deletions packages/dsw-tdk/dsw/tdk/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import signal

import click # type: ignore
import datetime
Expand All @@ -8,7 +9,7 @@
import mimetypes
import pathlib
import slugify
import watchgod # type: ignore
import watchfiles # type: ignore

from typing import Dict

Expand All @@ -31,9 +32,9 @@
class ClickPrinter:

CHANGE_SIGNS = {
watchgod.Change.added: click.style('+', fg='green'),
watchgod.Change.modified: click.style('*', fg='yellow'),
watchgod.Change.deleted: click.style('-', fg='red'),
watchfiles.Change.added: click.style('+', fg='green'),
watchfiles.Change.modified: click.style('*', fg='yellow'),
watchfiles.Change.deleted: click.style('-', fg='red'),
}

@staticmethod
Expand Down Expand Up @@ -61,7 +62,7 @@ def watch(message: str):
click.echo(f': {message}')

@classmethod
def watch_change(cls, change_type: watchgod.Change, filepath: pathlib.Path, root: pathlib.Path):
def watch_change(cls, change_type: watchfiles.Change, filepath: pathlib.Path, root: pathlib.Path):
timestamp = datetime.datetime.now().isoformat(timespec='milliseconds')
sign = cls.CHANGE_SIGNS[change_type]
click.secho('WATCH', fg='blue', bold=True, nl=False)
Expand Down Expand Up @@ -348,6 +349,7 @@ async def main_routine():
@click.pass_context
def put_template(ctx, api_url, template_dir, api_key, force, watch):
tdk = TDKCore(logger=ctx.obj.logger)
stop_event = asyncio.Event()

async def watch_callback(changes):
changes = list(changes)
Expand All @@ -370,7 +372,7 @@ async def main_routine():

if watch:
ClickPrinter.watch('Entering watch mode... (press Ctrl+C to abort)')
await tdk.watch_project(watch_callback)
await tdk.watch_project(watch_callback, stop_event)

await tdk.safe_client.close()
except TDKProcessingError as e:
Expand All @@ -387,8 +389,17 @@ async def main_routine():
await tdk.safe_client.safe_close()
exit(1)

def set_stop_event(signum, frame):
signame = signal.Signals(signum).name
ClickPrinter.warning(f'Got {signame}, finishing... Bye!')
stop_event.set()

signal.signal(signal.SIGINT, set_stop_event)
signal.signal(signal.SIGABRT, set_stop_event)

loop = asyncio.get_event_loop()
loop.run_until_complete(main_routine())
main_task = asyncio.ensure_future(main_routine())
loop.run_until_complete(main_task)


@main.command(help='Create ZIP package for a template.', name='package')
Expand Down
21 changes: 14 additions & 7 deletions packages/dsw-tdk/dsw/tdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import re
import shutil
import tempfile
import watchgod # type: ignore
import watchfiles # type: ignore
import zipfile


from typing import List, Optional, Tuple

from .api_client import DSWAPIClient, DSWCommunicationError
Expand All @@ -19,7 +20,7 @@
from .validation import ValidationError, TemplateValidator


ChangeItem = Tuple[watchgod.Change, pathlib.Path]
ChangeItem = Tuple[watchfiles.Change, pathlib.Path]


class TDKProcessingError(RuntimeError):
Expand Down Expand Up @@ -81,6 +82,9 @@ def __init__(self, template: Optional[Template] = None, project: Optional[Templa
self.changes_processor = ChangesProcessor(self)
self.remote_id = 'unknown'

async def close(self):
await self.safe_client.close()

@property
def safe_template(self) -> Template:
if self.template is None:
Expand Down Expand Up @@ -368,8 +372,11 @@ def create_dot_env(self, output: pathlib.Path, force: bool, api_url: str, api_ke
encoding=DEFAULT_ENCODING,
)

async def watch_project(self, callback):
async for changes in watchgod.awatch(self.safe_project.template_dir):
async def watch_project(self, callback, stop_event: asyncio.Event):
async for changes in watchfiles.awatch(
self.safe_project.template_dir,
stop_event=stop_event,
):
await callback((
change for change in ((change[0], pathlib.Path(change[1])) for change in changes)
if self.safe_project.is_template_file(
Expand Down Expand Up @@ -460,7 +467,7 @@ async def _process_file_changes(self):
self.tdk.logger.debug(f'Processing {file_change}')
change_type = file_change[0]
filepath = file_change[1]
if change_type == watchgod.Change.deleted and filepath not in deleted:
if change_type == watchfiles.Change.deleted and filepath not in deleted:
self.tdk.logger.debug('Scheduling delete operation')
deleted.add(filepath)
await self.tdk._delete_file(filepath)
Expand All @@ -472,7 +479,7 @@ async def _process_file_changes(self):
async def _reload_descriptor(self, force: bool) -> bool:
if self.descriptor_change is None:
return False
if self.descriptor_change[0] == watchgod.Change.deleted:
if self.descriptor_change[0] == watchfiles.Change.deleted:
raise RuntimeError(f'Deleted template descriptor {self.tdk.safe_project.descriptor_path} ... the end')
self.tdk.logger.debug(f'Reloading {TemplateProject.TEMPLATE_FILE} file')
previous_id = self.tdk.safe_project.safe_template.id
Expand All @@ -490,7 +497,7 @@ async def _reload_descriptor(self, force: bool) -> bool:
async def _reload_readme(self) -> bool:
if self.readme_change is None:
return False
if self.readme_change[0] == watchgod.Change.deleted:
if self.readme_change[0] == watchfiles.Change.deleted:
raise RuntimeError(f'Deleted used README file {self.tdk.safe_project.used_readme}')
self.tdk.logger.debug('Reloading README file')
self.tdk.safe_project.load_readme()
Expand Down
2 changes: 1 addition & 1 deletion packages/dsw-tdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
'pathspec',
'python-dotenv',
'python-slugify',
'watchgod',
'watchfiles',
]

[project.optional-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions packages/dsw-tdk/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aiohttp==3.8.5
aiosignal==1.3.1
anyio==3.7.1
anyio==4.0.0
async-timeout==4.0.3
attrs==23.1.0
charset-normalizer==3.3.0
Expand All @@ -17,5 +17,5 @@ python-dotenv==1.0.0
python-slugify==8.0.1
sniffio==1.3.0
text-unidecode==1.3
watchgod==0.8.2
watchfiles==0.20.0
yarl==1.9.2
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aiohttp==3.8.5
aiosignal==1.3.1
anyio==3.7.1
anyio==4.0.0
async-timeout==4.0.3
attrs==23.1.0
Brotli==1.0.9
Expand Down Expand Up @@ -50,7 +50,6 @@ tinycss2==1.2.1
typing_extensions==4.8.0
tzdata==2023.3
urllib3==2.0.6
watchgod==0.8.2
weasyprint==60.1
webencodings==0.5.1
XlsxWriter==3.1.6
Expand Down

0 comments on commit f436799

Please sign in to comment.