worker.py 1.24 KB
Newer Older
Andreas Valder's avatar
Andreas Valder committed
1
2
#!/usr/bin/env python3
import requests
Andreas Valder's avatar
Andreas Valder committed
3
4
5
6
7
8
import time
import threading
import sched
import random
import traceback
import configparser
Andreas Valder's avatar
Andreas Valder committed
9

Andreas Valder's avatar
Andreas Valder committed
10
11
12
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
config = configparser.ConfigParser()
config.read('config.ini')

scheduler = sched.scheduler()
def run_scheduler():
	time.sleep(1) # weird things on startup
	while True:
		scheduler.run()
		time.sleep(10)

def sched_func(delay, priority=0, firstdelay=None, args=[], kargs={}):
	print(delay)
	if firstdelay == None:
		firstdelay = random.randint(1, 10)
	def wrapper(func):
		def sched_wrapper():
			try:
				func(*args, **kargs)
			except Exception:
				traceback.print_exc()
			scheduler.enter(delay, priority, sched_wrapper)
		scheduler.enter(firstdelay, priority, sched_wrapper)
		return func
	return wrapper

threading.Thread(target=run_scheduler, daemon=True).start()

@sched_func(5)
def ping_website():
	r = requests.post(config['API']['BASE']+'/jobs/api/ping', params={'apikey': config['API']['KEY'], 'host':config['MAIN']['HOST']})
	if not r.status_code == 200:
		print("Error sending ping: ",r)

@sched_func(10)
def get_jobs():
	r = requests.post(config['API']['BASE']+'/jobs/api/schedule', json={'apikey': config['API']['KEY'], 'host':config['MAIN']['HOST'], 'jobtypes': 'thumbnail'})
	print(r.text)


while True:
	time.sleep(10)