We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 590c6b2 commit a11d721Copy full SHA for a11d721
config.py
@@ -1,3 +1,3 @@
1
run={
2
- "hello_world":{"directory":"hello_world", "restart":True}
+ "hello_world":{"directory":"hello_world", "restart":False, "main_method":"main"}
3
}
manager.py
@@ -3,7 +3,8 @@
import modules.commands as commands
4
from modules.worker import Worker
5
from modules.service import Service
6
-#
+from modules.utils import *
7
+#externam imports
8
import logging as log
9
from importlib import import_module
10
import config
@@ -12,12 +13,13 @@
12
13
worker = Worker()
14
15
def __init__():
- i = 0
16
global dynamic_imports
17
- log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
+ # log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
18
+ log.basicConfig(level=log.DEBUG)
19
+ check_config()
20
for service_name in config.run.keys():
- worker.add(Service(service_name, config.run[service_name]['directory'], i))
- i+=1
21
+ service_conf=config.run[service_name]
22
+ worker.add(Service(service_name, service_conf))
23
24
def handle_input(command):
25
command = command.strip()
@@ -33,7 +35,7 @@ def handle_input(command):
33
35
def main():
34
36
__init__()
37
worker.start()
- while handle_input(input()) is True:
38
+ while handle_input(input("# ")) is True:
39
continue
40
log.info('All tasks done. Stopping')
41
return 0
modules/commands.py
@@ -1,4 +1,4 @@
-commands = ["quit", "list"]
+commands = ["quit", "list", "start"]
def quit(worker, args=None):
@@ -8,11 +8,17 @@ def help(worker, args=None):
print("%s not in commands : %s" %(command, commands))
def list(worker, args=None):
11
- for thread in worker.threads:
- service = worker.get_service(i)
- print("[%i] | %s | %s" %(service.id, thread, service.name))
- i += 1
+ for service in worker.services:
+ print("[%i] | %s | Alive : %s" %(service.id, service.name, service.isAlive()))
+def start(worker, args=None):
+ if len(args) > 1:
+ id = int(args[1])
+ service = worker.getService(id)
+ if not service.isAlive():
+ service.restart()
+ else:
+ log.warning("Service %i is already running" %id)
+
def kill(worker, args=None):
worker.kill(int(args[1]))
modules/service.py
@@ -1,17 +1,35 @@
+from threading import Thread
import run
class Service():
- def __init__(self, name, directory, id):
- self.name = name
- self.directory = directory
- self.id = id
+ def __init__(self, service_name, service_conf):
+ self.name = service_name
+ self.directory = service_conf['directory']
+ self.keepAlive = service_conf['restart']
+ self.thread = Thread(target=self.run, args=[])
- def run(self):
- self.start()
+ def setId(self, id):
+ self.id=id
- def start(self):
+ def run(self):
import_module("run.%s.%s" %(self.directory, self.name))
- command = "run.%s.%s.main()" %(self.directory, self.name)
- eval(command)
-
+ command = "run.%s.%s.main" %(self.directory, self.name)
+ if self.keepAlive:
+ while self.keepAlive:
+ eval(command)()
+ def start(self):
+ self.thread.start()
26
27
+ def restart(self, thread=None):
28
+ if thread is not None:
29
+ self.thread = thread
30
31
32
+ def isAlive(self):
+ return self.thread.isAlive()
modules/utils.py
@@ -0,0 +1,18 @@
+import config
+import logging as log
+from sys import exit
+def check_config():
+ mandatory_in_services = ["restart", "directory", "main_method"]
+ try:
+ for service_name in config.run.keys():
+ service_conf = config.run[service_name]
+ for mandatory in mandatory_in_services:
+ if mandatory not in service_conf.keys():
+ log.error("Your service configuration must contain the attributes %s. %s is missing %s" %(mandatory_in_services, service_name, mandatory))
+ exit(-1)
+ except AttributeError as e:
+ log.error("Your config.py hasn't a correct format : [%s]" %str(e))
modules/worker.py
@@ -1,26 +1,31 @@
-fromthreadingimportThread
+importloggingaslog
class Worker():
def __init__(self):
- self.threads = []
self.services = []
+ self.lastId = 0
+ def getService(self, id):
+ for service in self.services:
+ if service.id == id:
+ return service
+ log.warning("There is no service with id %i" %id)
+ return None
def start(self):
for service in self.services:
- t = Thread(target=service.run, args=[])
- self.threads.append(t)
- t.start()
+ log.info("Starting service %s" %service.name)
+ service.start()
def add(self, service):
+ log.info("Added service %s" %service.name)
+ self.lastId += 1
+ service.setId(self.lastId)
self.services.append(service)
def kill(self, id):
- self.threads[id].interrupt_main()
- self.services.pop(id)
- self.threads.pop(id)
- def get_service(self, id):
- for service in self.services:
- if service.id == id:
- return service
- return None
+ service = self.get_service(id)
+ if service is None:
+ return
+ service.kill()
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments