|
1 | | -commands = ["quit", "list", "start", "kill"] |
| 1 | +available_commands = ["quit", "list", "start", "kill", "status"] |
2 | 2 |
|
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 |
5 | 12 |
|
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) |
9 | 21 |
|
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)) |
13 | 25 |
|
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): |
15 | 42 | if len(args) > 1: |
16 | 43 | id = int(args[1]) |
17 | 44 | service = worker.getService(id) |
| 45 | + if service is None: |
| 46 | + return "There isn't such service" |
18 | 47 | if not service.isAlive(): |
19 | 48 | service.restart() |
| 49 | + return "service started" |
20 | 50 | else: |
21 | 51 | 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): |
24 | 56 | if len(args) > 1: |
25 | 57 | worker.kill(int(args[1])) |
| 58 | + return "Service killed" |
| 59 | + return "Cannot find service" |
| 60 | + |
0 commit comments