Skip to content

Commit 0e78e03

Browse files
committed
updated commands
1 parent 6b0fbac commit 0e78e03

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

manager.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
#!/bin/python
2+
#local imports
3+
import modules.commands as commands
4+
from modules.worker import Worker
5+
from modules.service import Service
6+
#
27
import logging as log
38
from importlib import import_module
49
import services
510
import config
6-
from modules.worker import Worker
11+
712

813
LOG_FILENAME = 'python_manager.log'
914
dynamic_imports = []
1015
worker = Worker()
1116

12-
class Service():
13-
def __init__(self, name):
14-
self.name = name
15-
1617
def __init__():
18+
i = 0
1719
global dynamic_imports
1820
log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
1921
for service in config.run.keys():
20-
dynamic_imports.append(import_module('services.%s' %service))
21-
22+
dynamic_imports.append(import_module('services.%s' %service))
23+
for service_name in config.run.keys():
24+
worker.add(Service(service_name, i))
25+
i += 1
26+
27+
def handle_input(command):
28+
command = command.strip()
29+
command = command.split(" ")
30+
if command[0] not in commands.commands:
31+
commands.help(worker, command)
32+
else:
33+
output = eval("commands.%s" %command[0])(worker, command)
34+
if output is not None:
35+
return False
36+
return True
37+
2238
def main():
2339
__init__()
24-
for service_name in config.run.keys():
25-
worker.add(Service(service_name))
2640
worker.start()
41+
while handle_input(input()) is True:
42+
continue
2743
log.info('All tasks done. Stopping')
2844
return 0
2945

modules/commands.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
commands = ["quit", "list"]
2+
3+
def quit(worker, args=None):
4+
return 0
5+
6+
def help(worker, args=None):
7+
command = args[0]
8+
print("%s not in commands : %s" %(command, commands))
9+
10+
def list(worker, args=None):
11+
i = 0
12+
for thread in worker.threads:
13+
service = worker.get_service(i)
14+
print("[%i] | %s | %s" %(service.id, thread, service.name))
15+
i += 1
16+
17+
def kill(worker, args=None):
18+
worker.kill(int(args[1]))

modules/service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import services
2+
3+
class Service():
4+
def __init__(self, name, id):
5+
self.name = name
6+
self.id = id
7+
8+
def run(self):
9+
self.start()
10+
11+
def start(self):
12+
eval("services.%s.main()" %self.name)
13+

modules/worker.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ class Worker():
55
def __init__(self):
66
self.threads = []
77
self.services = []
8-
9-
def run_service(self, service):
10-
eval("services.%s.main()" %service.name)
118

129
def start(self):
1310
for service in self.services:
14-
t = Thread(target=self.run_service, args=[service])
11+
t = Thread(target=service.run, args=[])
1512
self.threads.append(t)
1613
t.start()
1714

1815
def add(self, service):
1916
self.services.append(service)
20-
2117

18+
def kill(self, id):
19+
self.threads[id].interrupt_main()
20+
self.services.pop(id)
21+
self.threads.pop(id)
22+
23+
def get_service(self, id):
24+
for service in self.services:
25+
if service.id == id:
26+
return service
27+
return None

0 commit comments

Comments
 (0)