Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Video AG Infrastruktur
website
Commits
5b5b02c7
Commit
5b5b02c7
authored
Feb 11, 2017
by
Andreas Valder
Browse files
extended job api
parent
e7532b6e
Changes
2
Hide whitespace changes
Inline
Side-by-side
db_schema.sql
View file @
5b5b02c7
...
...
@@ -241,6 +241,7 @@ CREATE TABLE IF NOT EXISTS `jobs` (
`priority`
INTEGER
NOT
NULL
DEFAULT
0
,
`state`
text
NOT
NULL
DEFAULT
'ready'
,
`time_finished`
datetime
DEFAULT
''
,
`time_scheduled`
datetime
DEFAULT
''
,
`time_created`
datetime
NOT
NULL
,
`last_ping`
datetime
NOT
NULL
DEFAULT
''
,
`worker`
text
DEFAULT
NULL
,
...
...
jobs.py
View file @
5b5b02c7
...
...
@@ -30,27 +30,54 @@ def jobs_api_token_required(func):
def
date_json_handler
(
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
def
jobs_worker_ping
():
hostname
=
request
.
values
[
'host'
]
query
(
'INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)'
,
hostname
,
datetime
.
now
())
def
jobs_worker_ping
(
hostname
):
query
(
'INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)'
,
hostname
,
datetime
.
now
())
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
def
jobs_ping
(
id
):
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
@
app
.
route
(
'/jobs/api/schedule'
,
methods
=
[
'POST'
])
@
app
.
route
(
'/jobs/api/
worker/<hostname>/
schedule'
,
methods
=
[
'POST'
])
@
jobs_api_token_required
def
jobs_schedule
():
def
jobs_schedule
(
hostname
):
hostdata
=
request
.
get_json
()
print
(
hostdata
)
if
not
hostdata
:
return
'no
data
'
,
5
00
return
'no
hostdata sent
'
,
4
00
job
=
None
jobtypes
=
hostdata
[
'jobtypes'
]
if
'jobtypes'
in
hostdata
else
[]
while
(
not
job
):
...
...
@@ -59,7 +86,9 @@ def jobs_schedule():
if
i
[
'type'
]
in
hostdata
[
'jobtypes'
].
split
(
','
):
job
=
i
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
:
query
(
"COMMIT"
)
except
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment