diff --git a/README.md b/README.md index 57823a8..2f6e0cd 100644 --- a/README.md +++ b/README.md @@ -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") ``` @@ -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")) ``` @@ -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');") ``` diff --git a/docs/index.md b/docs/index.md index 0506336..c0518e3 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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") ``` @@ -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")) ``` @@ -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');") ``` diff --git a/docs/usage.md b/docs/usage.md index c62957f..da991a7 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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');") ``` @@ -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") ``` @@ -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")) ``` @@ -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');") ``` @@ -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');") ``` diff --git a/pgcron/core.py b/pgcron/core.py index 8106ccc..333b783 100644 --- a/pgcron/core.py +++ b/pgcron/core.py @@ -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 @@ -17,7 +18,7 @@ class Job: """A registered pgcron job.""" name: str - schedule: schedule.Schedule + schedule: pgcron.schedule.Schedule expression: expressions.Expression database: str @@ -31,7 +32,7 @@ def run(self) -> None: def job( - schedule: schedule.Schedule, + schedule: pgcron.schedule.Schedule | str, *, name: str | None = None, database: str | None = None, @@ -39,7 +40,7 @@ def 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, @@ -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") ``` @@ -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") ``` @@ -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() diff --git a/pgcron/schedule.py b/pgcron/schedule.py index a8ebbd9..32b3dfc 100644 --- a/pgcron/schedule.py +++ b/pgcron/schedule.py @@ -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):