1
1
import os
2
2
from secrets import token_urlsafe
3
3
4
- from flask import Blueprint , jsonify , request , abort
4
+ from flask import Blueprint , jsonify , request , abort , send_file , url_for
5
5
from sqlalchemy import and_ , or_
6
6
from sqlalchemy .orm .exc import NoResultFound
7
7
10
10
from hwut_server .models import Jobs , JobStatus , Runners
11
11
from hwut_server .utils import FILE_STORAGE
12
12
13
- mod = Blueprint ('runner_jobs' , __name__ )
13
+ mod = Blueprint ('runner_jobs' , __name__ , url_prefix = '/runner_api' )
14
14
15
15
16
16
@mod .route ('/get' , methods = ['GET' ])
@@ -19,26 +19,23 @@ def job_get():
19
19
"""
20
20
Get a new job
21
21
"""
22
- runner = Runners .query .filter (id = request .authorization .username ).one ()
22
+ runner = Runners .query .filter (Runners . id == request .authorization .username ).one ()
23
23
24
24
# check if a job is already queued for the runner
25
- try :
26
- job = Jobs .query .filter (and_ (Jobs .runner == runner .id , Jobs .status == JobStatus .QUEUED )).one ()
25
+ job = Jobs .query .filter (and_ (Jobs .runner == runner .id , Jobs .status == JobStatus .QUEUED )).first ()
27
26
# Else queue a new job
28
- except NoResultFound :
29
- try :
30
- filters = list ()
31
- filters .append (Jobs .status == JobStatus .WAITING )
32
- filters .append (Jobs .microcontroller == runner .microcontroller )
33
- filters .append (or_ (Jobs .board == runner .board , Jobs .board == '' ))
34
- job = Jobs .query .filter (and_ (* filters )).order_by (Jobs .created .desc ()).first ()
35
- except NoResultFound :
27
+ if job is None :
28
+ #try:
29
+ filters = list ()
30
+ filters .append (Jobs .status == JobStatus .WAITING )
31
+ filters .append (Jobs .microcontroller == runner .target_microcontroller )
32
+ filters .append (or_ (Jobs .board == runner .target_board , Jobs .board == '' ))
33
+ job = Jobs .query .filter (and_ (* filters )).order_by (Jobs .created .desc ()).first ()
34
+ #except:
35
+ # return abort(500)
36
+ if job is None :
36
37
return '' , 404
37
- except :
38
- return abort (500 )
39
-
40
38
runner .acquire ()
41
-
42
39
job .status = JobStatus .QUEUED
43
40
job .runner = runner .id
44
41
@@ -47,9 +44,9 @@ def job_get():
47
44
48
45
return jsonify ({
49
46
'id' : job .id ,
50
- 'executable_location' : request .url_root + 'executable' ,
51
- 'log_location' : request .url_root + 'log' ,
52
- 'other_location' : request .url_root + 'other' ,
47
+ 'executable_location' : request .url_root [: - 1 ] + url_for ( 'runner_jobs.job_get_executable' ) ,
48
+ 'log_location' : request .url_root [: - 1 ] + url_for ( 'runner_jobs.job_upload_log' ) ,
49
+ 'other_location' : request .url_root [: - 1 ] + url_for ( 'runner_jobs.job_upload_other' ) ,
53
50
'time_limit' : job .duration_limit_seconds ,
54
51
'baudrate' : 115200 , # FIXME
55
52
})
@@ -60,13 +57,15 @@ def job_get():
60
57
def job_get_executable ():
61
58
runner_id = request .authorization .username
62
59
try :
63
- job = Jobs .query .filter (and_ (Jobs .runner == runner_id , Jobs .status == JobStatus .RUNNING )).one ()
60
+ job = Jobs .query .filter (and_ (Jobs .runner == runner_id , Jobs .status == JobStatus .QUEUED )).one ()
64
61
except NoResultFound :
65
62
return abort (400 )
66
63
67
64
if not job .filename_executable :
68
65
return abort (409 , 'executable file already exists' )
69
66
67
+ return send_file (os .path .join ('..' , FILE_STORAGE , job .filename_executable ))
68
+
70
69
71
70
@mod .route ('/start' , methods = ['POST' ])
72
71
@requires_runner_authentication
@@ -75,8 +74,15 @@ def job_start():
75
74
Runner confirms job has stopped
76
75
"""
77
76
runner_id = request .authorization .username
77
+
78
+ # mark any other job as finished
79
+ jobs = Jobs .query .filter (and_ (Jobs .runner == runner_id , Jobs .status == JobStatus .RUNNING )).all ()
80
+ for job in jobs :
81
+ job .status = JobStatus .ERROR
82
+ db .session .commit ()
83
+
78
84
try :
79
- job = Jobs .query .filter (and_ (Jobs .runner == runner_id , Jobs .status == JobStatus .RUNNING )).one ()
85
+ job = Jobs .query .filter (and_ (Jobs .runner == runner_id , Jobs .status == JobStatus .QUEUED )).one ()
80
86
job .status = JobStatus .RUNNING
81
87
db .session .commit ()
82
88
return '' , 204
@@ -90,9 +96,9 @@ def job_stop():
90
96
"""
91
97
Runner confirms job is about to start
92
98
"""
93
- runner = Runners .query .filter (id = request .authorization .username ).one ()
99
+ runner = Runners .query .filter (Runners . id == request .authorization .username ).one ()
94
100
try :
95
- job = Jobs .query .filter (and_ (Jobs .runner == runner .id , Jobs .status == JobStatus .QUEUED )).one ()
101
+ job = Jobs .query .filter (and_ (Jobs .runner == runner .id , Jobs .status == JobStatus .RUNNING )).one ()
96
102
if request .args .get ('error' ) and request .args .get ('error' ) == '1' :
97
103
job .status = JobStatus .ERROR
98
104
else :
0 commit comments