-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
121 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
# coding: utf-8 | ||
import os | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
from rerwatcher.app import RerWatcher | ||
|
||
logger.add("log/file_{time}.log", rotation="00:00", retention="2 days") | ||
log_success = f'{os.path.dirname(__file__)}/../log/rerwatcher.success.log' | ||
log_error = f'{os.path.dirname(__file__)}/../log/rerwatcher.error.log' | ||
|
||
logger.remove() | ||
|
||
logger.add(log_success, rotation='00:00', retention='2 days', level='DEBUG') | ||
logger.add(log_error, rotation='00:00', retention='2 days', level='ERROR') | ||
|
||
daemon = RerWatcher( | ||
pidfile='/tmp/rerwatcher.pid', | ||
stdout=log_success, | ||
stderr=log_error | ||
) | ||
|
||
if len(sys.argv) != 2: | ||
print(f"Usage: {sys.argv[0]} [start|stop].", file=sys.stderr) | ||
raise SystemExit(1) | ||
|
||
rer_watcher = RerWatcher() | ||
rer_watcher.start() | ||
if 'start' == sys.argv[1]: | ||
daemon.start() | ||
elif 'stop' == sys.argv[1]: | ||
daemon.stop() | ||
else: | ||
print(f"Unknown command {sys.argv[1]!r}.", file=sys.stderr) | ||
raise SystemExit(1) |
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,80 @@ | ||
import atexit | ||
import os | ||
import signal | ||
import sys | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Daemon(ABC): | ||
def __init__(self, | ||
pidfile: str, | ||
app_name: str, | ||
stdin: str = '/dev/null', | ||
stdout: str = '/dev/null', | ||
stderr: str = '/dev/null' | ||
): | ||
self.pidfile = pidfile | ||
self.app_name = app_name | ||
self.stdin = stdin | ||
self.stdout = stdout | ||
self.stderr = stderr | ||
|
||
def daemonize(self): | ||
if self._is_running(): | ||
raise RuntimeError(f"{self.app_name} is already Running.") | ||
|
||
try: | ||
if os.fork() > 0: | ||
raise SystemExit(0) | ||
except OSError: | ||
raise RuntimeError("Fork #1 failed.") | ||
|
||
os.chdir('/') | ||
os.umask(0) | ||
os.setsid() | ||
|
||
try: | ||
if os.fork() > 0: | ||
raise SystemExit(0) | ||
except OSError: | ||
raise RuntimeError("Fork #2 failed.") | ||
|
||
sys.stdout.flush() | ||
sys.stderr.flush() | ||
|
||
with open(self.stdin, 'rb') as f: | ||
os.dup2(f.fileno(), sys.stdin.fileno()) | ||
with open(self.stdout, 'ab') as f: | ||
os.dup2(f.fileno(), sys.stdout.fileno()) | ||
with open(self.stderr, 'ab') as f: | ||
os.dup2(f.fileno(), sys.stderr.fileno()) | ||
|
||
with open(self.pidfile, 'w') as f: | ||
print(os.getpid(), file=f) | ||
|
||
atexit.register(lambda: os.remove(self.pidfile)) | ||
|
||
def sigterm_handler(signo, frame): | ||
raise SystemExit(1) | ||
|
||
signal.signal(signal.SIGTERM, sigterm_handler) | ||
|
||
def _is_running(self): | ||
return os.path.exists(self.pidfile) | ||
|
||
def start(self): | ||
self.daemonize() | ||
self.run() | ||
|
||
def stop(self): | ||
if not self._is_running(): | ||
print(f"{self.app_name} is not running.") | ||
raise SystemExit(1) | ||
|
||
with open(self.pidfile) as f: | ||
pid = int(f.read()) | ||
os.kill(pid, signal.SIGTERM) | ||
|
||
@abstractmethod | ||
def run(self): | ||
raise NotImplementedError |
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