Python - schedule()
Creates a new Schedule to run a function on a defined frequency.
from nitric.resources import schedulefrom nitric.application import Nitricfrom nitric.context import IntervalContextreport_schedule = schedule('run-a-report')@report_schedule.every('1 days')async def process_transactions(ctx: IntervalContext):# do some processingpassNitric.run()
Parameters
- Name
description- Required
- Required
- Type
- string
- Description
The unique name of this Schedule within the app. Subsequent calls to
schedulewith the same name will return the same object.
- Name
every- Required
- Required
- Type
- string
- Description
The rate description for the schedule. Supported frequencies include
seconds,minutes,hoursanddays. Usingeveryas a keyword argument can help with readability of schedules, e.g.backup = schedule("backup")@backup.every("2 days")
Examples:
| description | example schedule |
|---|---|
| Every day | @schedule("work").every("day") |
| Every 14 hours | @schedule("work").every("14 hours") |
| Every 30 minutes | @schedule("work").every("30 minutes") |
| Every day (cron) | @schedule("work").cron("0 0 * * *") |
Singular rates will be automatically converted. e.g. "day" will be interpreted as "1 days".
Notes
-
Schedules do not require access permissions to be specified.
-
Local execution and testing can be done with the local dashboard.
Examples
Create a Schedule
from nitric.resources import schedulefrom nitric.application import Nitricfrom nitric.context import IntervalContextprocessor = schedule("processor")@processor.every('5 minutes')async def process_transactions(ctx: IntervalContext):# do some processingpass@processor.every('3 hours')async def process_transactions(ctx: IntervalContext):# do some processingpass@processor.every('1 days')async def process_transactions(ctx: IntervalContext):# do some processingpass@processor.cron('0 0 * * *')async def process_transactions(ctx: IntervalContext)# do some processingpassNitric.run()
Last updated on Oct 16, 2024