Reputation: 21
I am getting import error while I am trying to run my code. Scheduler module is not found. when I tried bypassing the error by using backgroundScheduler()
, I found it doesn't support cron-like job scheduling i.e add_cron_job
. Where am I going wrong ?
import ConfigParser
import pymysql # Allows us to connect to a database and issue queries
from time import sleep
from datetime import datetime
import threading, time
import json
from apscheduler.scheduler import Scheduler
import os
import logging
import smtplib # Import smtplib for the actual sending function
logging.basicConfig()
config = None
sched = Scheduler()
sched.start()
#----------------------------------------------------------------------------
# init_config_parser
# Initialize a config parser based on filename
#----------------------------------------------------------------------------
def init_config_parser():
global config
config = ConfigParser.SafeConfigParser()
config.readfp(open('settings.cfg'))
return config
# This is called when the runReportsScheduler.py file is first run.
# There is an infinite loop such that the script will never terminate.
# Every x number of seconds, the script reloads the latest
# notification schedules from the database. These notification schedules
# can be changed from the webapp in the configuration page.
# Then, it reschedules all of the email notifications accordingly.
# The scheduler calls the function email_send everytime it should be fired.
def email_send(emails):
From = '[email protected]'
To = emails
message = """Please ignore this email.
This is a test e-mail message.
"""
try :
smtpObj = smtplib.SMTP('smtphost.qualcomm.com')
smtpObj.sendmail(From, To.split(','), message) # the second parameter expects a list for multiple contacts
smtpObj.quit()
except SMTPException:
print "Error: unable to send email"
if __name__ == '__main__':
init_config_parser()
updateInterval = float(config.get("SCHEDULER",'updateInterval'))
thisURL = str(config.get("SCHEDULER",'rootURL'))
#refreshingFunction(50687,False)
i = 0
while True:
i += 1
print "This is the #"+str(i)+" time I am looping, on a "+str(updateInterval)+"s interval"
conn = pymysql.connect(host="10.52.244.877", user="wciadmin", passwd="admybutt", db="weekly_reports")
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute("SELECT * FROM reminders_table ORDER BY id DESC")
currentJobs = sched.get_jobs()
row = cur.fetchone()
while row is not None:
# this is for each row in the DB
thisQuery = row['id']
# delete any current jobs with that query name
for job in currentJobs:
if int(job.name) == int(thisQuery):
sched.unschedule_job(job)
thisRuntimes = row['runtimes'].split(";")
if len(thisRuntimes) > 0 and row['runtimes'] != "":
for thisSchedule in thisRuntimes:
daysOfWeek = thisSchedule.split("-")[0]
times = thisSchedule.split("-")[1].split(",")
for time in times:
hour = time.split(":")[0]
if hour[0] == "0":
hour = hour[1]
min = time.split(":")[1]
sched.add_cron_job(email_send, day_of_week=daysOfWeek, hour=hour, minute=min, args=[row['emails']])
row = cur.fetchone()
conn.close()
sleep(updateInterval) #in seconds
Upvotes: 0
Views: 7180
Reputation: 21
Got it. Scheduler() has been deprecated in apscheduler v3.0. I downloaded version 2.1.2 and it solved my problem. the link to v2.1.2 is below :
https://pypi.python.org/pypi/APScheduler/2.1.2#downloads
Upvotes: 2