404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
1 |
import sys |
2 |
import time |
|
3 |
import logging |
|
4 |
import traceback |
|
5 |
import base64 |
|
532.1.31
by kaputtnik
use base64 instead of codecs |
6 |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
7 |
try: |
8 |
import pickle as pickle |
|
532.1.1
by franku
converted to python 3.6 using 2to3 script |
9 |
except ImportError: |
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
10 |
import pickle |
11 |
||
12 |
from django.conf import settings |
|
13 |
from django.core.mail import mail_admins |
|
14 |
from django.contrib.auth.models import User |
|
15 |
from django.contrib.sites.models import Site |
|
16 |
||
17 |
from .lockfile import FileLock, AlreadyLocked, LockTimeout |
|
532.1.1
by franku
converted to python 3.6 using 2to3 script |
18 |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
19 |
from notification.models import NoticeQueueBatch |
20 |
from notification import models as notification |
|
21 |
||
22 |
# lock timeout value. how long to wait for the lock to become available.
|
|
23 |
# default behavior is to never wait for the lock to be available.
|
|
24 |
LOCK_WAIT_TIMEOUT = getattr(settings, 'NOTIFICATION_LOCK_WAIT_TIMEOUT', -1) |
|
438.1.6
by franku
run the script |
25 |
|
26 |
||
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
27 |
def send_all(): |
28 |
lock = FileLock('send_notices') |
|
438.1.6
by franku
run the script |
29 |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
30 |
logging.debug('acquiring lock...') |
438.1.6
by franku
run the script |
31 |
try: |
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
32 |
lock.acquire(LOCK_WAIT_TIMEOUT) |
33 |
except AlreadyLocked: |
|
34 |
logging.debug('lock already in place. quitting.') |
|
438.1.6
by franku
run the script |
35 |
return
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
36 |
except LockTimeout: |
37 |
logging.debug('waiting for the lock timed out. quitting.') |
|
438.1.6
by franku
run the script |
38 |
return
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
39 |
logging.debug('acquired.') |
438.1.6
by franku
run the script |
40 |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
41 |
batches, sent = 0, 0 |
42 |
start_time = time.time() |
|
43 |
||
44 |
try: |
|
45 |
# nesting the try statement to be Python 2.4
|
|
46 |
try: |
|
47 |
for queued_batch in NoticeQueueBatch.objects.all(): |
|
48 |
notices = pickle.loads( |
|
438.1.6
by franku
run the script |
49 |
base64.b64decode(queued_batch.pickled_data) |
532.1.31
by kaputtnik
use base64 instead of codecs |
50 |
)
|
51 |
for user, label, extra_context, on_site in notices: |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
52 |
user = User.objects.get(pk=user) |
53 |
# FrankU: commented, because not all users get e-mailed
|
|
450.1.3
by franku
removed unused files; queued e-mails |
54 |
# and to supress useless logging
|
450.1.7
by franku
distuinguish my comment to original comment |
55 |
# logging.info('emitting notice to %s' % user)
|
450.1.3
by franku
removed unused files; queued e-mails |
56 |
|
450.1.18
by franku
clean spaces from empty lines |
57 |
# call this once per user to be atomic and allow for logging to
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
58 |
# accurately show how long each takes.
|
59 |
notification.send_now( |
|
438.1.6
by franku
run the script |
60 |
[user], label, extra_context, on_site) |
61 |
sent += 1 |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
62 |
queued_batch.delete() |
63 |
batches += 1 |
|
64 |
except: |
|
65 |
# get the exception
|
|
66 |
exc_class, e, t = sys.exc_info() |
|
67 |
# email people
|
|
68 |
current_site = Site.objects.get_current() |
|
69 |
subject = '[%s emit_notices] %r' % (current_site.name, e) |
|
438.1.6
by franku
run the script |
70 |
message = '%s' % ( |
71 |
'\n'.join(traceback.format_exception(*sys.exc_info())),) |
|
72 |
mail_admins(subject, message, fail_silently=True) |
|
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
73 |
# log it as critical
|
74 |
logging.critical('an exception occurred: %r' % e) |
|
438.1.6
by franku
run the script |
75 |
finally: |
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
76 |
logging.debug('releasing lock...') |
438.1.6
by franku
run the script |
77 |
lock.release() |
404.2.25
by franku
added old notification app to widelands; deactivated notification feed |
78 |
logging.debug('released.') |
438.1.6
by franku
run the script |
79 |
|
80 |
logging.info('') |
|
81 |
logging.info('%s batches, %s sent' % (batches, sent,)) |
|
82 |
logging.info('done in %.2f seconds' % (time.time() - start_time)) |
|
83 |