File tree Expand file tree Collapse file tree 4 files changed +67
-14
lines changed Expand file tree Collapse file tree 4 files changed +67
-14
lines changed Original file line number Diff line number Diff line change 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+ #
27import logging as log
38from importlib import import_module
49import services
510import config
6- from modules . worker import Worker
11+
712
813LOG_FILENAME = 'python_manager.log'
914dynamic_imports = []
1015worker = Worker ()
1116
12- class Service ():
13- def __init__ (self , name ):
14- self .name = name
15-
1617def __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+
2238def 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
Original file line number Diff line number Diff line change 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 ]))
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments