~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to notification/engine.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
# lock timeout value. how long to wait for the lock to become available.
23
23
# default behavior is to never wait for the lock to be available.
24
 
LOCK_WAIT_TIMEOUT = getattr(settings, "NOTIFICATION_LOCK_WAIT_TIMEOUT", -1)
 
24
LOCK_WAIT_TIMEOUT = getattr(settings, 'NOTIFICATION_LOCK_WAIT_TIMEOUT', -1)
 
25
 
25
26
 
26
27
def send_all():
27
 
    lock = FileLock("send_notices")
 
28
    lock = FileLock('send_notices')
28
29
 
29
 
    logging.debug("acquiring lock...")
 
30
    logging.debug('acquiring lock...')
30
31
    try:
31
32
        lock.acquire(LOCK_WAIT_TIMEOUT)
32
33
    except AlreadyLocked:
33
 
        logging.debug("lock already in place. quitting.")
 
34
        logging.debug('lock already in place. quitting.')
34
35
        return
35
36
    except LockTimeout:
36
 
        logging.debug("waiting for the lock timed out. quitting.")
 
37
        logging.debug('waiting for the lock timed out. quitting.')
37
38
        return
38
 
    logging.debug("acquired.")
 
39
    logging.debug('acquired.')
39
40
 
40
41
    batches, sent = 0, 0
41
42
    start_time = time.time()
44
45
        # nesting the try statement to be Python 2.4
45
46
        try:
46
47
            for queued_batch in NoticeQueueBatch.objects.all():
47
 
                notices = pickle.loads(str(queued_batch.pickled_data).decode("base64"))
 
48
                notices = pickle.loads(
 
49
                    str(queued_batch.pickled_data).decode('base64'))
48
50
                for user, label, extra_context, on_site in notices:
49
51
                    user = User.objects.get(pk=user)
50
 
                    logging.info("emitting notice to %s" % user)
 
52
                    logging.info('emitting notice to %s' % user)
51
53
                    # call this once per user to be atomic and allow for logging to
52
54
                    # accurately show how long each takes.
53
 
                    notification.send_now([user], label, extra_context, on_site)
 
55
                    notification.send_now(
 
56
                        [user], label, extra_context, on_site)
54
57
                    sent += 1
55
58
                queued_batch.delete()
56
59
                batches += 1
59
62
            exc_class, e, t = sys.exc_info()
60
63
            # email people
61
64
            current_site = Site.objects.get_current()
62
 
            subject = "[%s emit_notices] %r" % (current_site.name, e)
63
 
            message = "%s" % ("\n".join(traceback.format_exception(*sys.exc_info())),)
 
65
            subject = '[%s emit_notices] %r' % (current_site.name, e)
 
66
            message = '%s' % (
 
67
                '\n'.join(traceback.format_exception(*sys.exc_info())),)
64
68
            mail_admins(subject, message, fail_silently=True)
65
69
            # log it as critical
66
 
            logging.critical("an exception occurred: %r" % e)
 
70
            logging.critical('an exception occurred: %r' % e)
67
71
    finally:
68
 
        logging.debug("releasing lock...")
 
72
        logging.debug('releasing lock...')
69
73
        lock.release()
70
 
        logging.debug("released.")
71
 
    
72
 
    logging.info("")
73
 
    logging.info("%s batches, %s sent" % (batches, sent,))
74
 
    logging.info("done in %.2f seconds" % (time.time() - start_time))
 
74
        logging.debug('released.')
 
75
 
 
76
    logging.info('')
 
77
    logging.info('%s batches, %s sent' % (batches, sent,))
 
78
    logging.info('done in %.2f seconds' % (time.time() - start_time))