Dear community, How can I pass `cron = CronSchedu...
# ask-community
v
Dear community, How can I pass
cron = CronSchedule(cron="8 9 * * *", timezone="Europe/Berlin")
into the following construction?
Copy code
flow.from_source(
    source=gitlab_repo,
    entrypoint=entrypoint,
).deploy(
    name=deployment_name,
    work_pool_name=work_pool_name,
    job_variables=job_variables,
)
The
deploy()
method expects either
cron: Optional[str]
or
schedules: Optional[list[DeploymentScheduleCreate]]
. I couldn't make it work with either of them. Prefect 3.1. Any advice would be appreciated.
b
Hi Vugar, can you give this a try?
Copy code
from prefect.schedules import CronSchedule
...

if __name__ == "__main__":
    flow.from_source(
    source=gitlab_repo,
    entrypoint=entrypoint,
).deploy(
        name="my-deployment",
        work_pool_name="my-work-pool",
        schedules=CronSchedule(cron="8 9 * * *", timezone="Europe/Berlin")
    )
v
@Bianca Hoch
Copy code
File "..../orchestrator/lib/deploy.py", line 64, in deploy_all_deployments
    ).deploy(
      ^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 353, in coroutine_wrapper
    return run_coro_as_sync(ctx_call())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 209, in run_coro_as_sync
    return call.result()
           ^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 330, in result
    return self.future.result(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 193, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 403, in _run_async
    result = await coro
             ^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 190, in coroutine_wrapper
    return await task
           ^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 343, in ctx_call
    result = await async_fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/flows.py", line 1479, in deploy
    deployment = await to_deployment_coro
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/flows.py", line 743, in ato_deployment
    return await RunnerDeployment.afrom_storage(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/deployments/runner.py", line 806, in afrom_storage
    deployment = cls(
                 ^^^^
  File "...venv/lib/python3.12/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/deployments/runner.py", line 256, in reconcile_schedules
    return reconcile_schedules_runner(values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/_internal/schemas/validators.py", line 194, in reconcile_schedules_runner
    if schedules is not None and len(schedules) > 0:
                                 ^^^^^^^^^^^^^^
TypeError: object of type 'CronSchedule' has no len()
schedules
needs list
If I try
Copy code
from prefect.schedules import CronSchedule
...

if __name__ == "__main__":
    flow.from_source(
    source=gitlab_repo,
    entrypoint=entrypoint,
).deploy(
        name="my-deployment",
        work_pool_name="my-work-pool",
        schedules=[CronSchedule(cron="8 9 * * *", timezone="Europe/Berlin")]
    )
then I get:
Copy code
File ".../orchestrator/lib/deploy.py", line 65, in deploy_all_deployments
    ).deploy(
      ^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 353, in coroutine_wrapper
    return run_coro_as_sync(ctx_call())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 209, in run_coro_as_sync
    return call.result()
           ^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 330, in result
    return self.future.result(timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 193, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "...venv/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line 403, in _run_async
    result = await coro
             ^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 190, in coroutine_wrapper
    return await task
           ^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/utilities/asyncutils.py", line 343, in ctx_call
    result = await async_fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/flows.py", line 1479, in deploy
    deployment = await to_deployment_coro
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/flows.py", line 743, in ato_deployment
    return await RunnerDeployment.afrom_storage(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...venv/lib/python3.12/site-packages/prefect/deployments/runner.py", line 806, in afrom_storage
    deployment = cls(
                 ^^^^
  File "...venv/lib/python3.12/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for RunnerDeployment
  Value error, Server schema schedules are not supported. Please use the schedule objects from `prefect.client.schemas.schedules` [type=value_error, input_value={'name': 'SnowflakeExport...ne, 'job_variables': {}}, input_type=dict]
    For further information visit <https://errors.pydantic.dev/2.10/v/value_error>
In 2 words: All I need is to add timezone to cron string. I can do it in UI, but couldn't in python code.
b
Ahhh I see. Apologies,
schedules
does require a list. The version you're using will influence how you pass in the schedule. For prefect versions < 3.1.16:
Copy code
schedules=[CronSchedule(cron="8 9 * * *", timezone="Europe/Berlin")]
And then for versions >= 3.1.16:
Copy code
schedule=CronSchedule(
    cron="8 9 * * *",
    timezone="Europe/Berlin"
  )
v
Thanks! I will check within an hour.
b
Here's the portion of the documentation that covers this detail ^
Sounds good!
v
The documentation didn’t help, but your suggestion to check the version led me to upgrade from 3.1.15 to 3.2.7. After reading the source code, I tried the following approach:
Copy code
from prefect.schedules import Cron

flow.from_source(
    source=gitlab_repo,
    entrypoint=entrypoint,
).deploy(
    name=name,
    ....
    schedule=Cron(cron="8 9 * * *", timezone="Europe/Berlin"),
    ....
)
And it was successful. Thanks for your help!
🙌 1
prefect build 1