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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
django
pikatasks
Commits
72ae0ef4
Commit
72ae0ef4
authored
7 years ago
by
Art
Browse files
Options
Downloads
Patches
Plain Diff
Now @task can be called as if they were regular functions.
parent
c85511ac
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pikatasks/__init__.py
+15
-7
15 additions, 7 deletions
pikatasks/__init__.py
pikatasks/settings.py
+5
-2
5 additions, 2 deletions
pikatasks/settings.py
pikatasks/worker.py
+3
-2
3 additions, 2 deletions
pikatasks/worker.py
with
23 additions
and
11 deletions
pikatasks/__init__.py
+
15
−
7
View file @
72ae0ef4
...
@@ -14,6 +14,9 @@ class RPCTimedOut(RPCError):
...
@@ -14,6 +14,9 @@ class RPCTimedOut(RPCError):
pass
pass
all_tasks
=
set
()
# each registered task will show up here
def
run
(
name
,
**
kwargs
):
def
run
(
name
,
**
kwargs
):
"""
"""
Runs a task remotely.
Runs a task remotely.
...
@@ -89,14 +92,17 @@ def rpc(name, **kwargs):
...
@@ -89,14 +92,17 @@ def rpc(name, **kwargs):
def
task
(
name
):
def
task
(
name
):
"""
"""
Use this to decorate your tasks.
Use this to decorate your tasks.
:param name: name of the task and its queue
It doesn
'
t replace the function with a wrapper. Instead, it adds additional properties to the function.
:return: callable object, ready to be consumed by pika
'
s functions like Channel.basic_consume
Property .as_callback is a callable ready to be consumed by pika
'
s functions like Channel.basic_consume
:param name: name of the task == name of the queue
"""
"""
assert
isinstance
(
name
,
str
)
assert
isinstance
(
name
,
str
)
def
decorator
(
func
):
def
decorator
(
func
):
"""
Creates an actual decorator.
"""
def
wrapper
(
channel
,
method
,
properties
,
body
):
def
as_callback
(
channel
,
method
,
properties
,
body
):
"""
Creates a callback to be used by pika.
"""
nonlocal
name
,
func
nonlocal
name
,
func
func_result
,
func_error
=
None
,
None
func_result
,
func_error
=
None
,
None
channel
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
channel
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
...
@@ -124,10 +130,12 @@ def task(name):
...
@@ -124,10 +130,12 @@ def task(name):
if
func_result
:
if
func_result
:
logger
.
warning
(
"
Task {name} returned a result but the client doesn
'
t want to receive it.
"
.
format
(
name
=
name
))
logger
.
warning
(
"
Task {name} returned a result but the client doesn
'
t want to receive it.
"
.
format
(
name
=
name
))
wrapper
.
is_pikatasks_task
=
True
func
.
as_callback
=
as_callback
wrapper
.
task_name
=
name
func
.
task_name
=
name
wrapper
.
task_queue
=
name
func
.
task_queue
=
name
return
wrapper
global
all_tasks
all_tasks
.
add
(
func
)
return
func
return
decorator
return
decorator
This diff is collapsed.
Click to expand it.
pikatasks/settings.py
+
5
−
2
View file @
72ae0ef4
...
@@ -33,13 +33,16 @@ WORKER_CHECK_SUBPROCESSES_PERIOD = timedelta(seconds=2)
...
@@ -33,13 +33,16 @@ WORKER_CHECK_SUBPROCESSES_PERIOD = timedelta(seconds=2)
# merge these settings with django.conf.settings
# merge these settings with django.conf.settings
try
:
try
:
from
django.conf
import
settings
as
django_settings
from
django.conf
import
settings
as
django_settings
from
django.core.exceptions
import
ImproperlyConfigured
for
k
in
list
(
globals
().
keys
()):
# list() prevents errors on changes
for
k
in
list
(
globals
().
keys
()):
# list() prevents errors on changes
if
k
.
isupper
()
and
not
k
.
startswith
(
"
_
"
):
# looks like a setting
if
k
.
isupper
()
and
not
k
.
startswith
(
"
_
"
):
# looks like a setting
try
:
try
:
new_value
=
getattr
(
django_settings
,
"
PIKATASKS_
"
+
k
)
new_value
=
getattr
(
django_settings
,
"
PIKATASKS_
"
+
k
)
globals
()[
k
]
=
new_value
globals
()[
k
]
=
new_value
except
ImproperlyConfigured
:
pass
# django is installed but not used
except
AttributeError
:
except
AttributeError
:
pass
pass
# django is installed and used, but the setting is not present
except
ImportError
:
except
ImportError
:
pass
# no
D
jango
pass
# no
d
jango
This diff is collapsed.
Click to expand it.
pikatasks/worker.py
+
3
−
2
View file @
72ae0ef4
...
@@ -121,9 +121,10 @@ def _task_process(tasks, control_queue):
...
@@ -121,9 +121,10 @@ def _task_process(tasks, control_queue):
with
utils
.
get_connection
()
as
conn
:
with
utils
.
get_connection
()
as
conn
:
channel
=
conn
.
channel
()
channel
=
conn
.
channel
()
for
task
in
tasks
:
for
task
in
tasks
:
assert
getattr
(
task
,
"
is_pikatasks_task
"
,
False
)
and
callable
(
task
),
"
Not a task: {0}
"
.
format
(
task
)
callback
=
getattr
(
task
,
"
as_callback
"
,
None
)
assert
callback
and
callable
(
callback
),
"
Not a valid task: {0}
"
.
format
(
task
)
subprocess_logger
.
debug
(
log_prefix
+
"
Registering task {t} on queue {q}
"
.
format
(
t
=
task
.
task_name
,
q
=
task
.
task_queue
))
subprocess_logger
.
debug
(
log_prefix
+
"
Registering task {t} on queue {q}
"
.
format
(
t
=
task
.
task_name
,
q
=
task
.
task_queue
))
channel
.
basic_consume
(
tas
k
,
queue
=
task
.
task_queue
)
channel
.
basic_consume
(
callbac
k
,
queue
=
task
.
task_queue
)
check_control_queue
()
check_control_queue
()
channel
.
start_consuming
()
channel
.
start_consuming
()
channel
.
close
()
channel
.
close
()
...
...
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