add shiny new daemonize mode
This commit is contained in:
parent
2b7145d0ed
commit
68d3784533
2 changed files with 42 additions and 10 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
python-daemon
|
||||||
pyyaml
|
pyyaml
|
||||||
websocket-client
|
websocket-client
|
||||||
git+https://github.com/slackhq/python-slackclient.git
|
git+https://github.com/slackhq/python-slackclient.git
|
||||||
|
|
51
rtmbot.py
51
rtmbot.py
|
@ -9,12 +9,13 @@ import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
from slackclient import SlackClient
|
from slackclient import SlackClient
|
||||||
|
|
||||||
def dbg(debug_string):
|
def dbg(debug_string):
|
||||||
if debug:
|
if debug:
|
||||||
print debug_string
|
logging.info(debug_string)
|
||||||
|
|
||||||
class RtmBot(object):
|
class RtmBot(object):
|
||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
|
@ -55,30 +56,34 @@ class RtmBot(object):
|
||||||
for plugin in self.bot_plugins:
|
for plugin in self.bot_plugins:
|
||||||
plugin.do_jobs()
|
plugin.do_jobs()
|
||||||
def load_plugins(self):
|
def load_plugins(self):
|
||||||
path = os.path.dirname(sys.argv[0])
|
for plugin in glob.glob(directory+'/plugins/*'):
|
||||||
for plugin in glob.glob(path+'/plugins/*'):
|
|
||||||
sys.path.insert(0, plugin)
|
sys.path.insert(0, plugin)
|
||||||
sys.path.insert(0, path+'/plugins/')
|
sys.path.insert(0, directory+'/plugins/')
|
||||||
for plugin in glob.glob(path+'/plugins/*.py') + glob.glob(path+'/plugins/*/*.py'):
|
for plugin in glob.glob(directory+'/plugins/*.py') + glob.glob(directory+'/plugins/*/*.py'):
|
||||||
print plugin
|
logging.info(plugin)
|
||||||
name = plugin.split('/')[-1][:-2]
|
name = plugin.split('/')[-1][:-3]
|
||||||
# try:
|
# try:
|
||||||
self.bot_plugins.append(Plugin(name))
|
self.bot_plugins.append(Plugin(name))
|
||||||
# except:
|
# except:
|
||||||
# print "error loading plugin %s" % name
|
# print "error loading plugin %s" % name
|
||||||
|
|
||||||
class Plugin(object):
|
class Plugin(object):
|
||||||
def __init__(self, name):
|
def __init__(self, name, plugin_config={}):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.jobs = []
|
self.jobs = []
|
||||||
self.module = __import__(name)
|
self.module = __import__(name)
|
||||||
self.register_jobs()
|
self.register_jobs()
|
||||||
self.outputs = []
|
self.outputs = []
|
||||||
|
if name in config:
|
||||||
|
logging.info("config found for: " + name)
|
||||||
|
self.module.config = config[name]
|
||||||
|
if 'setup' in dir(self.module):
|
||||||
|
self.module.setup()
|
||||||
def register_jobs(self):
|
def register_jobs(self):
|
||||||
if 'crontable' in dir(self.module):
|
if 'crontable' in dir(self.module):
|
||||||
for interval, function in self.module.crontable:
|
for interval, function in self.module.crontable:
|
||||||
self.jobs.append(Job(interval, eval("self.module."+function)))
|
self.jobs.append(Job(interval, eval("self.module."+function)))
|
||||||
print self.module.crontable
|
logging.info(self.module.crontable)
|
||||||
else:
|
else:
|
||||||
self.module.crontable = []
|
self.module.crontable = []
|
||||||
def do(self, function_name, data):
|
def do(self, function_name, data):
|
||||||
|
@ -104,6 +109,7 @@ class Plugin(object):
|
||||||
while True:
|
while True:
|
||||||
if 'outputs' in dir(self.module):
|
if 'outputs' in dir(self.module):
|
||||||
if len(self.module.outputs) > 0:
|
if len(self.module.outputs) > 0:
|
||||||
|
logging.info("output from {}".format(self.module))
|
||||||
output.append(self.module.outputs.pop(0))
|
output.append(self.module.outputs.pop(0))
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -136,9 +142,34 @@ class UnknownChannel(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main_loop():
|
||||||
|
logging.basicConfig(filename=config["LOGFILE"], level=logging.INFO, format='%(asctime)s %(message)s')
|
||||||
|
logging.info(directory)
|
||||||
|
try:
|
||||||
|
bot.start()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit(0)
|
||||||
|
except:
|
||||||
|
logging.exception('OOPS')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
directory = os.path.dirname(sys.argv[0])
|
||||||
|
if not directory.startswith('/'):
|
||||||
|
directory = os.path.abspath("{}/{}".format(os.getcwd(),
|
||||||
|
directory
|
||||||
|
))
|
||||||
|
|
||||||
config = yaml.load(file('rtmbot.conf', 'r'))
|
config = yaml.load(file('rtmbot.conf', 'r'))
|
||||||
debug = config["DEBUG"]
|
debug = config["DEBUG"]
|
||||||
bot = RtmBot(config["SLACK_TOKEN"])
|
bot = RtmBot(config["SLACK_TOKEN"])
|
||||||
bot.start()
|
site_plugins = []
|
||||||
|
files_currently_downloading = []
|
||||||
|
job_hash = {}
|
||||||
|
|
||||||
|
if config.has_key("DAEMON"):
|
||||||
|
if config["DAEMON"]:
|
||||||
|
import daemon
|
||||||
|
with daemon.DaemonContext():
|
||||||
|
main_loop()
|
||||||
|
main_loop()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue