Skip to content

Commit 2a98b92

Browse files
committed
watch files
1 parent 61dcdae commit 2a98b92

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎Pipfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ lambda-gateway = {editable = true,path = "."}
1818
aiohttp = "*"
1919
nest_asyncio = "*"
2020
ruamel.yaml = "*"
21+
watchfiles = "*"

‎lambda_gateway/__main__.py‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from aiohttp import web
88
import asyncio
99
import nest_asyncio
10+
from watchfiles import awatch
1011

1112
from lambda_gateway.event_proxy import EventProxy
1213
from lambda_gateway.request_handler import LambdaRequestHandler
@@ -64,14 +65,20 @@ def get_opts():
6465
default='2.0',
6566
help='API Gateway payload version [default: 2.0]',
6667
)
68+
parser.add_argument(
69+
'-w', '--watch',
70+
dest='watch',
71+
help='Watch the base python path and exit if files change.',
72+
action="store_true"
73+
)
6774
parser.add_argument(
6875
'SAM_TEMPLATE',
6976
help='Path to SAM YAML template',
7077
)
7178
return parser.parse_args()
7279

7380

74-
async def run_server(app, bind, port):
81+
async def run_server(app, bind, port, path, quit_on_change=True):
7582
"""
7683
Run Lambda Gateway server.
7784
"""
@@ -80,8 +87,19 @@ async def run_server(app, bind, port):
8087
site = web.TCPSite(runner, bind, port)
8188
await site.start()
8289

83-
while True:
84-
await asyncio.sleep(3600) # sleep forever
90+
stop_event = asyncio.Event()
91+
92+
# Wait for a source file to change, then quit
93+
async for changes in awatch(path, stop_event=stop_event):
94+
print(f"Source file changed: {changes}")
95+
if quit_on_change:
96+
print('Exiting so you can reload')
97+
stop_event.set() # `break` would probably be enough BTW
98+
else:
99+
print("No reload or quit - try -w flag")
100+
101+
await runner.cleanup()
102+
return True
85103

86104

87105
def main():
@@ -108,12 +126,9 @@ def main():
108126

109127
app.add_routes(routes)
110128

111-
# Start server
112-
#web.run_app(app, host=opts.bind, port=opts.port)
113-
114-
asyncio.run(run_server(app, opts.bind, opts.port))
115-
129+
asyncio.run(run_server(app, opts.bind, opts.port, base_python_path, opts.watch))
116130

131+
os._exit(0) # OS exit because awatch thread seems to still be locked; without this it hangs
117132

118133

119134
if __name__ == '__main__': # pragma: no cover

‎lambda_gateway/event_proxy.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def get_handler(self):
2626
try:
2727
sys.path.append(self.base_python_path)
2828
module = importlib.import_module(name)
29+
importlib.reload(module)
2930
handler = getattr(module, func)
3031
return handler
3132
except ModuleNotFoundError:

0 commit comments

Comments
 (0)