-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrei Neagu
committed
Oct 23, 2023
1 parent
63ed455
commit 40bc314
Showing
7 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
#!/home/jovyan/.venv/bin/python | ||
|
||
# prints the result of the inactivity command | ||
|
||
import requests | ||
|
||
r = requests.get("http://localhost:9000") | ||
print(r.text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/home/jovyan/.venv/bin/python | ||
|
||
|
||
import asyncio | ||
import json | ||
import requests | ||
from datetime import datetime | ||
import tornado | ||
from contextlib import suppress | ||
from typing import Final | ||
|
||
|
||
KERNEL_BUSY_CHECK_INTERVAL_S: Final[float] = 5 | ||
|
||
|
||
class JupyterKernelChecker: | ||
BASE_URL = "http://localhost:8888" | ||
HEADERS = {"accept": "application/json"} | ||
|
||
def __init__(self) -> None: | ||
self.last_busy: datetime| None = None | ||
|
||
def _get(self, path: str) -> dict: | ||
r = requests.get(f'{self.BASE_URL}{path}', headers=self.HEADERS) | ||
return r.json() | ||
|
||
def _are_kernels_busy(self)-> bool: | ||
json_response = self._get("/api/kernels") | ||
|
||
are_kernels_busy = False | ||
|
||
for kernel_data in json_response: | ||
kernel_id = kernel_data["id"] | ||
|
||
kernel_info = self._get(f"/api/kernels/{kernel_id}") | ||
if kernel_info["execution_state"] != "idle": | ||
are_kernels_busy = True | ||
|
||
return are_kernels_busy | ||
|
||
def check(self): | ||
are_kernels_busy = self._are_kernels_busy() | ||
print(f"{are_kernels_busy=}") | ||
|
||
if not are_kernels_busy: | ||
self.last_busy = None | ||
|
||
if are_kernels_busy and self.last_busy is None: | ||
self.last_busy = datetime.utcnow() | ||
|
||
|
||
def get_idle_seconds(self)-> float: | ||
if self.last_busy is None: | ||
return 0 | ||
|
||
return (datetime.utcnow() - self.last_busy).total_seconds() | ||
|
||
async def run(self): | ||
while True: | ||
with suppress(Exception): | ||
self.check() | ||
await asyncio.sleep(KERNEL_BUSY_CHECK_INTERVAL_S) | ||
|
||
|
||
|
||
kernel_checker = JupyterKernelChecker() | ||
|
||
|
||
class MainHandler(tornado.web.RequestHandler): | ||
def get(self): | ||
idle_seconds = kernel_checker.get_idle_seconds() | ||
response = ( | ||
{"is_inactive": True, "seconds_inactive" : idle_seconds} | ||
if idle_seconds > 0 else | ||
{"is_inactive": False, "seconds_inactive" : None} | ||
) | ||
self.write(json.dumps(response)) | ||
|
||
|
||
def make_app()-> tornado.web.Application: | ||
return tornado.web.Application([(r"/", MainHandler)]) | ||
|
||
async def main(): | ||
app = make_app() | ||
app.listen(9000) | ||
asyncio.create_task(kernel_checker.run()) | ||
await asyncio.Event().wait() | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |