Skip to content

Commit a11d721

Browse files
committed
updated the way modules were run
1 parent 590c6b2 commit a11d721

File tree

6 files changed

+87
-38
lines changed

6 files changed

+87
-38
lines changed

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
run={
2-
"hello_world":{"directory":"hello_world", "restart":True}
2+
"hello_world":{"directory":"hello_world", "restart":False, "main_method":"main"}
33
}

manager.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import modules.commands as commands
44
from modules.worker import Worker
55
from modules.service import Service
6-
#
6+
from modules.utils import *
7+
#externam imports
78
import logging as log
89
from importlib import import_module
910
import config
@@ -12,12 +13,13 @@
1213
worker = Worker()
1314

1415
def __init__():
15-
i = 0
1616
global dynamic_imports
17-
log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
17+
# log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
18+
log.basicConfig(level=log.DEBUG)
19+
check_config()
1820
for service_name in config.run.keys():
19-
worker.add(Service(service_name, config.run[service_name]['directory'], i))
20-
i += 1
21+
service_conf = config.run[service_name]
22+
worker.add(Service(service_name, service_conf))
2123

2224
def handle_input(command):
2325
command = command.strip()
@@ -33,7 +35,7 @@ def handle_input(command):
3335
def main():
3436
__init__()
3537
worker.start()
36-
while handle_input(input()) is True:
38+
while handle_input(input("# ")) is True:
3739
continue
3840
log.info('All tasks done. Stopping')
3941
return 0

modules/commands.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
commands = ["quit", "list"]
1+
commands = ["quit", "list", "start"]
22

33
def quit(worker, args=None):
44
return 0
@@ -8,11 +8,17 @@ def help(worker, args=None):
88
print("%s not in commands : %s" %(command, commands))
99

1010
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
11+
for service in worker.services:
12+
print("[%i] | %s | Alive : %s" %(service.id, service.name, service.isAlive()))
1613

14+
def start(worker, args=None):
15+
if len(args) > 1:
16+
id = int(args[1])
17+
service = worker.getService(id)
18+
if not service.isAlive():
19+
service.restart()
20+
else:
21+
log.warning("Service %i is already running" %id)
22+
1723
def kill(worker, args=None):
1824
worker.kill(int(args[1]))

modules/service.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
from importlib import import_module
2+
from threading import Thread
23
import run
34

45
class Service():
5-
def __init__(self, name, directory, id):
6-
self.name = name
7-
self.directory = directory
8-
self.id = id
6+
def __init__(self, service_name, service_conf):
7+
self.name = service_name
8+
self.directory = service_conf['directory']
9+
self.keepAlive = service_conf['restart']
10+
self.thread = Thread(target=self.run, args=[])
911

10-
def run(self):
11-
self.start()
12+
def setId(self, id):
13+
self.id = id
1214

13-
def start(self):
15+
def run(self):
1416
import_module("run.%s.%s" %(self.directory, self.name))
15-
command = "run.%s.%s.main()" %(self.directory, self.name)
16-
eval(command)
17-
17+
command = "run.%s.%s.main" %(self.directory, self.name)
18+
if self.keepAlive:
19+
while self.keepAlive:
20+
eval(command)()
21+
else:
22+
eval(command)()
23+
24+
def start(self):
25+
self.thread.start()
26+
27+
def restart(self, thread=None):
28+
if thread is not None:
29+
self.thread = thread
30+
else:
31+
self.thread = Thread(target=self.run, args=[])
32+
self.thread.start()
33+
34+
def isAlive(self):
35+
return self.thread.isAlive()

modules/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import config
2+
import logging as log
3+
from sys import exit
4+
5+
def check_config():
6+
mandatory_in_services = ["restart", "directory", "main_method"]
7+
try:
8+
for service_name in config.run.keys():
9+
service_conf = config.run[service_name]
10+
for mandatory in mandatory_in_services:
11+
if mandatory not in service_conf.keys():
12+
log.error("Your service configuration must contain the attributes %s. %s is missing %s" %(mandatory_in_services, service_name, mandatory))
13+
exit(-1)
14+
15+
except AttributeError as e:
16+
log.error("Your config.py hasn't a correct format : [%s]" %str(e))
17+
exit(-1)
18+

modules/worker.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
from threading import Thread
1+
import logging as log
22

33
class Worker():
44
def __init__(self):
5-
self.threads = []
65
self.services = []
7-
6+
self.lastId = 0
7+
8+
def getService(self, id):
9+
for service in self.services:
10+
if service.id == id:
11+
return service
12+
log.warning("There is no service with id %i" %id)
13+
return None
14+
815
def start(self):
916
for service in self.services:
10-
t = Thread(target=service.run, args=[])
11-
self.threads.append(t)
12-
t.start()
17+
log.info("Starting service %s" %service.name)
18+
service.start()
1319

1420
def add(self, service):
21+
log.info("Added service %s" %service.name)
22+
self.lastId += 1
23+
service.setId(self.lastId)
1524
self.services.append(service)
1625

1726
def kill(self, id):
18-
self.threads[id].interrupt_main()
19-
self.services.pop(id)
20-
self.threads.pop(id)
21-
22-
def get_service(self, id):
23-
for service in self.services:
24-
if service.id == id:
25-
return service
26-
return None
27+
service = self.get_service(id)
28+
if service is None:
29+
log.warning("There is no service with id %i" %id)
30+
return
31+
service.kill()

0 commit comments

Comments
 (0)