Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pikatasks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
django
pikatasks
Commits
92258d23
Commit
92258d23
authored
7 years ago
by
Art
Browse files
Options
Downloads
Patches
Plain Diff
Add autodiscover_tasks to avoid kilometers of manual imports
parent
8c736918
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pikatasks/django_compat.py
+25
-0
25 additions, 0 deletions
pikatasks/django_compat.py
pikatasks/worker.py
+1
-1
1 addition, 1 deletion
pikatasks/worker.py
with
26 additions
and
1 deletion
pikatasks/django_compat.py
+
25
−
0
View file @
92258d23
import
logging
import
importlib
from
.
import
utils
try
:
from
django
import
db
as
django_db
...
...
@@ -61,3 +63,26 @@ def check_fix_db_connection():
pass
def
autodiscover_tasks
(
apps
=
None
,
modules
=
(
"
tasks
"
,)):
"""
Imports modules with tasks from django apps.
This function utilizes the fact that each task registers itself in utils.all_tasks
:param apps: tuple of app names, leave None for everything in INSTALLED_APPS
:param modules: tuple of module names, if apps have their tasks in places other than
"
tasks.py
"
:return: utils.all_tasks
"""
assert
DJANGO
if
apps
is
None
:
apps
=
django_conf
.
settings
.
INSTALLED_APPS
for
app_name
in
apps
:
for
module_name
in
modules
:
full_module_name
=
"
{0}.{1}
"
.
format
(
app_name
,
module_name
)
try
:
importlib
.
import_module
(
full_module_name
)
# just importing the module is perfectly enough, each task will register itself on import
logger
.
info
(
"
Autodiscover: imported
\"
{0}
\"
"
.
format
(
full_module_name
))
except
ImportError
:
logger
.
debug
(
"
Autodiscover: module
\"
{0}
\"
does not exist
"
.
format
(
full_module_name
))
return
utils
.
all_tasks
This diff is collapsed.
Click to expand it.
pikatasks/worker.py
+
1
−
1
View file @
92258d23
...
...
@@ -93,7 +93,7 @@ def start(tasks=utils.all_tasks, number_of_processes=None):
channel
.
queue_declare
(
queue
=
queue_name
,
passive
=
True
)
exists
=
True
except
AMQPChannelError
as
e
:
logger
.
warning
(
"
Failed to
{queue_name}. {e.__class__.__name__}: {e}
"
.
format
(
**
locals
()))
logger
.
warning
(
"
Cannot access queue
\"
{queue_name}
\"
. {e.__class__.__name__}: {e}
"
.
format
(
**
locals
()))
exists
=
False
finally
:
if
conn
and
conn
.
is_open
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment