Creating Supervisors in Python with Multi-Threading and Process Manager

And how to schedule your supervisors using the schedule module

Image for post
Image for post

A supervisor is an automated way to ensure that a particular service or element of your system is up and running. They check if certain criteria are met in order to determine whether everything is operating as expected. From here they can report their results, as a database record to display on a Service Status page, for example.

pip install schedule, requests

Why Supervisors?

How should supervisors be run?

#perform supervisor task
def supervisorTask():
...
#record to my database that the supervisor is running
def supervisorCheckIn():
...
#this function will execute our tasks in a separate thread
def threadTask(job_function, arguments):
...
#schedule my supervisor to run and check itself in every 10 minutesschedule.every(10).minutes.do(threadTask(superviorTask))
schedule.every(10).minutes.do(threadTask(supervisorCheckIn))
while 1:
schedule.run_pending()
time.sleep(1)
supervisorctl start mySupervisor

Supervisor Example

import requests, time, schedule, threading
from my_status import *

#perform supervisor task
def superviorTask():

r = requests.get('https://medium.com')

if r.status_code == 200 and len(r.text) > 0:
status = STATUS_LIVE
else:
status = STATUS_INACTIVE
#record to my database that the supervisor is running
def supervisorCheckIn():
#pymongo, mysql, <your favourite database> query here.
#function for handling threads
def threadTask(job_function):
job_thread = threading.Thread(target=job_function)
job_thread.start()
#start schedule
schedule.every(10).minutes.do(threadTask, superviorTask)
schedule.every(10).minutes.do(threadTask, supervisorCheckIn)
while 1:
schedule.run_pending()
time.sleep(1)

Status Codes

"""
My Status Codes
used throughout my supervisor reporting
"""
STATUS_LIVE = 'Live'
STATUS_INACTIVE = 'Inactive'

Multi Threading

job_thread = threading.Thread(target=job_function)
job_thread.start()
job_thread = threading.Thread(target=job_function, args=(arg1, arg2, ))
job_thread.start()

Scheduling

schedule.every(10).minutes.do(threadTask, superviorTask, arg1, arg2)

Where should supervisors be run?

Further Considerations

Programmer and Author. Director @ JKRBInvestments.com. Creator of ChineseGrammarReview.app for iOS.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store