queue an irc message when we sell a ticket
This commit is contained in:
parent
4d9663123a
commit
5c689e4076
|
@ -18,7 +18,6 @@ class Plugin(object):
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.log = self.bot.log
|
|
||||||
|
|
||||||
|
|
||||||
###############################################################################################
|
###############################################################################################
|
||||||
|
@ -69,7 +68,7 @@ class Plugin(object):
|
||||||
This method gets unprocessed OutgoingIrcMessage objects and attempts to send them to
|
This method gets unprocessed OutgoingIrcMessage objects and attempts to send them to
|
||||||
the target channel. Messages are skipped if the bot is not in the channel.
|
the target channel. Messages are skipped if the bot is not in the channel.
|
||||||
"""
|
"""
|
||||||
logger.debug("inside get_outgoing_messages()")
|
#logger.debug("inside get_outgoing_messages()")
|
||||||
for msg in OutgoingIrcMessage.objects.filter(processed=False).order_by('created'):
|
for msg in OutgoingIrcMessage.objects.filter(processed=False).order_by('created'):
|
||||||
# if this message expired mark it as expired and processed without doing anything
|
# if this message expired mark it as expired and processed without doing anything
|
||||||
if msg.timeout < timezone.now():
|
if msg.timeout < timezone.now():
|
||||||
|
@ -92,7 +91,7 @@ class Plugin(object):
|
||||||
else:
|
else:
|
||||||
logger.warning("skipping message to %s" % msg.target)
|
logger.warning("skipping message to %s" % msg.target)
|
||||||
|
|
||||||
# call this function again in 60 seconds
|
# call this function again in X seconds
|
||||||
self.bot.loop.call_later(settings.IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS, self.bot.get_outgoing_messages)
|
self.bot.loop.call_later(settings.IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS, self.bot.get_outgoing_messages)
|
||||||
|
|
||||||
|
|
||||||
|
|
29
src/ircbot/ircworker.py
Normal file
29
src/ircbot/ircworker.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from .models import OutgoingIrcMessage
|
||||||
|
from django.conf import settings
|
||||||
|
import logging, irc3
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger('bornhack.%s' % __name__)
|
||||||
|
|
||||||
|
|
||||||
|
def do_work():
|
||||||
|
"""
|
||||||
|
Run irc3 module code, wait for events on IRC and wait for messages in OutgoingIrcMessage
|
||||||
|
"""
|
||||||
|
config = {
|
||||||
|
'nick': settings.IRCBOT_NICK,
|
||||||
|
'autojoins': [settings.IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL],
|
||||||
|
'host': settings.IRCBOT_SERVER_HOSTNAME,
|
||||||
|
'port': settings.IRCBOT_SERVER_PORT,
|
||||||
|
'ssl': settings.IRCBOT_SERVER_USETLS,
|
||||||
|
'timeout': 30,
|
||||||
|
'includes': [
|
||||||
|
'ircbot.irc3module',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
logger.debug("Connecting to IRC with the following config: %s" % config)
|
||||||
|
try:
|
||||||
|
irc3.IrcBot(**config).run(forever=True)
|
||||||
|
except Exception as E:
|
||||||
|
logger.exception("Got exception inside do_work for %s" % self.workermodule)
|
||||||
|
raise
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
default_app_config = 'shop.apps.ShopConfig'
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from .signal_handlers import ticket_created
|
||||||
|
from django.db.models.signals import post_save
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
||||||
|
|
||||||
class ShopConfig(AppConfig):
|
class ShopConfig(AppConfig):
|
||||||
name = 'shop'
|
name = 'shop'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
post_save.connect(ticket_created, sender='shop.Ticket')
|
||||||
|
|
||||||
|
|
28
src/shop/signal_handlers.py
Normal file
28
src/shop/signal_handlers.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.conf import settings
|
||||||
|
from datetime import timedelta
|
||||||
|
from django.db.models import Count
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
||||||
|
|
||||||
|
|
||||||
|
def ticket_created(sender, instance, created, **kwargs):
|
||||||
|
# only send a message when a ticket is created
|
||||||
|
if not created:
|
||||||
|
return
|
||||||
|
|
||||||
|
# queue an IRC message to the orga channel if any is defined, otherwise for the default channel
|
||||||
|
target = settings.IRCBOT_CHANNELS['orga'] if 'orga' in settings.IRCBOT_CHANNELS else settings.IRCBOT_CHANNELS['default']
|
||||||
|
|
||||||
|
# get ticket stats, FIXME: Camp name is hardcoded here for now
|
||||||
|
from shop.models import Ticket
|
||||||
|
stats = ", ".join(["%s: %s" % (tickettype['product__name'], tickettype['total']) for tickettype in Ticket.objects.filter(product__name__startswith="BornHack 2017").values('product__name').annotate(total=Count('product__name')).order_by('product__name')])
|
||||||
|
|
||||||
|
# queue the message
|
||||||
|
from ircbot.models import OutgoingIrcMessage
|
||||||
|
OutgoingIrcMessage.objects.create(
|
||||||
|
target=target,
|
||||||
|
message="%s sold! Totals: %s" % (instance.product.name, stats),
|
||||||
|
timeout=timezone.now()+timedelta(minutes=10)
|
||||||
|
)
|
||||||
|
|
|
@ -53,8 +53,12 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
logger.info("Entering main loop...")
|
logger.info("Entering main loop...")
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
# run worker code
|
# run worker code
|
||||||
getattr(self.workermodule, 'do_work')()
|
getattr(self.workermodule, 'do_work')()
|
||||||
|
except Exception as E:
|
||||||
|
logger.exception("Got exception inside do_work for %s" % self.workermodule)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# sleep for N seconds before calling worker code again
|
# sleep for N seconds before calling worker code again
|
||||||
i = 0
|
i = 0
|
||||||
|
|
Loading…
Reference in a new issue