Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Jannik Hellenkamp
website
Commits
5b5b02c7
Commit
5b5b02c7
authored
8 years ago
by
Andreas Valder
Browse files
Options
Downloads
Patches
Plain Diff
extended job api
parent
e7532b6e
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
db_schema.sql
+1
-0
1 addition, 0 deletions
db_schema.sql
jobs.py
+40
-11
40 additions, 11 deletions
jobs.py
with
41 additions
and
11 deletions
db_schema.sql
+
1
−
0
View file @
5b5b02c7
...
@@ -241,6 +241,7 @@ CREATE TABLE IF NOT EXISTS `jobs` (
...
@@ -241,6 +241,7 @@ CREATE TABLE IF NOT EXISTS `jobs` (
`priority`
INTEGER
NOT
NULL
DEFAULT
0
,
`priority`
INTEGER
NOT
NULL
DEFAULT
0
,
`state`
text
NOT
NULL
DEFAULT
'ready'
,
`state`
text
NOT
NULL
DEFAULT
'ready'
,
`time_finished`
datetime
DEFAULT
''
,
`time_finished`
datetime
DEFAULT
''
,
`time_scheduled`
datetime
DEFAULT
''
,
`time_created`
datetime
NOT
NULL
,
`time_created`
datetime
NOT
NULL
,
`last_ping`
datetime
NOT
NULL
DEFAULT
''
,
`last_ping`
datetime
NOT
NULL
DEFAULT
''
,
`worker`
text
DEFAULT
NULL
,
`worker`
text
DEFAULT
NULL
,
...
...
This diff is collapsed.
Click to expand it.
jobs.py
+
40
−
11
View file @
5b5b02c7
...
@@ -30,27 +30,54 @@ def jobs_api_token_required(func):
...
@@ -30,27 +30,54 @@ def jobs_api_token_required(func):
def
date_json_handler
(
obj
):
def
date_json_handler
(
obj
):
return
obj
.
isoformat
()
if
hasattr
(
obj
,
'
isoformat
'
)
else
obj
return
obj
.
isoformat
()
if
hasattr
(
obj
,
'
isoformat
'
)
else
obj
@app.route
(
'
/jobs/api/ping
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@sched_func
(
10
)
def
jobs_catch_broken
():
# scheduled but never pinged
query
(
'
BEGIN
'
)
query
(
'
UPDATE jobs SET state=
"
ready
"
WHERE state=
"
scheduled
"
and time_scheduled < ?
'
,
datetime
.
now
()
-
timedelta
(
seconds
=
10
))
try
:
query
(
'
COMMIT
'
)
except
:
pass
# no pings since 60s
query
(
'
BEGIN
'
)
query
(
'
UPDATE jobs SET state=
"
failed
"
WHERE state=
"
running
"
and last_ping < ?
'
,
datetime
.
now
()
-
timedelta
(
seconds
=
60
))
try
:
query
(
'
COMMIT
'
)
except
:
pass
@app.route
(
'
/jobs/api/worker/<hostname>/ping
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@jobs_api_token_required
@jobs_api_token_required
def
jobs_worker_ping
():
def
jobs_worker_ping
(
hostname
):
hostname
=
request
.
values
[
'
host
'
]
query
(
'
INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)
'
,
hostname
,
datetime
.
now
())
query
(
'
INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)
'
,
hostname
,
datetime
.
now
())
return
'
OK
'
,
200
return
'
OK
'
,
200
@app.route
(
'
/jobs/api/job/<id>/ping
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@app.route
(
'
/jobs/api/job/<
int:
id>/ping
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@jobs_api_token_required
@jobs_api_token_required
def
jobs_ping
(
id
):
def
jobs_ping
(
id
):
hostname
=
request
.
values
[
'
host
'
]
hostname
=
request
.
values
[
'
host
'
]
query
(
'
UPDATE jobs SET worker = ?, last_ping = ? where id = ?
'
,
hostname
,
datetime
.
now
(),
id
)
status
=
json
.
dumps
(
request
.
values
[
'
status
'
],
default
=
date_json_handler
)
state
=
request
.
values
[
'
state
'
]
query
(
'
UPDATE jobs SET worker = ?, last_ping = ?, status = ?, state = ? where id = ?
'
,
hostname
,
datetime
.
now
(),
status
,
state
,
id
)
return
'
OK
'
,
200
@app.route
(
'
/jobs/api/job/<int:id>/finished
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@jobs_api_token_required
def
jobs_finished
(
id
):
if
'
status
'
in
request
.
values
:
status
=
request
.
values
[
'
status
'
]
else
:
status
=
json
.
dumps
(
request
.
get_json
()[
'
status
'
],
default
=
date_json_handler
)
query
(
'
UPDATE jobs SET time_finished = ?, status = ?, state =
"
finished
"
where id = ?
'
,
datetime
.
now
(),
status
,
id
)
return
'
OK
'
,
200
return
'
OK
'
,
200
@app.route
(
'
/jobs/api/schedule
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/jobs/api/
worker/<hostname>/
schedule
'
,
methods
=
[
'
POST
'
])
@jobs_api_token_required
@jobs_api_token_required
def
jobs_schedule
():
def
jobs_schedule
(
hostname
):
hostdata
=
request
.
get_json
()
hostdata
=
request
.
get_json
()
print
(
hostdata
)
if
not
hostdata
:
if
not
hostdata
:
return
'
no
data
'
,
5
00
return
'
no
hostdata sent
'
,
4
00
job
=
None
job
=
None
jobtypes
=
hostdata
[
'
jobtypes
'
]
if
'
jobtypes
'
in
hostdata
else
[]
jobtypes
=
hostdata
[
'
jobtypes
'
]
if
'
jobtypes
'
in
hostdata
else
[]
while
(
not
job
):
while
(
not
job
):
...
@@ -59,7 +86,9 @@ def jobs_schedule():
...
@@ -59,7 +86,9 @@ def jobs_schedule():
if
i
[
'
type
'
]
in
hostdata
[
'
jobtypes
'
].
split
(
'
,
'
):
if
i
[
'
type
'
]
in
hostdata
[
'
jobtypes
'
].
split
(
'
,
'
):
job
=
i
job
=
i
break
break
modify
(
'
UPDATE jobs SET state=
"
running
"
, worker = ? WHERE id = ?
'
,
hostdata
[
'
host
'
],
job
[
'
id
'
])
if
not
job
:
return
'
no jobs
'
,
503
modify
(
'
UPDATE jobs SET state=
"
scheduled
"
, worker = ?, time_scheduled = ? WHERE id = ?
'
,
hostname
,
datetime
.
now
(),
job
[
'
id
'
])
try
:
try
:
query
(
"
COMMIT
"
)
query
(
"
COMMIT
"
)
except
:
except
:
...
...
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