jobs.py 3.12 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
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
50
@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'])
Andreas Valder's avatar
Andreas Valder committed
51
@jobs_api_token_required
Andreas Valder's avatar
Andreas Valder committed
52
53
def jobs_worker_ping(hostname):
	query('INSERT OR REPLACE INTO worker (hostname, last_ping) values (?, ?)', hostname, datetime.now())
Andreas Valder's avatar
Andreas Valder committed
54
55
	return 'OK',200

Andreas Valder's avatar
Andreas Valder committed
56
@app.route('/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
62
63
64
65
66
67
68
69
70
71
72
	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)
Andreas Valder's avatar
Andreas Valder committed
73
74
	return 'OK',200

Andreas Valder's avatar
Andreas Valder committed
75
@app.route('/jobs/api/worker/<hostname>/schedule', methods=['POST'])
Andreas Valder's avatar
Andreas Valder committed
76
@jobs_api_token_required
Andreas Valder's avatar
Andreas Valder committed
77
def jobs_schedule(hostname):
Andreas Valder's avatar
Andreas Valder committed
78
79
	hostdata =  request.get_json()
	if not hostdata:
Andreas Valder's avatar
Andreas Valder committed
80
		return 'no hostdata sent', 400
Andreas Valder's avatar
Andreas Valder committed
81
	job = None
Andreas Valder's avatar
Andreas Valder committed
82
	jobtypes = hostdata['jobtypes'] if 'jobtypes' in hostdata else []
Andreas Valder's avatar
Andreas Valder committed
83
84
85
86
87
88
	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
89
90
91
		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
92
93
94
95
96
		try:
			query("COMMIT")
		except:
			job = None
			sleep(random.randinti(0,50))
Andreas Valder's avatar
Andreas Valder committed
97
	return Response(json.dumps(job, default=date_json_handler),  mimetype='application/json')