Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 48491a8

Browse files
author
Jeff Ammons
committed
Simplifies the Jobs interface
Standardizes onto using run() as the method for calling a Job and returning output. This lets us pass around fewer args. I also slipped in a change to make _dbg print to logging.DEBUG log level. Because, that just makes sense.
1 parent 8251988 commit 48491a8

File tree

1 file changed

+59
-22
lines changed

1 file changed

+59
-22
lines changed

rtmbot/core.py

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ def __init__(self, config):
4444
path = os.path.join(os.getcwd(), self.directory)
4545
self.directory = os.path.abspath(path)
4646

47+
self.debug = self.config.get('DEBUG', False)
4748
# establish logging
4849
log_file = config.get('LOGFILE', 'rtmbot.log')
50+
if self.debug:
51+
log_level = logging.DEBUG
52+
else:
53+
log_level = logging.INFO
4954
logging.basicConfig(filename=log_file,
50-
level=logging.INFO,
55+
level=log_level,
5156
format='%(asctime)s %(message)s')
5257
logging.info('Initialized in: {}'.format(self.directory))
53-
self.debug = self.config.get('DEBUG', False)
5458

5559
# initialize stateful fields
5660
self.last_ping = 0
@@ -59,7 +63,7 @@ def __init__(self, config):
5963

6064
def _dbg(self, debug_string):
6165
if self.debug:
62-
logging.info(debug_string)
66+
logging.debug(debug_string)
6367

6468
def connect(self):
6569
"""Convenience method that creates Server instance"""
@@ -218,7 +222,24 @@ def do(self, function_name, data):
218222

219223
def do_jobs(self):
220224
for job in self.jobs:
221-
job.check()
225+
if job.check():
226+
# interval is up, so run the job
227+
228+
if self.debug is True:
229+
# this makes the plugin fail with stack trace in debug mode
230+
job_output = job.run(self.slack_client)
231+
else:
232+
# otherwise we log the exception and carry on
233+
try:
234+
job_output = job.run(self.slack_client)
235+
except Exception:
236+
logging.exception("Problem in job run: {}".format(
237+
job.__class__)
238+
)
239+
240+
# job attempted execution so reset the timer and log output
241+
job.lastrun = time.time()
242+
self.outputs.append(job_output)
222243

223244
def do_output(self):
224245
output = []
@@ -232,34 +253,50 @@ def do_output(self):
232253

233254

234255
class Job(object):
235-
def __init__(self, interval, function, debug, plugin):
236-
self.function = function
256+
'''
257+
Jobs can be used to trigger periodic method calls. Jobs must be
258+
registered with a Plugin to be called. See the register_jobs method
259+
and documentation for how to make this work.
260+
261+
:Args:
262+
interval (int): The interval in seconds at which this Job's run
263+
method should be called
264+
'''
265+
def __init__(self, interval):
237266
self.interval = interval
238267
self.lastrun = 0
239-
self.debug = debug
240-
self.plugin = plugin
241268

242269
def __str__(self):
243-
return "{} {} {}".format(self.function, self.interval, self.lastrun)
270+
return "{} {} {}".format(self.__class__, self.interval, self.lastrun)
244271

245272
def __repr__(self):
246273
return self.__str__()
247274

248275
def check(self):
276+
''' Returns True if `interval` seconds have passed since it last ran '''
249277
if self.lastrun + self.interval < time.time():
250-
func = getattr(self, self.function)
251-
if self.debug is True:
252-
# this makes the plugin fail with stack trace in debug mode
253-
func()
254-
else:
255-
# otherwise we log the exception and carry on
256-
try:
257-
func()
258-
except Exception:
259-
logging.exception("Problem in job check: {}: {}".format(
260-
self.__class__, self.function)
261-
)
262-
self.lastrun = time.time()
278+
return True
279+
else:
280+
return False
281+
282+
def run(self, slack_client):
283+
''' This method is called from the plugin and is where the logic for
284+
your Job starts and finished. It is called every `interval` seconds
285+
from Job.check()
286+
287+
:Args:
288+
slackclient (Slackclient): An instance of the Slackclient API connector
289+
this can be used to make calls directly to the Slack Web API if
290+
necessary.
291+
292+
293+
This method should return an array of outputs in the form of::
294+
295+
[Channel Identifier, Output String]
296+
or
297+
['C12345678', 'Here's my output for this channel']
298+
'''
299+
raise NotImplementedError
263300

264301

265302
class UnknownChannel(Exception):

0 commit comments

Comments
 (0)