Skip to content

Commit 6d6c982

Browse files
committed
added server interface for the daemon
1 parent c5340e9 commit 6d6c982

File tree

6 files changed

+61
-60
lines changed

6 files changed

+61
-60
lines changed

config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
run={
2-
"hello_world":{"directory":"hello_world", "restart":False, "main_method":"main"},
3-
"tester":{"directory":"proxy_tester", "restart":False, "main_method":"main"}
2+
"hello_world":{"directory":"hello_world", "restart":False, "main_method":"main"}
3+
}
4+
5+
daemon={
6+
"port": 8889,
7+
"host": "127.0.0.1"
48
}

manager.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

modules/commands.py

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,60 @@
1-
commands = ["quit", "list", "start", "kill"]
1+
available_commands = ["quit", "list", "start", "kill", "status"]
22

3-
def quit(worker, args=None):
4-
return 0
3+
def handle_input(command, worker, server=None):
4+
command = command.strip()
5+
command = command.split(" ")
6+
if command[0] not in available_commands:
7+
return False, help(worker, command)
8+
else:
9+
print(command)
10+
output = eval("%s" %command[0])(worker, command, server)
11+
return True, output
512

6-
def help(worker, args=None):
7-
command = args[0]
8-
print("%s not in commands : %s" %(command, commands))
13+
def quit(worker, args=None, server=None):
14+
print("Quitting server")
15+
server.stop()
16+
print("Closing running services...")
17+
for service in worker.services:
18+
service.stop()
19+
print("Goodbye !\n")
20+
exit(0)
921

10-
def list(worker, args=None):
11-
for service in worker.services:
12-
print("[%i] | %s | Alive : %s" %(service.id, service.name, service.isAlive()))
22+
def help(worker, args=None, server=None):
23+
command = args[0]
24+
return ("%s not in commands : %s" %(command, available_commands))
1325

14-
def start(worker, args=None):
26+
def status(worker, args=None, server=None):
27+
if len(args) > 1:
28+
id = int(args[1])
29+
service = worker.getService(id)
30+
if service is None:
31+
return "There isn't such service"
32+
return "[%i] | %s | Alive : %s" %(service.id, service.name, service.isAlive())
33+
return "Invalid number of arguments"
34+
35+
def list(worker, args=None, server=None):
36+
strlist = ""
37+
for service in worker.services:
38+
strlist += "[%i] | %s | Alive : %s" %(service.id, service.name, service.isAlive())
39+
return strlist
40+
41+
def start(worker, args=None, server=None):
1542
if len(args) > 1:
1643
id = int(args[1])
1744
service = worker.getService(id)
45+
if service is None:
46+
return "There isn't such service"
1847
if not service.isAlive():
1948
service.restart()
49+
return "service started"
2050
else:
2151
log.warning("Service %i is already running" %id)
22-
23-
def kill(worker, args=None):
52+
return "Service is already running"
53+
return "Invalid number of arguments"
54+
55+
def kill(worker, args=None, server=None):
2456
if len(args) > 1:
2557
worker.kill(int(args[1]))
58+
return "Service killed"
59+
return "Cannot find service"
60+

modules/service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def run(self):
2525
def start(self):
2626
self.process.start()
2727

28+
def stop(self):
29+
self.process.terminate()
30+
2831
def restart(self, process=None):
2932
if process is not None:
3033
self.process = process

modules/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
def check_config():
66
mandatory_in_services = ["restart", "directory", "main_method"]
7+
mandatory_in_daemon = ["host", "port"]
78
try:
9+
for mandatory in mandatory_in_daemon:
10+
if mandatory not in config.daemon.keys():
11+
log.error("Your daemon configuration must containes the attributes %s. %s is missing" %(mandatory_in_daemon, mandatory))
812
for service_name in config.run.keys():
913
service_conf = config.run[service_name]
1014
for mandatory in mandatory_in_services:
@@ -15,4 +19,3 @@ def check_config():
1519
except AttributeError as e:
1620
log.error("Your config.py hasn't a correct format : [%s]" %str(e))
1721
exit(-1)
18-

modules/worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ def kill(self, id):
2828
if service is None:
2929
log.warning("There is no service with id %i" %id)
3030
return
31-
service.process.terminate()
31+
service.stop()

0 commit comments

Comments
 (0)