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
16c3200c
Commit
16c3200c
authored
7 years ago
by
Art
Browse files
Options
Downloads
Patches
Plain Diff
Task subprocesses now stop gracefully when parent process disappears.
parent
ada432a9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pikatasks/worker.py
+18
-9
18 additions, 9 deletions
pikatasks/worker.py
with
18 additions
and
9 deletions
pikatasks/worker.py
+
18
−
9
View file @
16c3200c
...
@@ -10,7 +10,7 @@ from . import django_compat
...
@@ -10,7 +10,7 @@ from . import django_compat
from
datetime
import
datetime
from
datetime
import
datetime
MSG_S
IGINT
=
"
MSG_S
IGINT
"
MSG_S
TOP
=
"
MSG_S
TOP
"
MSG_CHECK_FREQ
=
2
# seconds
MSG_CHECK_FREQ
=
2
# seconds
logger
=
logging
.
getLogger
(
"
pikatasks.worker
"
)
logger
=
logging
.
getLogger
(
"
pikatasks.worker
"
)
...
@@ -64,6 +64,7 @@ def _create_worker_process(tasks, control_queue):
...
@@ -64,6 +64,7 @@ def _create_worker_process(tasks, control_queue):
kwargs
=
dict
(
kwargs
=
dict
(
tasks
=
tasks
,
tasks
=
tasks
,
control_queue
=
control_queue
,
control_queue
=
control_queue
,
parent_pid
=
os
.
getpid
(),
)
)
)
)
p
.
start
()
p
.
start
()
...
@@ -75,7 +76,7 @@ def _stop_worker_processes(processes, control_queue):
...
@@ -75,7 +76,7 @@ def _stop_worker_processes(processes, control_queue):
deadline
=
datetime
.
now
()
+
settings
.
WORKER_GRACEFUL_STOP_TIMEOUT
deadline
=
datetime
.
now
()
+
settings
.
WORKER_GRACEFUL_STOP_TIMEOUT
while
datetime
.
now
()
<
deadline
:
while
datetime
.
now
()
<
deadline
:
while
control_queue
.
qsize
()
<
len
(
processes
)
*
2
:
while
control_queue
.
qsize
()
<
len
(
processes
)
*
2
:
control_queue
.
put
(
MSG_S
IGINT
)
control_queue
.
put
(
MSG_S
TOP
)
time
.
sleep
(
1
)
time
.
sleep
(
1
)
_remove_ended_processes
(
processes
,
expect_exited_processes
=
True
)
_remove_ended_processes
(
processes
,
expect_exited_processes
=
True
)
if
processes
:
if
processes
:
...
@@ -94,7 +95,7 @@ def _start_ignoring_sigint():
...
@@ -94,7 +95,7 @@ def _start_ignoring_sigint():
signal
.
signal
(
signal
.
SIGINT
,
signal
.
SIG_IGN
)
signal
.
signal
(
signal
.
SIGINT
,
signal
.
SIG_IGN
)
def
_task_process
(
tasks
,
control_queue
):
def
_task_process
(
tasks
,
control_queue
,
parent_pid
):
"""
This is a single process, that performs tasks.
"""
"""
This is a single process, that performs tasks.
"""
_start_ignoring_sigint
()
# no interruptions in the middle of the task, graceful exit is controlled by the main process
_start_ignoring_sigint
()
# no interruptions in the middle of the task, graceful exit is controlled by the main process
own_pid
=
os
.
getpid
()
own_pid
=
os
.
getpid
()
...
@@ -103,18 +104,26 @@ def _task_process(tasks, control_queue):
...
@@ -103,18 +104,26 @@ def _task_process(tasks, control_queue):
assert
tasks
assert
tasks
conn
,
channel
=
None
,
None
conn
,
channel
=
None
,
None
def
check_
control_
queue
():
def
control_
beat
():
# this function registers itself to be called again
# this function registers itself to be called again
stop
=
False
# check whether parent process is alive
if
os
.
getppid
()
!=
parent_pid
:
# got adopted, new owner is probably init
logger
.
error
(
"
Parent process disappeared :( Stopping.
"
)
stop
=
True
try
:
try
:
# check whether graceful stop is requested
msg
=
control_queue
.
get_nowait
()
msg
=
control_queue
.
get_nowait
()
if
msg
==
MSG_SIGINT
:
if
msg
==
MSG_STOP
:
subprocess_logger
.
debug
(
log_prefix
+
"
Stopping consuming messages from queues.
"
)
stop
=
True
channel
.
stop_consuming
()
else
:
else
:
subprocess_logger
.
error
(
log_prefix
+
"
Don
'
t know what to do with the control message: {msg}
"
.
format
(
msg
=
msg
))
subprocess_logger
.
error
(
log_prefix
+
"
Don
'
t know what to do with the control message: {msg}
"
.
format
(
msg
=
msg
))
except
Empty
:
except
Empty
:
pass
pass
conn
.
add_timeout
(
MSG_CHECK_FREQ
,
check_control_queue
)
# run this function again soon
if
stop
:
channel
.
stop_consuming
()
subprocess_logger
.
debug
(
log_prefix
+
"
Stopping consuming messages from queues.
"
)
conn
.
add_timeout
(
MSG_CHECK_FREQ
,
control_beat
)
# run this function again soon
try
:
try
:
subprocess_logger
.
debug
(
log_prefix
+
"
Opening a connection...
"
)
subprocess_logger
.
debug
(
log_prefix
+
"
Opening a connection...
"
)
...
@@ -128,7 +137,7 @@ def _task_process(tasks, control_queue):
...
@@ -128,7 +137,7 @@ def _task_process(tasks, control_queue):
subprocess_logger
.
debug
(
log_prefix
+
"
Registered task {t} on queue {q}
"
.
format
(
t
=
task
.
task_name
,
q
=
task
.
task_queue
))
subprocess_logger
.
debug
(
log_prefix
+
"
Registered task {t} on queue {q}
"
.
format
(
t
=
task
.
task_name
,
q
=
task
.
task_queue
))
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
"
Could not register task {t}. {e.__class__.__name__}: {e}
"
.
format
(
t
=
task
,
e
=
e
))
logger
.
error
(
"
Could not register task {t}. {e.__class__.__name__}: {e}
"
.
format
(
t
=
task
,
e
=
e
))
check_
control_
queue
()
# initial
control_
beat
()
# initial
channel
.
start_consuming
()
channel
.
start_consuming
()
channel
.
close
()
channel
.
close
()
except
Exception
as
e
:
except
Exception
as
e
:
...
...
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