2017-11-16 20:54:59 +01:00
|
|
|
import logging
|
2018-12-16 19:47:39 +01:00
|
|
|
import time
|
2017-11-16 20:54:59 +01:00
|
|
|
|
2017-11-19 22:51:51 +01:00
|
|
|
from celery.exceptions import MaxRetriesExceededError
|
2024-12-28 23:18:45 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-12-16 19:47:39 +01:00
|
|
|
from django.core.cache import cache
|
2017-11-10 16:56:50 +01:00
|
|
|
from django.utils.formats import date_format
|
2022-04-03 16:33:43 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.utils.translation import ngettext_lazy
|
2017-11-10 16:56:50 +01:00
|
|
|
|
|
|
|
from c3nav.celery import app
|
|
|
|
|
2017-11-16 20:54:59 +01:00
|
|
|
logger = logging.getLogger('c3nav')
|
|
|
|
|
2017-11-10 16:56:50 +01:00
|
|
|
|
2017-11-10 23:23:22 +01:00
|
|
|
@app.task(bind=True, max_retries=10)
|
|
|
|
def process_map_updates(self):
|
2017-11-16 20:54:59 +01:00
|
|
|
if self.request.called_directly:
|
|
|
|
logger.info('Processing map updates by direct command...')
|
|
|
|
else:
|
|
|
|
logger.info('Processing map updates...')
|
|
|
|
|
2017-11-10 16:56:50 +01:00
|
|
|
from c3nav.mapdata.models import MapUpdate
|
2017-11-10 23:23:22 +01:00
|
|
|
try:
|
2017-11-19 22:51:51 +01:00
|
|
|
try:
|
|
|
|
updates = MapUpdate.process_updates()
|
2017-12-23 15:51:26 +01:00
|
|
|
except MapUpdate.ProcessUpdatesAlreadyRunning:
|
2017-11-19 22:51:51 +01:00
|
|
|
if self.request.called_directly:
|
|
|
|
raise
|
|
|
|
logger.info('Processing is already running, retrying in 30 seconds.')
|
|
|
|
raise self.retry(countdown=30)
|
2018-12-16 19:47:39 +01:00
|
|
|
except Exception:
|
2018-12-21 05:36:32 +01:00
|
|
|
cache.set('mapdata:last_process_updates_run', (int(time.time()), False), None)
|
2018-12-16 19:47:39 +01:00
|
|
|
raise
|
|
|
|
else:
|
2018-12-21 05:36:32 +01:00
|
|
|
cache.set('mapdata:last_process_updates_run', (int(time.time()), True), None)
|
2017-11-19 22:51:51 +01:00
|
|
|
except MaxRetriesExceededError:
|
|
|
|
logger.info('Cannot retry, retries exceeded. Exiting.')
|
|
|
|
return
|
2017-11-10 16:56:50 +01:00
|
|
|
|
|
|
|
if updates:
|
|
|
|
print()
|
|
|
|
|
2022-04-03 16:33:43 +02:00
|
|
|
logger.info(ngettext_lazy('%d map update processed.', '%d map updates processed.', len(updates)) % len(updates))
|
2017-11-10 16:56:50 +01:00
|
|
|
|
|
|
|
if updates:
|
2017-11-16 20:54:59 +01:00
|
|
|
logger.info(_('Last processed update: %(date)s (#%(id)d)') % {
|
2017-11-10 16:56:50 +01:00
|
|
|
'date': date_format(updates[-1].datetime, 'DATETIME_FORMAT'),
|
|
|
|
'id': updates[-1].pk,
|
|
|
|
})
|
2024-12-28 00:45:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.task(bind=True, max_retries=10)
|
|
|
|
def delete_map_cache_key(self, cache_key):
|
2024-12-28 15:35:15 +01:00
|
|
|
if hasattr(cache, 'keys'):
|
|
|
|
for key in cache.keys(f'*{cache_key}*'):
|
|
|
|
cache.delete(key)
|
2024-12-28 22:16:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.task(bind=True, max_retries=10)
|
2024-12-28 23:18:45 +01:00
|
|
|
def update_ap_names_bssid_mapping(self, map_name, user_id):
|
2024-12-29 00:40:10 +01:00
|
|
|
user = get_user_model().objects.filter(pk=user_id).first()
|
|
|
|
if user is None:
|
|
|
|
return
|
2024-12-28 22:16:25 +01:00
|
|
|
from c3nav.mapdata.models.geometry.space import RangingBeacon
|
|
|
|
todo = []
|
|
|
|
for beacon in RangingBeacon.objects.filter(ap_name__in=map_name.keys(),
|
|
|
|
beacon_type=RangingBeacon.BeaconType.EVENT_WIFI):
|
|
|
|
print(beacon, "add ssids", set(map_name[beacon.ap_name]))
|
|
|
|
if set(map_name[beacon.ap_name]) - set(beacon.addresses):
|
|
|
|
todo.append((beacon, list(set(beacon.addresses) | set(map_name[beacon.ap_name]))))
|
|
|
|
|
|
|
|
if todo:
|
|
|
|
from c3nav.editor.models import ChangeSet
|
|
|
|
from c3nav.editor.views.base import within_changeset
|
|
|
|
changeset = ChangeSet()
|
|
|
|
changeset.author = user
|
|
|
|
with within_changeset(changeset=changeset, user=user) as locked_changeset:
|
|
|
|
for beacon, addresses in todo:
|
|
|
|
beacon.addresses = addresses
|
|
|
|
beacon.save()
|
|
|
|
with changeset.lock_to_edit() as locked_changeset:
|
|
|
|
locked_changeset.title = 'passive update bssids'
|
|
|
|
locked_changeset.apply(user)
|