Possible Error

40 views
Skip to first unread message

Miguel Fonseca

unread,
Jun 28, 2021, 4:32:18 AM6/28/21
to APScheduler

I'm trying to implement a schedule that will run all day and have ON time and OFF time. So I wan't a method to be called with state 1 keeps in state 1 for a period and them call the same method with state 0 and keep for another period. So basically a Lamp will turn ON for 1 minute and them will be OFF for 4 minutes, always switching between states. I already try changing/modifying the job, with Interval and Cron, they all gave me same problems. So the problem is that it will run flawless for hours and them fail to fired the event once or twice. Sometimes it just don't trigger. The Log doesn't show nothing.

I made a simple snippet just for testing:

import time
import logging
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED
from datetime import datetime

LOG_LEVEL = logging.INFO
LOG_FILE = "/var/log/test.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(filename=LOG_FILE, format=LOG_FORMAT, level=LOG_LEVEL)

def rele(on, off, state):
    print(str(datetime.now()) + ': ' + str(state))
    state = str(1 - int(state))
    return on + ',' + off + ',' + state

def my_listener(event):
    if(event.exception):
        print('The job crashed: ' + event.job_id)
    else:
        splited = event.retval.split(',')
        jobid = event.job_id
        sched.remove_job(event.job_id)
        period = 1
        if(splited[2] == '0'):
            period = int(splited[0])
        else:
            period = int(splited[1])
        sched.add_job(rele, 'interval', minutes=period, args=[splited[0], splited[1], splited[2]], id=jobid, misfire_grace_time=None)

sched = BackgroundScheduler(daemon=True)
sched.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
sched.add_job(rele, 'interval', minutes=1, args=['1', '4', '1'], id='rele1', misfire_grace_time=None)
sched.start()
print(str(datetime.now()) + ": start")

while(True):
    time.sleep(.1)

And the Log is:

2021-06-23 13:01:51,094 INFO Running job "sensor (trigger: interval[0:01:00], next run at: 2021-06-23 13:02:51 BST)" (scheduled at 2021-06-23 13:01:51.091280+01:00)
2021-06-23 13:01:51,096 INFO Job "sensor (trigger: interval[0:01:00], next run at: 2021-06-23 13:02:51 BST)" executed successfully
2021-06-23 13:01:51,598 INFO Removed job rele1
2021-06-23 13:01:52,102 INFO Added job "sensor" to job store "default"
2021-06-23 13:05:52,102 INFO Running job "sensor (trigger: interval[0:04:00], next run at: 2021-06-23 13:05:52 BST)" (scheduled at 2021-06-23 13:05:52.100417+01:00)
2021-06-23 13:05:52,104 INFO Job "sensor (trigger: interval[0:04:00], next run at: 2021-06-23 13:05:52 BST)" executed successfully
2021-06-23 13:05:52,607 INFO Removed job rele1
2021-06-23 13:05:53,111 INFO Added job "sensor" to job store "default"
2021-06-23 13:09:52,102 INFO Running job "sensor (trigger: interval[0:04:00], next run at: 2021-06-23 13:09:52 BST)" (scheduled at 2021-06-23 13:09:52.100417+01:00)
2021-06-23 13:09:52,105 INFO Job "sensor (trigger: interval[0:04:00], next run at: 2021-06-23 13:13:52 BST)" executed successfully
2021-06-23 13:09:52,608 INFO Removed job rele1
2021-06-23 13:09:53,112 INFO Added job "sensor" to job store "default"
2021-06-23 13:10:53,113 INFO Running job "sensor (trigger: interval[0:01:00], next run at: 2021-06-23 13:11:53 BST)" (scheduled at 2021-06-23 13:10:53.110220+01:00)
2021-06-23 13:10:53,114 INFO Job "sensor (trigger: interval[0:01:00], next run at: 2021-06-23 13:11:53 BST)" executed successfully
2021-06-23 13:10:53,616 INFO Removed job rele1
2021-06-23 13:10:54,121 INFO Added job "sensor" to job store "default"

The result in Console for that log is:

2021-06-23 13:01:51.095290: 0
2021-06-23 13:05:52.103565: 1
2021-06-23 13:09:52.103524: 1
2021-06-23 13:10:53.114225: 0

An extended version of the output:

2021-06-23 23:31:40.113978: 0
2021-06-23 23:35:40.121087: 1
2021-06-23 23:36:40.127794: 0
2021-06-23 23:40:40.135061: 1
2021-06-23 23:44:40.134991: 1
2021-06-23 23:45:40.143204: 0
2021-06-23 23:49:40.149844: 1
2021-06-23 23:50:40.156796: 0
2021-06-23 23:54:40.163674: 1
2021-06-23 23:55:40.169375: 0
2021-06-23 23:59:40.177340: 1
2021-06-24 00:00:40.184118: 0

I'm running this is a Raspberry Pi 3 with Python 2.7.

Thank you in advance.

AF


Alex Grönholm

unread,
Jun 28, 2021, 5:22:54 AM6/28/21
to apsch...@googlegroups.com

Have you tried logging with the logging.DEBUG level?

I must say that constantly removing and adding jobs is not the intended usage pattern with APScheduler.

Why not have two jobs: one that turns on the lamp and one that turns it off, running on different schedules?

--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apscheduler/cd21dd41-ba4f-45e4-8f9c-dd58865c01cbn%40googlegroups.com.

Miguel Fonseca

unread,
Jun 28, 2021, 6:38:04 AM6/28/21
to APScheduler
Hi Alex,

Thank you for the answer.

I've triyed with the two jobs but how can I calculate the interval to each other? For example (1 minute ON and 4 minutes OFF) job1(ON) runs every 5 minutes and job2(OFF) runs every 6 minutes? This doesn't work. In the second iteration of Job2 it will be 2 minutes ON instead of 1 minute.

The conclution I reached is that I have to calculate each one using the time of the other but I can't get it right. I'm not very good in Math.

Can you help with the Math?

Thank you
Reply all
Reply to author
Forward
0 new messages