Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
beat.core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
beat
beat.core
Commits
dfca1e82
Commit
dfca1e82
authored
7 years ago
by
Philip ABBET
Browse files
Options
Downloads
Patches
Plain Diff
[scripts] worker.py: supports several jobs in parallel
parent
92dadf8f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
beat/core/scripts/worker.py
+53
-56
53 additions, 56 deletions
beat/core/scripts/worker.py
beat/core/test/test_worker.py
+3
-3
3 additions, 3 deletions
beat/core/test/test_worker.py
beat/core/worker.py
+2
-1
2 additions, 1 deletion
beat/core/worker.py
with
58 additions
and
60 deletions
beat/core/scripts/worker.py
+
53
−
56
View file @
dfca1e82
...
...
@@ -73,10 +73,11 @@ stop = False
class
ExecutionProcess
(
multiprocessing
.
Process
):
def
__init__
(
self
,
queue
,
prefix
,
data
,
cache
,
docker
,
images_cache
=
None
):
def
__init__
(
self
,
queue
,
job_id
,
prefix
,
data
,
cache
,
docker
,
images_cache
=
None
):
super
(
ExecutionProcess
,
self
).
__init__
()
self
.
queue
=
queue
self
.
job_id
=
job_id
self
.
prefix
=
prefix
self
.
data
=
data
self
.
cache
=
cache
...
...
@@ -235,57 +236,57 @@ def main(user_input=None):
# Process the requests
execution_process
=
None
current_job_id
=
None
execution_processes
=
[]
while
not
stop
:
# Send the result of the processing (if any)
if
(
execution_process
is
not
None
)
and
not
execution_process
.
is_alive
():
if
execution_process
.
exitcode
==
0
:
result
=
execution_process
.
queue
.
get
()
else
:
result
=
dict
(
system_error
=
'
Execution error in the subprocess
'
)
for
execution_process
in
execution_processes
:
if
not
execution_process
.
is_alive
():
if
execution_process
.
exitcode
==
0
:
result
=
execution_process
.
queue
.
get
()
else
:
result
=
dict
(
system_error
=
'
Execution error in the subprocess
'
)
if
result
.
has_key
(
'
result
'
):
content
=
simplejson
.
dumps
(
result
[
'
result
'
])
if
result
.
has_key
(
'
result
'
):
content
=
simplejson
.
dumps
(
result
[
'
result
'
])
status
=
WorkerController
.
DONE
if
result
[
'
result
'
][
'
status
'
]
!=
0
:
status
=
WorkerController
.
JOB_ERROR
status
=
WorkerController
.
DONE
if
result
[
'
result
'
][
'
status
'
]
!=
0
:
status
=
WorkerController
.
JOB_ERROR
logger
.
info
(
"
Job #%s completed
"
,
current_
job_id
)
logger
.
debug
(
'
send:
"""
%s
"""'
%
content
.
rstrip
())
logger
.
info
(
"
Job #%s completed
"
,
execution_process
.
job_id
)
logger
.
debug
(
'
send:
"""
%s
"""'
%
content
.
rstrip
())
message
=
[
status
,
current_
job_id
,
content
]
elif
result
.
has_key
(
'
error
'
):
logger
.
error
(
result
[
'
error
'
])
message
=
[
status
,
execution_process
.
job_id
,
content
]
elif
result
.
has_key
(
'
error
'
):
logger
.
error
(
result
[
'
error
'
])
message
=
[
WorkerController
.
JOB_ERROR
,
current_
job_id
,
]
message
=
[
WorkerController
.
JOB_ERROR
,
execution_process
.
job_id
,
]
message
+=
result
[
'
details
'
]
message
+=
result
[
'
details
'
]
else
:
logger
.
error
(
result
[
'
system_error
'
])
else
:
logger
.
error
(
result
[
'
system_error
'
])
message
=
[
WorkerController
.
ERROR
,
current_
job_id
,
result
[
'
system_error
'
]
]
message
=
[
WorkerController
.
ERROR
,
execution_process
.
job_id
,
result
[
'
system_error
'
]
]
socket
.
send_multipart
(
message
)
socket
.
send_multipart
(
message
)
execution_process
=
None
execution_process
es
.
remove
(
execution_process
)
if
execution_process
is
None
:
if
len
(
execution_process
es
)
==
0
:
timeout
=
1000
# ms
else
:
timeout
=
100
...
...
@@ -309,46 +310,42 @@ def main(user_input=None):
job_id
=
parts
[
1
]
data
=
simplejson
.
loads
(
parts
[
2
])
# Check that the worker isn't busy
if
execution_process
is
not
None
:
socket
.
send_multipart
([
WorkerController
.
ERROR
,
job_id
,
'
Worker is already busy
'
])
continue
# Start the execution
logger
.
info
(
"
Running
'
%s
'
with job id #%s
"
,
data
[
'
algorithm
'
],
job_id
)
current_job_id
=
job_id
execution_process
=
ExecutionProcess
(
multiprocessing
.
Queue
(),
prefix
,
data
,
cache
,
docker
=
args
[
'
--docker
'
],
images_cache
=
docker_images_cache
)
execution_process
=
ExecutionProcess
(
multiprocessing
.
Queue
(),
job_id
,
prefix
,
data
,
cache
,
docker
=
args
[
'
--docker
'
],
images_cache
=
docker_images_cache
)
execution_process
.
start
()
execution_process
.
queue
.
get
()
execution_processes
.
append
(
execution_process
)
# Command: cancel
elif
command
==
WorkerController
.
CANCEL
:
# Check that the worker is busy
if
execution_process
is
None
:
job_id
=
parts
[
1
]
try
:
execution_process
=
[
p
for
p
in
execution_processes
if
p
.
job_id
==
job_id
]
except
:
socket
.
send_multipart
([
WorkerController
.
ERROR
,
"
Worker isn
'
t busy
"
"
Unknown job: %s
"
%
job_id
])
continue
# Kill the processing thread
logger
.
info
(
"
Cancelling the job #%s
"
,
current_
job_id
)
logger
.
info
(
"
Cancelling the job #%s
"
,
execution_process
.
job_id
)
execution_process
.
terminate
()
execution_process
.
join
()
execution_process
=
None
execution_process
es
.
remove
(
execution_process
)
socket
.
send_multipart
([
WorkerController
.
CANCELLED
,
current_
job_id
,
job_id
,
])
...
...
@@ -356,7 +353,7 @@ def main(user_input=None):
# Cleanup
i
f
execution_process
i
s
not
None
:
f
or
execution_process
i
n
execution_processes
:
execution_process
.
terminate
()
execution_process
.
join
()
...
...
This diff is collapsed.
Click to expand it.
beat/core/test/test_worker.py
+
3
−
3
View file @
dfca1e82
...
...
@@ -339,7 +339,7 @@ class TestOneWorker(unittest.TestCase):
config
[
'
algorithm
'
]
=
'
user/integers_echo_slow/1
'
self
.
controller
.
execute
(
WORKER1
,
1
,
config
)
self
.
controller
.
cancel
(
WORKER1
)
self
.
controller
.
cancel
(
WORKER1
,
1
)
(
worker
,
status
,
job_id
,
data
)
=
self
.
_wait
()
...
...
@@ -349,8 +349,8 @@ class TestOneWorker(unittest.TestCase):
self
.
assertEqual
(
len
(
data
),
0
)
def
test_error_cancel_
free_worker
(
self
):
self
.
controller
.
cancel
(
WORKER1
)
def
test_error_cancel_
unknown_job
(
self
):
self
.
controller
.
cancel
(
WORKER1
,
1
)
(
worker
,
status
,
job_id
,
data
)
=
self
.
_wait
()
...
...
This diff is collapsed.
Click to expand it.
beat/core/worker.py
+
2
−
1
View file @
dfca1e82
...
...
@@ -93,9 +93,10 @@ class WorkerController(object):
])
def
cancel
(
self
,
worker
):
def
cancel
(
self
,
worker
,
job_id
):
self
.
socket
.
send_multipart
([
str
(
worker
),
str
(
job_id
),
WorkerController
.
CANCEL
])
...
...
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