try to get the singleton class done for importing
fixed mail task
This commit is contained in:
parent
3ef1dcecd7
commit
8f9d9bc551
4 changed files with 95 additions and 19 deletions
|
|
@ -67,10 +67,10 @@ class MailTask(Task):
|
|||
now = datetime.datetime.now()
|
||||
message['Date'] = now.strftime("%a, %d %b %Y %H:%M:%S")
|
||||
message['From'] = self.sender
|
||||
message['To'] = self.rcpt["rcpt"]
|
||||
message['To'] = self.rcpt
|
||||
s = smtplib.SMTP(self.host, self.port)
|
||||
#s.login(self.userName, self.password)
|
||||
s.sendmail(self.sender, self.rcpt["rcpt"], message.as_string())
|
||||
s.sendmail(self.sender, self.rcpt, message.as_string())
|
||||
s.quit()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -66,39 +66,54 @@ class Task(object):
|
|||
raise e
|
||||
|
||||
|
||||
"""
|
||||
class TaskList(object):
|
||||
def __init__(self, tasks):
|
||||
self.tasks = tasks
|
||||
|
||||
class TaskExecutor(object):
|
||||
_instance = None
|
||||
def __new__(cls, *args, **kwargs):
|
||||
if not cls._instance:
|
||||
cls._instance = super(TaskExecutor, cls).__init__()
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
self.event = Event()
|
||||
self.taskInfos = []
|
||||
task_thread = Thread(target=self._run_worker_thread)
|
||||
task_thread.setDaemon(True)
|
||||
self._instantEnd = False
|
||||
self._running = True
|
||||
task_thread.start()
|
||||
|
||||
|
||||
def _run_worker_thread(self):
|
||||
while True:
|
||||
while self.is_running() or not self.instand_end():
|
||||
if len(self.taskInfos) == 0:
|
||||
self.event.clear()
|
||||
self.event.wait()
|
||||
|
||||
try:
|
||||
msg, taskInfos = self.taskInfos[0]
|
||||
del self.taskInfos[0]
|
||||
try:
|
||||
for taskInfo in taskInfos:
|
||||
task = self.find_task_by_name(taskInfo["class"])
|
||||
if task:
|
||||
logger.debug("Starting Task Execution...")
|
||||
task.execute(msg, taskInfo["args"])
|
||||
except:
|
||||
logger.debug("Something failed!")
|
||||
pass
|
||||
except Exception, e:
|
||||
logger.error("Error " + str(e))
|
||||
|
||||
def find_task_by_name(self, clazzName):
|
||||
for task in self.tasks:
|
||||
if task.get_task_type() == clazzName:
|
||||
return task
|
||||
def is_instand_end(self):
|
||||
return self._instantEnd
|
||||
|
||||
def execute_task_infos(self, msg, taskInfos):
|
||||
self.taskInfos.append((msg, taskInfos))
|
||||
def is_running(self):
|
||||
return self._running
|
||||
|
||||
def stop(self):
|
||||
self._running = False
|
||||
|
||||
def stop_immediately(self):
|
||||
self.stop()
|
||||
self._instantEnd = True
|
||||
|
||||
def schedule_task(self, msg, task):
|
||||
self.taskInfos.append((msg, task))
|
||||
self.event.set()
|
||||
"""
|
||||
0
linspector/utils/__init__.py
Normal file
0
linspector/utils/__init__.py
Normal file
61
linspector/utils/singleton.py
Normal file
61
linspector/utils/singleton.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
"""
|
||||
The Singleton classes.
|
||||
|
||||
Copyright (c) 2011-2013 by Johannes Findeisen and Rafael Timmerberg
|
||||
|
||||
This file is part of Linspector (http://linspector.org).
|
||||
|
||||
Linspector is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
#see http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
||||
|
||||
class Singleton:
|
||||
"""
|
||||
A non-thread-safe helper class to ease implementing singletons.
|
||||
This should be used as a decorator -- not a metaclass -- to the
|
||||
class that should be a singleton.
|
||||
|
||||
The decorated class can define one `__init__` function that
|
||||
takes only the `self` argument. Other than that, there are
|
||||
no restrictions that apply to the decorated class.
|
||||
|
||||
To get the singleton instance, use the `Instance` method. Trying
|
||||
to use `__call__` will result in a `TypeError` being raised.
|
||||
|
||||
Limitations: The decorated class cannot be inherited from.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, decorated):
|
||||
self._decorated = decorated
|
||||
|
||||
def Instance(self):
|
||||
"""
|
||||
Returns the singleton instance. Upon its first call, it creates a
|
||||
new instance of the decorated class and calls its `__init__` method.
|
||||
On all subsequent calls, the already created instance is returned.
|
||||
|
||||
"""
|
||||
try:
|
||||
return self._instance
|
||||
except AttributeError:
|
||||
self._instance = self._decorated()
|
||||
return self._instance
|
||||
|
||||
def __call__(self):
|
||||
raise TypeError('Singletons must be accessed through `Instance()`.')
|
||||
|
||||
def __instancecheck__(self, inst):
|
||||
return isinstance(inst, self._decorated)
|
||||
Loading…
Add table
Add a link
Reference in a new issue