Update signals.
This commit is contained in:
parent
5f06d7e04c
commit
015337d75c
src/snek
@ -3,6 +3,7 @@ import logging
|
|||||||
import pathlib
|
import pathlib
|
||||||
import ssl
|
import ssl
|
||||||
import uuid
|
import uuid
|
||||||
|
import signal
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from snek import snode
|
from snek import snode
|
||||||
@ -210,6 +211,10 @@ class Application(BaseApplication):
|
|||||||
# app.loop = asyncio.get_running_loop()
|
# app.loop = asyncio.get_running_loop()
|
||||||
app.executor = ThreadPoolExecutor(max_workers=200)
|
app.executor = ThreadPoolExecutor(max_workers=200)
|
||||||
app.loop.set_default_executor(self.executor)
|
app.loop.set_default_executor(self.executor)
|
||||||
|
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||||
|
app.loop.add_signal_handler(
|
||||||
|
sig, lambda: asyncio.create_task(self.services.container.shutdown())
|
||||||
|
)
|
||||||
|
|
||||||
async def create_task(self, task):
|
async def create_task(self, task):
|
||||||
await self.tasks.put(task)
|
await self.tasks.put(task)
|
||||||
|
@ -12,6 +12,9 @@ class ContainerService(BaseService):
|
|||||||
self.compose = ComposeFileManager(self.compose_path,self.container_event_handler)
|
self.compose = ComposeFileManager(self.compose_path,self.container_event_handler)
|
||||||
self.event_listeners = {}
|
self.event_listeners = {}
|
||||||
|
|
||||||
|
async def shutdown(self):
|
||||||
|
return await self.compose.shutdown()
|
||||||
|
|
||||||
async def add_event_listener(self, name, event,event_handler):
|
async def add_event_listener(self, name, event,event_handler):
|
||||||
if not name in self.event_listeners:
|
if not name in self.event_listeners:
|
||||||
self.event_listeners[name] = {}
|
self.event_listeners[name] = {}
|
||||||
|
@ -17,6 +17,17 @@ class ComposeFileManager:
|
|||||||
self.running_instances = {}
|
self.running_instances = {}
|
||||||
self.event_handler = event_handler
|
self.event_handler = event_handler
|
||||||
|
|
||||||
|
async def shutdown(self):
|
||||||
|
print("Stopping all sessions")
|
||||||
|
for name in self.list_instances():
|
||||||
|
proc = self.running_instances.get(name)
|
||||||
|
if not proc:
|
||||||
|
continue
|
||||||
|
if proc['proc'].returncode == None:
|
||||||
|
print("Stopping",name)
|
||||||
|
await proc['proc'].stop()
|
||||||
|
print("Stopped",name,"gracefully")
|
||||||
|
|
||||||
def _load(self):
|
def _load(self):
|
||||||
try:
|
try:
|
||||||
with open(self.compose_path) as f:
|
with open(self.compose_path) as f:
|
||||||
|
Loading…
Reference in New Issue
Block a user