|
| 1 | +from datetime import datetime |
| 2 | +from datetime import timedelta |
| 3 | +from datetime import timezone |
| 4 | + |
| 5 | +from celery import shared_task |
| 6 | +from dateutil import parser |
| 7 | +from django.core.cache import cache |
| 8 | + |
| 9 | +from adhocracy4.phases.models import Phase |
| 10 | +from meinberlin.apps import logger |
| 11 | + |
| 12 | + |
| 13 | +def get_next_projects_start() -> list: |
| 14 | + """ |
| 15 | + Helper function to query the db and retreive the |
| 16 | + phases for projects that will start in the next 10min. |
| 17 | + """ |
| 18 | + now = datetime.now(tz=timezone.utc) # tz is UTC |
| 19 | + phases = ( |
| 20 | + Phase.objects.filter( |
| 21 | + start_date__isnull=False, |
| 22 | + start_date__gt=now, |
| 23 | + start_date__lt=now + timedelta(minutes=10), |
| 24 | + ) |
| 25 | + .order_by("start_date") |
| 26 | + .all() |
| 27 | + ) |
| 28 | + list_format_phases = [] |
| 29 | + if phases: |
| 30 | + for phase in phases: |
| 31 | + str_phase = phase.start_date.astimezone(timezone.utc).strftime( |
| 32 | + "%Y-%m-%d, %H:%M:%S %Z" |
| 33 | + ) |
| 34 | + list_format_phases.append(str_phase) |
| 35 | + return list_format_phases |
| 36 | + |
| 37 | + |
| 38 | +def get_next_project_ends() -> str: |
| 39 | + """ |
| 40 | + Helper function to query the db and |
| 41 | + retreive the erliest phase that will end. |
| 42 | + """ |
| 43 | + now = datetime.now(tz=timezone.utc) # tz is UTC |
| 44 | + phase = ( |
| 45 | + Phase.objects.filter(end_date__isnull=False, end_date__gt=now) |
| 46 | + .order_by("end_date") |
| 47 | + .first() |
| 48 | + ) |
| 49 | + if not phase: |
| 50 | + return "" |
| 51 | + str_format_phase = phase.end_date.astimezone(timezone.utc).strftime( |
| 52 | + "%Y-%m-%d, %H:%M:%S %Z" |
| 53 | + ) |
| 54 | + return str_format_phase |
| 55 | + |
| 56 | + |
| 57 | +@shared_task(name="schedule_reset_cache_for_projects_becoming_active") |
| 58 | +def schedule_reset_cache_for_projects_becoming_active(): |
| 59 | + """The task is set via celery beat every 10 minutes in |
| 60 | + settings/production.txt. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + A string message with a list of the projects becoming active |
| 64 | + in the next 10 minutes and when the cache will be cleared. |
| 65 | + """ |
| 66 | + |
| 67 | + # check if redis has a next_project_starts key |
| 68 | + list_next_projects_start = cache.get("next_projects_start") |
| 69 | + if list_next_projects_start is None or list_next_projects_start == []: |
| 70 | + list_next_projects_start = get_next_projects_start() |
| 71 | + |
| 72 | + msg = "Future projects becoming active in next 10mins will be removed from cache: " |
| 73 | + |
| 74 | + if list_next_projects_start: # if not an empty list |
| 75 | + # set the redis key: value |
| 76 | + cache.set("next_projects_start", list_next_projects_start) |
| 77 | + |
| 78 | + for str_project_starts in list_next_projects_start: |
| 79 | + # convert to datetime obj |
| 80 | + date_project_starts = parser.parse(str_project_starts) |
| 81 | + |
| 82 | + # compare now with next start date |
| 83 | + now = datetime.now(tz=timezone.utc) |
| 84 | + remain_time = date_project_starts - now |
| 85 | + |
| 86 | + if remain_time <= timedelta(minutes=10): |
| 87 | + # schedule cache clear for the remaining time btwn now and next start |
| 88 | + reset_cache_for_projects.apply_async( |
| 89 | + [True, False], countdown=remain_time |
| 90 | + ) |
| 91 | + msg += f""" |
| 92 | + {date_project_starts} in {remain_time} minutes |
| 93 | + """ |
| 94 | + else: |
| 95 | + msg += "None" |
| 96 | + return logger.info(msg) |
| 97 | + |
| 98 | + |
| 99 | +@shared_task(name="schedule_reset_cache_for_projects_becoming_past") |
| 100 | +def schedule_reset_cache_for_projects_becoming_past(): |
| 101 | + """The task is set via celery beat every 10 minutes in |
| 102 | + settings/production.txt. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + A string message if the next project ends |
| 106 | + date will be removed from the cache. |
| 107 | + """ |
| 108 | + |
| 109 | + # check if redis has a next_project_ends key |
| 110 | + str_project_ends = cache.get("next_project_ends") |
| 111 | + |
| 112 | + if not str_project_ends: |
| 113 | + str_project_ends = get_next_project_ends() |
| 114 | + if not str_project_ends: |
| 115 | + raise Exception("At least one phase with dates is required") |
| 116 | + # set the redis key: value |
| 117 | + cache.set("next_project_ends", str_project_ends) |
| 118 | + |
| 119 | + msg = "Active projects will be removed from cache: " |
| 120 | + |
| 121 | + # compare now with next end date |
| 122 | + date_project_ends = parser.parse(str_project_ends) |
| 123 | + now = datetime.now(tz=timezone.utc) |
| 124 | + remain_time = now - date_project_ends |
| 125 | + if remain_time <= timedelta(minutes=10): |
| 126 | + # schedule cache clear for the remaining time btwn now and next end |
| 127 | + reset_cache_for_projects.apply_async([False, True], countdown=remain_time) |
| 128 | + msg = f""" |
| 129 | + {date_project_ends} in {remain_time} minutes |
| 130 | + """ |
| 131 | + else: |
| 132 | + msg += "None" |
| 133 | + return logger.info(msg) |
| 134 | + |
| 135 | + |
| 136 | +@shared_task |
| 137 | +def reset_cache_for_projects(starts, ends): |
| 138 | + msg = "Clear cache " |
| 139 | + if starts: |
| 140 | + # remove redis key next_project_start |
| 141 | + cache.delete("next_project_starts") |
| 142 | + cache.delete_many( |
| 143 | + [ |
| 144 | + "projects_activeParticipation", |
| 145 | + "projects_futureParticipation", |
| 146 | + "private_projects", |
| 147 | + "extprojects", |
| 148 | + ] |
| 149 | + ) |
| 150 | + if cache.get("projects_*") or cache.get("extprojects"): |
| 151 | + msg += "failed for future projects becoming active" |
| 152 | + else: |
| 153 | + msg += "succeeded for future projects becoming active" |
| 154 | + if ends: |
| 155 | + # remove redis key next_project_ends |
| 156 | + cache.delete("next_project_ends") |
| 157 | + cache.delete_many( |
| 158 | + [ |
| 159 | + "projects_activeParticipation", |
| 160 | + "projects_pastParticipation", |
| 161 | + "private_projects", |
| 162 | + "extprojects", |
| 163 | + ] |
| 164 | + ) |
| 165 | + if cache.get("projects_*") or cache.get("extprojects"): |
| 166 | + msg += "failed for active projects becoming past" |
| 167 | + else: |
| 168 | + msg += "succeeded for active projects becoming past" |
| 169 | + return logger.info(msg) |
0 commit comments