jobs.py 2.92 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
@app.route('/internal/jobs/overview')
Andreas Valder's avatar
Andreas Valder committed
7
8
9
@register_navbar('Jobs', iconlib='fa',  icon='suitcase')
@mod_required
def jobs_overview():
Andreas Valder's avatar
Andreas Valder committed
10
	worker = query('SELECT * FROM worker ORDER BY last_ping DESC')
11
	jobs = query('SELECT * FROM jobs WHERE state != "finished"')
Andreas Valder's avatar
Andreas Valder committed
12
	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
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

Andreas Valder's avatar
Andreas Valder committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@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

50
@app.route('/internal/jobs/api/worker/<hostname>/ping', methods=['GET', 'POST'])
Andreas Valder's avatar
Andreas Valder committed
51
@jobs_api_token_required
Andreas Valder's avatar
Andreas Valder committed
52
def jobs_worker_ping(hostname):
53
	query('REPLACE INTO worker (hostname, last_ping) values (?, ?)', hostname, datetime.now())
Andreas Valder's avatar
Andreas Valder committed
54
55
	return 'OK',200

56
@app.route('/internal/jobs/api/job/<int:id>/ping', methods=['GET', 'POST'])
Andreas Valder's avatar
Andreas Valder committed
57
58
59
@jobs_api_token_required
def jobs_ping(id):
	hostname = request.values['host']
Andreas Valder's avatar
Andreas Valder committed
60
61
	status = json.dumps(request.values['status'], default=date_json_handler)
	state = request.values['state']
62
63
	if state == 'finished':
		query('UPDATE jobs SET time_finished = ?, status = ?, state = "finished" where id = ?', datetime.now(), status, id)
Andreas Valder's avatar
Andreas Valder committed
64
	else:
65
		query('UPDATE jobs SET worker = ?, last_ping = ?, status = ?, state = ? where id = ?', hostname, datetime.now(), status, state, id)
Andreas Valder's avatar
Andreas Valder committed
66
67
	return 'OK',200

68
@app.route('/internal/jobs/api/worker/<hostname>/schedule', methods=['POST'])
Andreas Valder's avatar
Andreas Valder committed
69
@jobs_api_token_required
Andreas Valder's avatar
Andreas Valder committed
70
def jobs_schedule(hostname):
Andreas Valder's avatar
Andreas Valder committed
71
72
	hostdata =  request.get_json()
	if not hostdata:
Andreas Valder's avatar
Andreas Valder committed
73
		return 'no hostdata sent', 400
Andreas Valder's avatar
Andreas Valder committed
74
	job = None
Andreas Valder's avatar
Andreas Valder committed
75
	jobtypes = hostdata['jobtypes'] if 'jobtypes' in hostdata else []
Andreas Valder's avatar
Andreas Valder committed
76
77
78
79
80
81
	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
Andreas Valder's avatar
Andreas Valder committed
82
83
84
		if not job:
			return 'no jobs', 503
		modify('UPDATE jobs SET state="scheduled", worker = ?, time_scheduled = ? WHERE id = ?', hostname, datetime.now(), job['id']) 
Andreas Valder's avatar
Andreas Valder committed
85
86
87
88
89
		try:
			query("COMMIT")
		except:
			job = None
			sleep(random.randinti(0,50))
Andreas Valder's avatar
Andreas Valder committed
90
	return Response(json.dumps(job, default=date_json_handler),  mimetype='application/json')