File tree Expand file tree Collapse file tree 6 files changed +87
-38
lines changed Expand file tree Collapse file tree 6 files changed +87
-38
lines changed Original file line number Diff line number Diff line change 1
1
run = {
2
- "hello_world" :{"directory" :"hello_world" , "restart" :True }
2
+ "hello_world" :{"directory" :"hello_world" , "restart" :False , "main_method" : "main" }
3
3
}
Original file line number Diff line number Diff line change 3
3
import modules .commands as commands
4
4
from modules .worker import Worker
5
5
from modules .service import Service
6
- #
6
+ from modules .utils import *
7
+ #externam imports
7
8
import logging as log
8
9
from importlib import import_module
9
10
import config
12
13
worker = Worker ()
13
14
14
15
def __init__ ():
15
- i = 0
16
16
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 ()
18
20
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 ))
21
23
22
24
def handle_input (command ):
23
25
command = command .strip ()
@@ -33,7 +35,7 @@ def handle_input(command):
33
35
def main ():
34
36
__init__ ()
35
37
worker .start ()
36
- while handle_input (input ()) is True :
38
+ while handle_input (input ("# " )) is True :
37
39
continue
38
40
log .info ('All tasks done. Stopping' )
39
41
return 0
Original file line number Diff line number Diff line change 1
- commands = ["quit" , "list" ]
1
+ commands = ["quit" , "list" , "start" ]
2
2
3
3
def quit (worker , args = None ):
4
4
return 0
@@ -8,11 +8,17 @@ def help(worker, args=None):
8
8
print ("%s not in commands : %s" % (command , commands ))
9
9
10
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
11
+ for service in worker .services :
12
+ print ("[%i] | %s | Alive : %s" % (service .id , service .name , service .isAlive ()))
16
13
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
+
17
23
def kill (worker , args = None ):
18
24
worker .kill (int (args [1 ]))
Original file line number Diff line number Diff line change 1
1
from importlib import import_module
2
+ from threading import Thread
2
3
import run
3
4
4
5
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 = [])
9
11
10
- def run (self ):
11
- self .start ()
12
+ def setId (self , id ):
13
+ self .id = id
12
14
13
- def start (self ):
15
+ def run (self ):
14
16
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 ()
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 1
- from threading import Thread
1
+ import logging as log
2
2
3
3
class Worker ():
4
4
def __init__ (self ):
5
- self .threads = []
6
5
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
+
8
15
def start (self ):
9
16
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 ()
13
19
14
20
def add (self , service ):
21
+ log .info ("Added service %s" % service .name )
22
+ self .lastId += 1
23
+ service .setId (self .lastId )
15
24
self .services .append (service )
16
25
17
26
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 ()
You can’t perform that action at this time.
0 commit comments