Skip to content
Snippets Groups Projects
Select Git revision
  • f4a65647a0a01d6e6bdc401811a0aa9655ad9b6b
  • master default protected
  • forbid-save-as
  • upload-via-token
  • moodle-integration
  • patch-double-tap-seek
  • patch_datum_anzeigen
  • patch_raum_anzeigen
  • intros
  • live_sources
  • bootstrap4
  • modules
12 results

db.py

Blame
  • Forked from Video AG Infrastruktur / website
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    manager.py 1.47 KiB
    #!/usr/bin/env python3
    import time
    import threading
    import sched
    import random
    import traceback
    import configparser
    import psutil
    import subprocess
    from workerapi import WorkerApi
    import json
    
    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(1)
    
    def sched_func(delay, priority=0, firstdelay=None, args=[], kargs={}):
    	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
    
    api = WorkerApi(config['API']['BASE'],config['API']['KEY'])
    
    threading.Thread(target=run_scheduler, daemon=True).start()
    
    @sched_func(15)
    def ping_website_for_host():
    	# ping so the website knows our host is still alive
    	if not api.worker_ping():
    		print("Error sending host ping")
    
    procs = []
    
    while True:
    	for p in procs:
    		p.poll()
    		if p.returncode == None:
    			continue
    		p.wait()
    		procs.remove(p)
    	if psutil.cpu_percent() > 85:
    		time.sleep(0.1)
    		continue
    	j = api.worker_schedule(config['JOBS']['TYPES'])
    	if not j:
    		time.sleep(30)
    		continue
    	print("started jobid %i"%j['id'])
    	procs.append(subprocess.Popen(['./worker.py', str(j['id']), str(j['type']), str(j['priority']) , str(j['data']) ] ))
    	time.sleep(1)