11#!/usr/bin/env python
22from __future__ import unicode_literals
33import sys
4- import glob
54import os
65import time
76import logging
@@ -68,9 +67,15 @@ def connect(self):
6867 def _start (self ):
6968 self .connect ()
7069 self .load_plugins ()
70+ for plugin in self .bot_plugins :
71+ try :
72+ self ._dbg ("Registering jobs for {}" .format (plugin .name ))
73+ plugin .register_jobs ()
74+ except :
75+ self ._dbg ("No jobs registered for {}" .format (plugin .name ))
7176 while True :
7277 for reply in self .slack_client .rtm_read ():
73- self .input (reply ) # <<<<------------------------
78+ self .input (reply )
7479 self .crons ()
7580 self .output ()
7681 self .autoping ()
@@ -96,7 +101,6 @@ def input(self, data):
96101 function_name = "process_" + data ["type" ]
97102 self ._dbg ("got {}" .format (function_name ))
98103 for plugin in self .bot_plugins :
99- plugin .register_jobs ()
100104 plugin .do (function_name , data )
101105
102106 def output (self ):
@@ -162,18 +166,14 @@ def __init__(self, name=None, slack_client=None, plugin_config=None):
162166 self .slack_client = slack_client
163167 self .jobs = []
164168 self .debug = self .plugin_config .get ('DEBUG' , False )
165- self .register_jobs ()
166169 self .outputs = []
167170
168171 def register_jobs (self ):
169- # if 'crontable' in dir(self.module):
170- # for interval, function in self.module.crontable:
171- # self.jobs.append(Job(interval, eval("self.module." + function), self.debug))
172- # logging.info(self.module.crontable)
173- # self.module.crontable = []
174- # else:
175- # self.module.crontable = []
176- pass
172+ ''' Please override this job with a method that instantiates any jobs
173+ you'd like to run from this plugin and attaches them to self.jobs. See
174+ the example plugins for examples.
175+ '''
176+ raise NotImplementedError
177177
178178 def do (self , function_name , data ):
179179 try :
@@ -193,11 +193,7 @@ def do(self, function_name, data):
193193 self .name , function_name , data )
194194 )
195195
196- try :
197- func = getattr (self , 'catch_all' )
198- except AttributeError :
199- return
200- else :
196+ if hasattr (self , 'catch_all' ):
201197 if self .debug is True :
202198 # this makes the plugin fail with stack trace in debug mode
203199 self .catch_all (data )
@@ -225,11 +221,12 @@ def do_output(self):
225221
226222
227223class Job (object ):
228- def __init__ (self , interval , function , debug ):
224+ def __init__ (self , interval , function , debug , plugin ):
229225 self .function = function
230226 self .interval = interval
231227 self .lastrun = 0
232228 self .debug = debug
229+ self .plugin = plugin
233230
234231 def __str__ (self ):
235232 return "{} {} {}" .format (self .function , self .interval , self .lastrun )
@@ -239,15 +236,18 @@ def __repr__(self):
239236
240237 def check (self ):
241238 if self .lastrun + self .interval < time .time ():
239+ func = getattr (self , self .function )
242240 if self .debug is True :
243241 # this makes the plugin fail with stack trace in debug mode
244- self . function ()
242+ func ()
245243 else :
246244 # otherwise we log the exception and carry on
247245 try :
248- self . function ()
246+ func ()
249247 except Exception :
250- logging .exception ("Problem in job check: {}" .format (self .function ))
248+ logging .exception ("Problem in job check: {}: {}" .format (
249+ self .__class__ , self .function )
250+ )
251251 self .lastrun = time .time ()
252252
253253
0 commit comments