-
Notifications
You must be signed in to change notification settings - Fork 126
/
cocod
executable file
·172 lines (140 loc) · 3.72 KB
/
cocod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import os
import subprocess
if os.environ.get('USE_EVENTLET', '1') == '1':
import eventlet
from eventlet.debug import hub_prevent_multiple_readers
eventlet.monkey_patch()
hub_prevent_multiple_readers(False)
print("Use eventlet dispatch")
else:
print("Use local threading")
import sys
import argparse
import time
import signal
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, BASE_DIR)
dirs = ('logs', 'keys')
for d in dirs:
d2 = os.path.join('data', d)
os.makedirs(d2, exist_ok=True)
try:
from coco import Coco
except ImportError as e:
print("Import error: {}".format(e))
print("Sys path: {}".format(sys.path))
print("Python is: ")
print(subprocess.call('which python', shell=True))
try:
import coco
print("Coco is: {}".format(coco))
print("Coco dir: {}".format(os.listdir("coco")))
except:
pass
raise
try:
from coco.conf import config
except ImportError:
sys.exit(1)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DAEMON = False
PID_FILE = os.path.join(BASE_DIR, 'coco.pid')
coco = Coco()
def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def start():
print("Start coco process")
if DAEMON:
fork_daemon()
with open(PID_FILE, 'w') as f:
f.write(str(os.getpid()))
coco.run_forever()
def stop():
print("Stop coco process")
pid = None
if os.path.isfile(PID_FILE):
with open(PID_FILE) as f:
pid = f.read().strip()
if pid and pid.isdigit():
for i in range(15):
try:
os.kill(int(pid), signal.SIGTERM)
except ProcessLookupError:
pass
if check_pid(int(pid)):
time.sleep(1)
continue
else:
os.unlink(PID_FILE)
break
def show_status():
pid = None
if os.path.isfile(PID_FILE):
with open(PID_FILE) as f:
pid = f.read().strip()
if pid and pid.isdigit() and check_pid(int(pid)):
print("Coco is running: {}".format(pid))
else:
print("Coco is stopped")
def fork_daemon():
try:
if os.fork() > 0:
sys.exit(0)
except OSError as e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
os.chdir(BASE_DIR)
os.setsid()
os.umask(0)
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError as e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
sys.stdout.flush()
sys.stderr.flush()
si = open('/dev/null', 'r')
so = open('/tmp/a.log', 'a')
se = open('/dev/null', 'a')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="""
coco service control tools;
Example: \r\n
%(prog)s start -d;
"""
)
parser.add_argument(
'action', type=str, default='start',
choices=("start", "stop", "restart", "status"),
help="Action to run"
)
parser.add_argument('-d', '--daemon', nargs="?", const=1)
args = parser.parse_args()
if args.daemon:
DAEMON = True
action = args.action
if action == "start":
start()
elif action == "stop":
stop()
elif action == "restart":
stop()
DAEMON = True
start()
else:
show_status()