|
1 | 1 | import re
|
2 |
| -import imp |
3 | 2 | import time
|
4 | 3 | import datetime
|
| 4 | +import importlib |
5 | 5 | import threading
|
6 |
| -from typing import Any, cast, Dict, List, Tuple, Callable, Optional, Sequence |
| 6 | +from typing import Any, Dict, List, Tuple, Callable, Optional, Sequence |
7 | 7 |
|
8 | 8 | from typing_extensions import TypedDict
|
9 | 9 |
|
10 | 10 | from django.apps import apps
|
11 | 11 | from django.core.management import call_command
|
| 12 | +from django.utils.module_loading import module_has_submodule |
12 | 13 |
|
13 | 14 | from .task import task
|
14 | 15 | from .types import QueueName
|
@@ -132,24 +133,19 @@ def get_matcher(minval, maxval, t):
|
132 | 133 | return lambda x: x in t_parts
|
133 | 134 |
|
134 | 135 | for app_config in apps.get_app_configs():
|
135 |
| - app = app_config.name |
| 136 | + # Adapted from django.utils.module_loading.autodiscover_modules |
136 | 137 | try:
|
137 |
| - # __import__ will break with anything other than a str object(!), |
138 |
| - # including e.g. unicode. So force to a str. |
139 |
| - part = str(app.split('.')[-1]) |
140 |
| - |
141 |
| - app_path = __import__(app, {}, {}, [part]).__path__ |
142 |
| - except AttributeError: |
143 |
| - continue |
144 |
| - |
145 |
| - try: |
146 |
| - imp.find_module('cron', app_path) |
147 |
| - except ImportError: |
148 |
| - continue |
149 |
| - |
150 |
| - mod = __import__('{}.cron'.format(app), fromlist=(app,)) |
| 138 | + mod = importlib.import_module(f'{app_config.name}.cron') |
| 139 | + except Exception: |
| 140 | + if module_has_submodule(app_config.module, 'cron'): |
| 141 | + # The module exists, so the error was something it did -- bubble the error. |
| 142 | + raise |
| 143 | + else: |
| 144 | + # No module, move on. |
| 145 | + continue |
151 | 146 |
|
152 |
| - for row in cast(List[CronConfig], mod.CONFIG): |
| 147 | + app_cron_config: List[CronConfig] = mod.CONFIG # type: ignore[attr-defined] |
| 148 | + for row in app_cron_config: |
153 | 149 | row['min_matcher'] = get_matcher(0, 59, row.get('minutes'))
|
154 | 150 | row['hour_matcher'] = get_matcher(0, 23, row.get('hours'))
|
155 | 151 | row['day_matcher'] = get_matcher(1, 7, row.get('days', '*'))
|
|
0 commit comments