Skip to content

Commit

Permalink
Use standard calls in a separate loop instead of using scheduler that…
Browse files Browse the repository at this point in the history
… has db locks problems because of concurrency
  • Loading branch information
iranzo committed Aug 5, 2017
1 parent 9c33896 commit aa8e067
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 59 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
apscheduler
prettytable
python-dateutil
requests
Expand Down
27 changes: 13 additions & 14 deletions stampy/plugin/comic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

import datetime
import logging
import random
from urlparse import urlparse

import dateutil.parser
import feedparser
import requests
from apscheduler.schedulers.background import BackgroundScheduler
from lxml import html
from prettytable import from_db_cursor

Expand All @@ -24,33 +22,34 @@
from stampy.i18n import _
from stampy.i18n import _L

sched = BackgroundScheduler()
sched.start()


def init():
"""
Initializes module
:return: List of triggers for plugin
"""
botname = stampy.stampy.getme()
if botname == 'redken_bot':
delay = int(random.randint(0, 10))
when = 30 + delay
sched.add_job(comics, 'interval', id='comic', minutes=when,
replace_existing=True, misfire_grace_time=120,
coalesce=True)

triggers = ["^/comic"]

if botname == 'redken_bot':
triggers.append("^#cron")

for comic in getcomics():
triggers.extend(["/%s" % comic])

# Refresh comics in case bot was down:
comics()

return triggers


def cron():
"""
Function to be executed periodically
:return:
"""

comics()


def run(message): # do not edit this line
"""
Executes plugin
Expand Down
22 changes: 11 additions & 11 deletions stampy/plugin/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import dateutil.parser
import feedparser
import pytz
from apscheduler.schedulers.background import BackgroundScheduler
from prettytable import from_db_cursor

import stampy.plugin.alias
Expand All @@ -22,30 +21,31 @@
from stampy.i18n import _
from stampy.i18n import _L

sched = BackgroundScheduler()
sched.start()


def init():
"""
Initializes module
:return: List of triggers for plugin
"""
botname = stampy.stampy.getme()
if botname == 'redken_bot':
when = 5
sched.add_job(feeds, 'interval', id='feeds', minutes=when,
replace_existing=True, misfire_grace_time=120,
coalesce=True)

triggers = ["^/feed"]

# Refresh feeds in case bot was down:
feeds()
if botname == 'redken_bot':
triggers.append("^#cron")

return triggers


def cron():
"""
Function to be executed periodically
:return:
"""

feeds()


def run(message): # do not edit this line
"""
Executes plugin
Expand Down
20 changes: 11 additions & 9 deletions stampy/plugin/karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

import datetime
import logging
import random

from apscheduler.schedulers.background import BackgroundScheduler
from prettytable import from_db_cursor

import stampy.plugin.alias
Expand All @@ -17,22 +15,26 @@
from stampy.i18n import _
from stampy.i18n import _L

sched = BackgroundScheduler()
sched.start()


def init():
"""
Initializes module
:return: List of triggers for plugin
"""
delay = int(random.randint(0, 10))
when = int(stampy.plugin.config.config('cleanup', 24 * 60)) + delay
sched.add_job(dokarmacleanup, 'interval', minutes=when, id='dokarmacleanup', replace_existing=True, misfire_grace_time=120)
triggers = ["++", "--", u"—", "@", "^rank", "^srank", "^skarma", "=="]

triggers = ["++", "--", u"—", "@", "^rank", "^srank", "^skarma", "==", "^#cron"]
return triggers


def cron():
"""
Function to be executed periodically
:return:
"""

dokarmacleanup()


def run(message): # do not edit this line
"""
Executes plugin
Expand Down
22 changes: 12 additions & 10 deletions stampy/plugin/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import datetime
import json
import logging
import random
import urllib

from apscheduler.schedulers.background import BackgroundScheduler
from prettytable import from_db_cursor

import stampy.plugin.config
Expand All @@ -19,23 +17,27 @@
from stampy.i18n import _
from stampy.i18n import _L

sched = BackgroundScheduler()
sched.start()


def init():
"""
Initializes module
:return: List of triggers for plugin
"""
delay = int(random.randint(0, 10))
when = int(stampy.plugin.config.config('cleanup', 24 * 60)) + delay
sched.add_job(dochatcleanup, 'interval', minutes=when + delay, id='dochatcleanup', replace_existing=True, misfire_grace_time=120)
sched.add_job(dousercleanup, 'interval', minutes=when - delay, id='dousercleanup', replace_existing=True, misfire_grace_time=120)
triggers = ["@all", "^/stats", "*", "^/getout"]

triggers = ["@all", "^/stats", "*", "^/getout", "^#cron"]
return triggers


def cron():
"""
Function to be executed periodically
:return:
"""

dochatcleanup()
dousercleanup()


def run(message): # do not edit this line
"""
Executes plugin
Expand Down
43 changes: 29 additions & 14 deletions stampy/stampy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from time import sleep

import requests
from apscheduler.schedulers.background import BackgroundScheduler

import plugin.config
import plugin.forward
Expand Down Expand Up @@ -70,11 +69,6 @@
options, unknown = p.parse_known_args()


# Set scheduler
scheduler = BackgroundScheduler()
scheduler.start()


# Implement switch from http://code.activestate.com/recipes/410692/
class Switch(object):
"""
Expand Down Expand Up @@ -642,11 +636,11 @@ def process(messages):
if "*" in trigger:
runplugin = True
break
elif trigger[0] == "^":
elif trigger[0] == "^" and trigger != '^#cron':
if command == trigger[1:]:
runplugin = True
break
elif trigger in texto:
elif trigger in texto and trigger != '^#cron':
runplugin = True
break

Expand Down Expand Up @@ -676,6 +670,31 @@ def process(messages):
clearupdates(offset=lastupdateid + 1)


def processcron():
"""
This function processes plugins with cron features
"""

logger = logging.getLogger(__name__)

# Call plugins to process message
global plugs
global plugtriggers

for i in plugs:
name = i.__name__.split(".")[-1]

runplugin = False
for trigger in plugtriggers[name]:
if "^#cron" in trigger:
runplugin = True
break

if runplugin:
logger.debug(msg=_L("Processing plugin cron: %s") % name)
i.cron()


def getitems(var):
"""
Returns list of items even if provided args are lists of lists
Expand Down Expand Up @@ -840,9 +859,6 @@ def conflogging(target=None):
else:
plugin.config.setconfig(key="verbosity", value=options.verbosity)


()

# create formatter
formatter = logging.Formatter('%(asctime)s : %(name)s : %(funcName)s(%(lineno)d) : %(levelname)s : %(message)s')

Expand Down Expand Up @@ -879,9 +895,6 @@ def main():
# Configure logging
conflogging(target="stampy")

# Configuring apscheduler logger
conflogging(target="apscheduler")

# Configuring alembic logger
conflogging(target="alembic")

Expand Down Expand Up @@ -926,10 +939,12 @@ def main():
logger.info(msg=_L("Running in daemon mode"))
while plugin.config.config(key='daemon') == 'True':
process(getupdates())
processcron()
sleep(int(plugin.config.config(key='sleep')))
else:
logger.info(msg=_L("Running in one-shoot mode"))
process(getupdates())
processcron()

logger.info(msg=_L("Stopped execution"))
logging.shutdown()
Expand Down

0 comments on commit aa8e067

Please sign in to comment.