Reference¶
pgcron
¶
Core interface for configuring pgcron jobs.
pgcron.Update
¶
Bases: Expression
An UPDATE statement to be executed by pgcron.
Examples:
Update all users over 100 years old:
Source code in pgcron/expressions.py
pgcron.crontab
¶
crontab(
minute: str = "*",
hour: str = "*",
day_of_month: str = "*",
month_of_year: str = "*",
day_of_week: str = "*",
)
pgcron.seconds
¶
seconds(n: int)
Bases: Schedule
Run a job every n seconds.
Source code in pgcron/schedule.py
pgcron.job
¶
job(
schedule: Schedule | str,
*,
name: str | None = None,
database: str | None = None
) -> Callable[[Callable[[], Expression]], Job]
Register a pgcron job that runs a SQL expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule
|
Schedule | str
|
The schedule object or crontab expression for the job. |
required |
name
|
str | None
|
The name of the job, if None, the name will be the function name. |
None
|
database
|
str | None
|
The database to run the job on.
Defaults to the database set in |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[], Expression]], Job]
|
A decorator that registers the job with pgcron, and returns a |
Callable[[Callable[[], Expression]], Job]
|
that can be used to run the job synchronously if needed. |
Examples:
Job that deletes users over 100 years old every minute:
import pgcron
@pgcron.job("* * * * *")
def delete_old_users() -> pgcron.SQLExpression:
return pgcron.SQLExpression("DELETE FROM users WHERE age > 100")
Job that vacuums the database every day at 9am:
import pgcron
@pgcron.job("0 9 * * *")
def vacuum_db() -> pgcron.SQLExpression:
return pgcron.SQLExpression("VACUUM")
Job that calls a stored procedure every 5 seconds:
import pgcron
@pgcron.job(pgcron.seconds(5))
def call_stored_procedure() -> pgcron.SQLExpression:
return pgcron.SQLExpression("CALL my_stored_procedure()")