jobs.py 1.56 KB
Newer Older
Andreas Valder's avatar
Andreas Valder committed
1
2
from server import *
import traceback
Andreas Valder's avatar
Andreas Valder committed
3
import json
Andreas Valder's avatar
Andreas Valder committed
4
5
6
7
8

@app.route('/jobs/overview')
@register_navbar('Jobs', iconlib='fa',  icon='suitcase')
@mod_required
def jobs_overview():
Andreas Valder's avatar
Andreas Valder committed
9
10
11
	worker = query('SELECT * FROM worker ORDER BY last_ping DESC')
	jobs = query('SELECT * FROM jobs')
	return render_template('jobs_overview.html',worker=worker,jobs=jobs)
Andreas Valder's avatar
Andreas Valder committed
12

Andreas Valder's avatar
Andreas Valder committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def jobs_api_token_required(func):
	@wraps(func)
	def decorator(*args, **kwargs):
		if 'apikey' in request.values:
			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')