Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

A script to monitor that functions are working properly

If you have a question or need help, this is the place to be.
Post Reply
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

A script to monitor that functions are working properly

Post by krambriw »

Hello,

I have played a bit with creating a script for a special purpose using the email sending sample provided by Bartman (thanks!) and it works as follows:

- The script starts a MainTask that then schedules itself and performs certain tasks as long as all conditions are fulfilled
- The script also starts a thread that schedules a LiveCheck monitoring function

As long as the MainTask is running, the LiveCheck is OK and nothing else except logging really happens. However, when the MainTask stops, the LiveCheck sends an email with an error report.

Could you please give me a feedback on the script below if this should be done differently or if you have any ideas to make it better/simpler/more professional?

I have attached an xml-file for the simple eg project so that you can run it easily. I have commented out the actual email sending...

First start the "LiveCheckWithThread" script and you should see activities in the log.

Then stop the MainTask by starting the "Stop" script (it sets a eg.globals that is used as a condition flag to stop it)

Best regards, Walter

Code: Select all

import time, smtplib
from threading import Thread

# Initialization...
try:
    dummy
except NameError:
    dummy = 0
    eg.globals.heartBeat = 0
    m_smtp_address_com = "smtp.provider.com"
    m_user = "user"
    m_password = "passw"
    m_recipient = "[email protected]"
    m_sender = "[email protected]"

def MainTask():
#   remain = 61 - int(time.strftime("%S", time.localtime()))
    remain = 1
    sc1 = eg.scheduler.AddTask(remain, MainTask)
    print "Alive..."+str(remain)+"  "+str(eg.globals.heartBeat)
    if eg.globals.heartBeat == 99:
        eg.scheduler.CancelTask(sc1)
        print "Main task is stopping..."
        eg.StopMacro()
    else:
        eg.globals.heartBeat = 1

def LiveCheck():
#   remain = 61 - int(time.strftime("%S", time.localtime()))
#   remain += 10
    remain = 2
    sc2 = eg.scheduler.AddTask(remain, LiveCheck)
    print "Live check..."+str(remain)+"  "+str(eg.globals.heartBeat)
    if eg.globals.heartBeat == 1:
        eg.globals.heartBeat = 0
    else:
        print "Main task is not working correctly...sending mail"
        eg.scheduler.CancelTask(sc2)
#        mail(
#            m_sender,
#            m_recipient,
#            "Script has stopped running...",
#            "EG Script Error",
#            m_smtp_address_com,
#            m_user,
#            m_password
#        )
        eg.StopMacro()

def mail(
    sender,
    recipient,
    text,
    subject,
    smtp_address_com,
    user,
    password
):
    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, recipient, subject)
    message = headers + text
    mailServer = smtplib.SMTP(smtp_address_com)
    mailServer.login(user, password)
    mailServer.sendmail(sender, recipient, message)
    mailServer.quit()

def threadStart():
    thread_1.start()
    

# Here we go...
MainTask()
thread_1 = Thread( target=LiveCheck )
eg.scheduler.AddTask(2, threadStart)


Attachments
LiveCheckWithThread.xml
(2.38 KiB) Downloaded 204 times
Image2.jpg
Post Reply