Skip to content

Commit

Permalink
Allow direct crontab
Browse files Browse the repository at this point in the history
  • Loading branch information
max-muoto committed Dec 27, 2024
1 parent 6510488 commit 1bdde4a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ A `pgcron.Update` is an update statement to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Update(NameTestModel.objects.all().filter(name="test"), name="test2")
```
Expand All @@ -57,7 +57,7 @@ A `pgcron.Delete` is a delete statement to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Delete(NameTestModel.objects.all().filter(name="test"))
```
Expand All @@ -69,7 +69,7 @@ A `pgcron.SQLExpression` is a simple SQL expression to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")
```
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ A `pgcron.Update` is an update statement to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Update(NameTestModel.objects.all().filter(name="test"), name="test2")
```
Expand All @@ -57,7 +57,7 @@ A `pgcron.Delete` is a delete statement to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Delete(NameTestModel.objects.all().filter(name="test"))
```
Expand All @@ -69,7 +69,7 @@ A `pgcron.SQLExpression` is a simple SQL expression to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")
```
Expand Down
10 changes: 5 additions & 5 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A `pgcron.SQLExpression` is a simple SQL expression to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")
```
Expand All @@ -32,7 +32,7 @@ A `pgcron.Update` is an update statement to be executed by pgcron, with the abil
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Update(NameTestModel.objects.all().filter(name="test"), name="test2")
```
Expand All @@ -45,7 +45,7 @@ A `pgcron.Delete` is a delete statement to be executed by pgcron.
```python
import pgcron

@pgcron.job(pgcron.crontab("0", "0", "1", "1", "*"))
@pgcron.job("0 0 1 1 *")
def my_job():
return pgcron.Delete(NameTestModel.objects.all().filter(name="test"))
```
Expand All @@ -71,7 +71,7 @@ An example of a job that runs every minute, defined using a crontab expression:
```python
import pgcron

@pgcron.job(pgcron.crontab("* * *"))
@pgcron.job("* * * * *")
def my_job():
return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")
```
Expand All @@ -88,7 +88,7 @@ An example of a job that runs every 5 seconds:
```python
import pgcron

@pgcron.job(pgcron.seconds(5))
@pgcron.job("*/5 * * * *")
def my_job():
return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")
```
Expand Down
16 changes: 10 additions & 6 deletions pgcron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from django.db import connections

from pgcron import _config, _registry, expressions, schedule
import pgcron.schedule
from pgcron import _config, _registry, expressions

if TYPE_CHECKING:
from collections.abc import Callable
Expand All @@ -17,7 +18,7 @@ class Job:
"""A registered pgcron job."""

name: str
schedule: schedule.Schedule
schedule: pgcron.schedule.Schedule
expression: expressions.Expression
database: str

Expand All @@ -31,15 +32,15 @@ def run(self) -> None:


def job(
schedule: schedule.Schedule,
schedule: pgcron.schedule.Schedule | str,
*,
name: str | None = None,
database: str | None = None,
) -> Callable[[Callable[[], expressions.Expression]], Job]:
"""Register a pgcron job that runs a SQL expression.
Args:
schedule: The schedule for the job.
schedule: The schedule object or crontab expression for the job.
name: The name of the job, if None, the name will be the function name.
database: The database to run the job on.
Defaults to the database set in `PGCRON_DATABASE` in settings,
Expand All @@ -55,7 +56,7 @@ def job(
import pgcron
@pgcron.job(pgcron.crontab("*", "*", "*", "*", "*"))
@pgcron.job("* * * * *")
def delete_old_users() -> pgcron.SQLExpression:
return pgcron.SQLExpression("DELETE FROM users WHERE age > 100")
```
Expand All @@ -65,7 +66,7 @@ def delete_old_users() -> pgcron.SQLExpression:
import pgcron
@pgcron.job(pgcron.crontab("0", "9", "*", "*", "*"))
@pgcron.job("0 9 * * *")
def vacuum_db() -> pgcron.SQLExpression:
return pgcron.SQLExpression("VACUUM")
```
Expand All @@ -81,6 +82,9 @@ def call_stored_procedure() -> pgcron.SQLExpression:
```
"""

if isinstance(schedule, str):
schedule = pgcron.schedule.crontab.from_str(schedule)

def decorator(func: Callable[[], expressions.Expression]) -> Job:
app_label = func.__module__.split(".")[0]
sql = func()
Expand Down
6 changes: 6 additions & 0 deletions pgcron/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def to_pgcron_expr(self) -> str:
f"{self.minute} {self.hour} {self.day_of_month} {self.month_of_year} {self.day_of_week}"
)

@classmethod
def from_str(cls, expr: str) -> crontab:
"""Create a crontab from a string."""
minute, hour, day_of_month, month_of_year, day_of_week = expr.split(" ")
return cls(minute, hour, day_of_month, month_of_year, day_of_week)


@final
class seconds(Schedule):
Expand Down

0 comments on commit 1bdde4a

Please sign in to comment.