diff --git a/catfish/utils/sockets.py b/catfish/utils/sockets.py index 1602b8f..e6e56a4 100644 --- a/catfish/utils/sockets.py +++ b/catfish/utils/sockets.py @@ -1,7 +1,12 @@ +import os import select +import shutil +import tempfile import ujson +BASE_SOCKET_DIR = os.path.join(tempfile.gettempdir(), "catfish") + BUFFER_SIZE = 4096 DEFAULT_SOCKET_READ_TIMEOUT = 0.01 NEW_LINE = b"\n" @@ -22,3 +27,11 @@ def read_all_from_socket(socket): break data += message return ujson.loads(data) + + +def create_base_socket_dir(): + os.mkdir(BASE_SOCKET_DIR) + + +def delete_base_socket_dir(): + shutil.rmtree(BASE_SOCKET_DIR) diff --git a/catfish/worker/__init__.py b/catfish/worker/__init__.py index 4b44796..ca5044a 100644 --- a/catfish/worker/__init__.py +++ b/catfish/worker/__init__.py @@ -1,16 +1,16 @@ import asyncio import os -import tempfile import time import psutil from catfish.router import start_router from catfish.utils.processes import terminate_processes +from catfish.utils.sockets import BASE_SOCKET_DIR from .server import WORKER_SERVER_SOCKET, start_server -PID_FILE = os.path.join(tempfile.gettempdir(), "catfish.pid") +PID_FILE = os.path.join(BASE_SOCKET_DIR, "catfish.pid") def is_running(): diff --git a/catfish/worker/server.py b/catfish/worker/server.py index 3ffbbda..89d141a 100644 --- a/catfish/worker/server.py +++ b/catfish/worker/server.py @@ -3,15 +3,14 @@ import os import shlex import socket import subprocess -import tempfile import time import click import ujson -from catfish.utils.sockets import NEW_LINE, read_all_from_socket +from catfish.utils.sockets import BASE_SOCKET_DIR, NEW_LINE, read_all_from_socket -WORKER_SERVER_SOCKET = os.path.join(tempfile.gettempdir(), "catfish.sock") +WORKER_SERVER_SOCKET = os.path.join(BASE_SOCKET_DIR, "catfish.sock") def send_to_server(payload): diff --git a/tests/__init__.py b/tests/__init__.py index a0355e4..69ac01e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -8,10 +8,12 @@ from click.testing import CliRunner from catfish import worker from catfish.__main__ import cli from catfish.utils.processes import CURRENT_PROCESS, terminate_processes +from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir class BaseTestCase(AsyncTestCase): def setUp(self): + create_base_socket_dir() worker.stop_worker() self.cli_runner = CliRunner() self.cli = cli @@ -20,6 +22,7 @@ class BaseTestCase(AsyncTestCase): def tearDown(self): worker.stop_worker() terminate_processes(CURRENT_PROCESS.children(recursive=True)) + delete_base_socket_dir() class BaseWorkerTestCase(BaseTestCase):