summaryrefslogtreecommitdiff
path: root/examples/persistent.py
blob: 1e77d1b6c8824bb507a22a511198a0379d95153a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
This example demonstrates the use of persistent job stores. On each run, it
adds a new alarm that fires after one minute. You can exit the program, restart
it and observe that any previous alarms that have not fired yet are still
active.
"""

from datetime import datetime, timedelta
import sys
import time

from apscheduler.scheduler import Scheduler
from apscheduler.jobstores.shelve_store import ShelveJobStore


def alarm(time):
    sys.stdout.write('Alarm! This alarm was scheduled at %s.\n' % time)


if __name__ == '__main__':
    scheduler = Scheduler()
    scheduler.add_jobstore(ShelveJobStore('example.db'), 'shelve')
    alarm_time = datetime.now() + timedelta(minutes=1)
    scheduler.add_date_job(alarm, alarm_time, name='alarm',
                           jobstore='shelve', args=[datetime.now()])
    sys.stdout.write('To clear the alarms, delete the example.db file.\n')
    sys.stdout.write('Press Ctrl+C to exit\n')
    scheduler.start()

    # This is here to prevent the main thread from exiting so that the
    # scheduler has time to work -- this is rarely necessary in real world
    # applications
    time.sleep(9999)