jobs.py 2.05 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
import random
Andreas Valder's avatar
Andreas Valder committed
5
6
7
8
9

@app.route('/jobs/overview')
@register_navbar('Jobs', iconlib='fa',  icon='suitcase')
@mod_required
def jobs_overview():
Andreas Valder's avatar
Andreas Valder committed
10
11
12
	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
13

Andreas Valder's avatar
Andreas Valder committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
Andreas Valder's avatar
Andreas Valder committed
35
def jobs_worker_ping():
Andreas Valder's avatar
Andreas Valder committed
36
37
38
39
	hostname = request.values['host']
	query('INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)',hostname,datetime.now())
	return 'OK',200

Andreas Valder's avatar
Andreas Valder committed
40
41
42
43
44
45
46
@app.route('/jobs/api/job/<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)
	return 'OK',200

Andreas Valder's avatar
Andreas Valder committed
47
48
49
50
51
52
53
@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
Andreas Valder's avatar
Andreas Valder committed
54
	job = None
Andreas Valder's avatar
Andreas Valder committed
55
	jobtypes = hostdata['jobtypes'] if 'jobtypes' in hostdata else []
Andreas Valder's avatar
Andreas Valder committed
56
57
58
59
60
61
62
63
64
65
66
67
	while (not job):
		query("BEGIN")
		for i in query('SELECT * FROM jobs WHERE state = "ready" ORDER BY priority DESC'):
			if i['type'] in hostdata['jobtypes'].split(','):
				job=i
				break
		modify('UPDATE jobs SET state="running", worker = ? WHERE id = ?', hostdata['host'], job['id'])
		try:
			query("COMMIT")
		except:
			job = None
			sleep(random.randinti(0,50))
Andreas Valder's avatar
Andreas Valder committed
68
	return Response(json.dumps(job, default=date_json_handler),  mimetype='application/json')