Skip to content

Commit

Permalink
Merge pull request #1002 from stinovlas/997-fix-cli-defer-in-at
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Mar 20, 2024
2 parents 79e57ad + 7f1e3f6 commit 165c80e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion procrastinate/cli.py
Expand Up @@ -393,6 +393,7 @@ def configure_defer_parser(subparsers: argparse._SubParsersAction):
time_group,
"--at",
default=argparse.SUPPRESS,
dest="schedule_at",
type=utils.parse_datetime,
help="ISO-8601 localized datetime after which to launch the job",
envvar="DEFER_AT",
Expand All @@ -401,7 +402,7 @@ def configure_defer_parser(subparsers: argparse._SubParsersAction):
time_group,
"--in",
default=argparse.SUPPRESS,
dest="in_",
dest="schedule_in",
type=lambda s: {"seconds": int(s)},
help="Number of seconds after which to launch the job",
envvar="DEFER_IN",
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/test_cli.py
Expand Up @@ -2,6 +2,7 @@

import asyncio
import dataclasses
import datetime
import logging
import os

Expand Down Expand Up @@ -169,6 +170,67 @@ def mytask(a):
}


async def test_defer_at(entrypoint, cli_app, connector):
@cli_app.task(name="hello")
def mytask(a):
pass

# No space in the json helps entrypoint() to simply split args
result = await entrypoint(
"""defer --lock=sherlock --at=2020-01-01T12:00:00Z hello {"a":1}"""
)

assert "Launching a job: hello(a=1)\n" in result.stderr
assert result.exit_code == 0
assert connector.jobs == {
1: {
"args": {"a": 1},
"attempts": 0,
"id": 1,
"lock": "sherlock",
"queueing_lock": None,
"queue_name": "default",
"scheduled_at": datetime.datetime(
2020, 1, 1, 12, tzinfo=datetime.timezone.utc
),
"status": "todo",
"task_name": "hello",
}
}


async def test_defer_in(entrypoint, cli_app, connector):
@cli_app.task(name="hello")
def mytask(a):
pass

now = datetime.datetime.now(datetime.timezone.utc)

# No space in the json helps entrypoint() to simply split args
result = await entrypoint("""defer --lock=sherlock --in=10 hello {"a":1}""")

assert "Launching a job: hello(a=1)\n" in result.stderr
assert result.exit_code == 0
assert len(connector.jobs) == 1
job = connector.jobs[1]
scheduled_at = job.pop("scheduled_at")
assert job == {
"args": {"a": 1},
"attempts": 0,
"id": 1,
"lock": "sherlock",
"queueing_lock": None,
"queue_name": "default",
"status": "todo",
"task_name": "hello",
}
assert (
now + datetime.timedelta(seconds=9)
< scheduled_at
< now + datetime.timedelta(seconds=11)
)


async def test_defer_queueing_lock(entrypoint, cli_app, connector):
@cli_app.task(name="hello")
def mytask(a):
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_cli.py
Expand Up @@ -60,14 +60,16 @@ def test_main(mocker):
["defer", "x", "--at", "2023-01-01T00:00:00"],
{
"command": "defer",
"at": datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc),
"schedule_at": datetime.datetime(
2023, 1, 1, tzinfo=datetime.timezone.utc
),
},
),
(
["defer", "x", "--in", "3600"],
{
"command": "defer",
"in_": {"seconds": 3600},
"schedule_in": {"seconds": 3600},
},
),
(
Expand Down

0 comments on commit 165c80e

Please sign in to comment.