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
989a7eae
Commit
989a7eae
authored
8 years ago
by
Andreas Valder
Browse files
Options
Downloads
Patches
Plain Diff
added basics for a jobs api
parent
9d545a54
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
db_schema.sql
+18
-0
18 additions, 0 deletions
db_schema.sql
jobs.py
+43
-7
43 additions, 7 deletions
jobs.py
server.py
+0
-1
0 additions, 1 deletion
server.py
sorter.py
+18
-0
18 additions, 0 deletions
sorter.py
templates/jobs_overview.html
+87
-0
87 additions, 0 deletions
templates/jobs_overview.html
with
166 additions
and
8 deletions
db_schema.sql
+
18
−
0
View file @
989a7eae
...
@@ -230,6 +230,24 @@ CREATE TABLE IF NOT EXISTS `sorterrorlog_data` (
...
@@ -230,6 +230,24 @@ CREATE TABLE IF NOT EXISTS `sorterrorlog_data` (
`time_created`
datetime
NOT
NULL
`time_created`
datetime
NOT
NULL
);
);
CREATE
TABLE
IF
NOT
EXISTS
`worker`
(
`hostname`
text
NOT
NULL
PRIMARY
KEY
,
`last_ping`
datetime
NOT
NULL
);
CREATE
TABLE
IF
NOT
EXISTS
`jobs`
(
`id`
INTEGER
NOT
NULL
PRIMARY
KEY
AUTOINCREMENT
,
`type`
text
NOT
NULL
,
`priority`
INTEGER
NOT
NULL
DEFAULT
0
,
`state`
text
NOT
NULL
DEFAULT
'ready'
,
`time_finished`
datetime
DEFAULT
''
,
`time_created`
datetime
NOT
NULL
,
`last_ping`
datetime
NOT
NULL
DEFAULT
''
,
`worker`
text
DEFAULT
NULL
,
`data`
text
NOT
NULL
DEFAULT
'{}'
,
`status`
text
NOT
NULL
DEFAULT
'{}'
);
CREATE
VIEW
IF
NOT
EXISTS
`courses`
AS
select
*
from
`courses_data`
where
(
not
(
`courses_data`
.
`deleted`
));
CREATE
VIEW
IF
NOT
EXISTS
`courses`
AS
select
*
from
`courses_data`
where
(
not
(
`courses_data`
.
`deleted`
));
CREATE
VIEW
IF
NOT
EXISTS
`lectures`
AS
select
`lectures_data`
.
*
from
`lectures_data`
join
`courses_data`
on
(
`courses_data`
.
`id`
=
`course_id`
)
where
(
not
(
`lectures_data`
.
`deleted`
or
`courses_data`
.
`deleted`
));
CREATE
VIEW
IF
NOT
EXISTS
`lectures`
AS
select
`lectures_data`
.
*
from
`lectures_data`
join
`courses_data`
on
(
`courses_data`
.
`id`
=
`course_id`
)
where
(
not
(
`lectures_data`
.
`deleted`
or
`courses_data`
.
`deleted`
));
CREATE
VIEW
IF
NOT
EXISTS
`videos`
AS
select
`videos_data`
.
*
from
`videos_data`
join
`lectures_data`
on
(
`lectures_data`
.
`id`
=
`lecture_id`
)
join
`courses_data`
on
(
`courses_data`
.
`id`
=
`course_id`
)
where
(
not
(
`videos_data`
.
`deleted`
or
`lectures_data`
.
`deleted`
or
`courses_data`
.
`deleted`
));
CREATE
VIEW
IF
NOT
EXISTS
`videos`
AS
select
`videos_data`
.
*
from
`videos_data`
join
`lectures_data`
on
(
`lectures_data`
.
`id`
=
`lecture_id`
)
join
`courses_data`
on
(
`courses_data`
.
`id`
=
`course_id`
)
where
(
not
(
`videos_data`
.
`deleted`
or
`lectures_data`
.
`deleted`
or
`courses_data`
.
`deleted`
));
...
...
This diff is collapsed.
Click to expand it.
jobs.py
+
43
−
7
View file @
989a7eae
from
server
import
*
from
server
import
*
import
traceback
import
traceback
import
json
@app.route
(
'
/jobs/overview
'
)
@app.route
(
'
/jobs/overview
'
)
@register_navbar
(
'
Jobs
'
,
iconlib
=
'
fa
'
,
icon
=
'
suitcase
'
)
@register_navbar
(
'
Jobs
'
,
iconlib
=
'
fa
'
,
icon
=
'
suitcase
'
)
@mod_required
@mod_required
def
jobs_overview
():
def
jobs_overview
():
# todo
worker
=
query
(
'
SELECT * FROM worker ORDER BY last_ping DESC
'
)
return
render_template
(
'
base.html
'
)
jobs
=
query
(
'
SELECT * FROM jobs
'
)
return
render_template
(
'
jobs_overview.html
'
,
worker
=
worker
,
jobs
=
jobs
)
@app.route
(
'
/jobs/api
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
def
jobs_api_token_required
(
func
):
@mod_required
@wraps
(
func
)
def
jobs_api
():
def
decorator
(
*
args
,
**
kwargs
):
# todo
if
'
apikey
'
in
request
.
values
:
return
render_template
(
'
base.html
'
)
token
=
request
.
values
[
'
apikey
'
]
elif
request
.
get_json
()
and
(
'
apikey
'
in
request
.
get_json
()):
token
=
request
.
get_json
()[
'
apikey
'
]
else
:
token
=
None
if
not
token
==
config
[
'
JOBS_API_KEY
'
]:
return
'
Permission denied
'
,
403
else
:
return
func
(
*
args
,
**
kwargs
)
return
decorator
def
date_json_handler
(
obj
):
return
obj
.
isoformat
()
if
hasattr
(
obj
,
'
isoformat
'
)
else
obj
@app.route
(
'
/jobs/api/ping
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
@jobs_api_token_required
def
jobs_ping
():
hostname
=
request
.
values
[
'
host
'
]
query
(
'
INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)
'
,
hostname
,
datetime
.
now
())
return
'
OK
'
,
200
@app.route
(
'
/jobs/api/schedule
'
,
methods
=
[
'
POST
'
])
@jobs_api_token_required
def
jobs_schedule
():
hostdata
=
request
.
get_json
()
print
(
hostdata
)
if
not
hostdata
:
return
'
no data
'
,
500
jobtypes
=
hostdata
[
'
jobtypes
'
]
if
'
jobtypes
'
in
hostdata
else
[]
for
i
in
query
(
'
SELECT * FROM jobs WHERE state =
"
ready
"
ORDER BY priority DESC
'
):
if
i
[
'
type
'
]
in
hostdata
[
'
jobtypes
'
].
split
(
'
,
'
):
job
=
i
break
return
Response
(
json
.
dumps
(
job
,
default
=
date_json_handler
),
mimetype
=
'
application/json
'
)
This diff is collapsed.
Click to expand it.
server.py
+
0
−
1
View file @
989a7eae
...
@@ -10,7 +10,6 @@ import random
...
@@ -10,7 +10,6 @@ import random
import
sched
import
sched
import
traceback
import
traceback
import
string
import
string
import
traceback
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
...
...
This diff is collapsed.
Click to expand it.
sorter.py
+
18
−
0
View file @
989a7eae
...
@@ -33,7 +33,25 @@ def insert_video(lectureid,dbfilepath,filepath,fileformatid):
...
@@ -33,7 +33,25 @@ def insert_video(lectureid,dbfilepath,filepath,fileformatid):
(?,0,?,?,
""
,
""
,
""
,?,?,?,?,
""
,?)
'''
,
(?,0,?,?,
""
,
""
,
""
,?,?,?,?,
""
,?)
'''
,
lectureid
,
dbfilepath
,
fileformatid
,
datetime
.
now
(),
datetime
.
now
(),
datetime
.
now
(),
-
1
,
os
.
stat
(
filepath
).
st_size
)
lectureid
,
dbfilepath
,
fileformatid
,
datetime
.
now
(),
datetime
.
now
(),
datetime
.
now
(),
-
1
,
os
.
stat
(
filepath
).
st_size
)
query
(
'
INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)
'
,
lectureid
,
video_id
,
dbfilepath
,
datetime
.
now
())
query
(
'
INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)
'
,
lectureid
,
video_id
,
dbfilepath
,
datetime
.
now
())
schedule_thumbnail
(
lectureid
)
def
schedule_thumbnail
(
lectureid
):
exists
=
query
(
'
SELECT * FROM jobs WHERE data LIKE ?
'
,
'
%
"
lectureid
"
:
"'
+
str
(
lectureid
)
+
'"
%
'
)
if
exists
:
return
path
=
query
(
'
SELECT path FROM videos WHERE lecture_id
'
)
videos
=
query
(
'''
SELECT videos.path
FROM videos
JOIN formats ON (videos.video_format = formats.id)
WHERE videos.lecture_id = ?
ORDER BY formats.prio DESC
'''
,
lectureid
)
if
videos
:
path
=
videos
[
0
][
'
path
'
]
else
:
return
data
=
'
{
"
lectureid
"
:
"'
+
str
(
lectureid
)
+
'"
,
"
path
"
:
"'
+
path
+
'"
}
'
query
(
'
INSERT INTO jobs (type, data, time_created) VALUES (
"
thumbnail
"
, ?, ?)
'
,
data
,
datetime
.
now
());
@app.route
(
'
/sort/now
'
)
@app.route
(
'
/sort/now
'
)
@mod_required
@mod_required
...
...
This diff is collapsed.
Click to expand it.
templates/jobs_overview.html
0 → 100644
+
87
−
0
View file @
989a7eae
{% extends "base.html" %}
{% block content %}
<div
class=
"panel-group"
>
<div
class=
"panel panel-default"
>
<div
class=
"panel-heading"
>
<h1
class=
"panel-title"
>
Worker
</h1>
</div>
<div
class=
"panel-collapse collapse in"
>
<div
class=
"panel-body"
>
<table
class=
"table"
>
<tr>
<th>
Hostname
</th>
<th>
letzter Ping
</th>
</tr>
{% for i in worker %}
{% set td = (datetime.now()-(i.last_ping) ) %}
{% if td
<
timedelta
(seconds=
10)
%}
<
tr
class=
"success"
>
{% elif td
<
timedelta
(seconds=
30)
%}
<
tr
class=
"warning"
>
{% else %}
<tr
class=
"danger"
>
{% endif %}
<td>
{{i.hostname}}
</td>
<td>
{{i.last_ping}}
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
<div
class=
"panel panel-default"
>
<div
class=
"panel-heading"
>
<h1
class=
"panel-title"
>
Jobs
</h1>
</div>
<div
class=
"panel-collapse collapse in"
>
<div
class=
"panel-body"
>
<table
class=
"table"
>
<tr>
<th>
ID
</th>
<th>
Type
</th>
<th>
Priority
</th>
<th>
Worker
</th>
<th>
letzter Ping
</th>
<th>
State
</th>
<th>
Eingereiht am
</th>
<th>
Fertig geworden am
</th>
<th>
Daten
</th>
</tr>
{% for i in jobs %}
{% if i.last_ping %}
{% set td = (datetime.now()-(i.last_ping) ) %}
{% else %}
{% set td = -1 %}
{% endif %}
{% if td == -1 %}
<tr>
{% elif td
<
timedelta
(seconds=
20)
%}
<
tr
class=
"success"
>
{% elif td
<
timedelta
(seconds=
60)
%}
<
tr
class=
"warning"
>
{% else %}
<tr
class=
"danger"
>
{% endif %}
<td>
{{i.id}}
</td>
<td>
{{i.type}}
</td>
<td>
{{i.priority}}
</td>
<td>
{{i.worker}}
</td>
<td>
{{i.last_ping}}
</td>
<td>
{{i.state}}
</td>
<td>
{{i.time_created}}
</td>
<td>
{{i.time_finished}}
</td>
<td>
{{i.data}}
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>
{% endblock %}
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