Temporal Schedule

I am trying to use Schedule by calling a func that creates a schedule inside another WF.
But getting NotImplemented error.

@workflow.defn
class TestWorkflow:
    @workflow.run
    async def run(self, params: dict[str, str]):
        handle = await create_test_schedule("test")

async def create_test_schedule(text: str) -> ScheduleHandle:
     // BREAKS HERE WHEN TRYING TO CONNECT HOST IS VALID
    client = await Client.connect("my_temporal_host")

    handle = await client.create_schedule(
        "workflow-schedule-id",
        Schedule(
            action=ScheduleActionStartWorkflow(
                MyWF.run,
                text,
                id="test",
                task_queue="queue",
            ),
            spec=ScheduleSpec(
                start_at=datetime.now() + timedelta(hours=48)
            ),
            state=ScheduleState(
                note="note"
            ),
        ),
    )

    return handle

For some reason I can’t get a client connection inside WF, seems like I am doing something wrong but can’t get what.

You cannot use a Temporal client from inside a workflow. You can’t use any network calls from a workflow or make any calls to anything external in any way. Workflows must be deterministic.

Yeah, true. But how can I achieve this flow:
I need to call from a workflow another wf/activity that will run after 48h.
I used sleep in activity but seems like Temporal thinks that activity died without a heartbeat.
So I am kindly asking about the proper way to implement delayed execution of workflow/activity called inside another workflow.

You can sleep inside the workflow before starting an activity or child workflow.

The idea is to have it on a background. If parent WF finished execution I need to cancel this activity/wf.
It’s like I have a happy case when parent wf finishes in 2 days, so I can cancel. If not this needs to be executed.

There is also a hacky way of wrapping schedule code in activity + import passed through.
But what is a proper way?

Not following exactly, but this is how parent/child works by default. When parent cancels/completes, child/activity cancels/completes, but it is configurable.

Yes, you can run a workflow or a schedule or anything you with a Temporal client from inside an activity

If you want to do start an activity after a certain amount of time, you sleep in the workflow before starting the activity. Same for child workflows.

Can the calling workflow remain open while this call is made, or do you want to complete the parent but still schedule this one? If you want the parent to complete without affecting this one, you can start a child workflow with parent close policy as abandon that sleeps 48h as its first step and then does work.

Thanks!

One more thing why it’s not triggered when I set start time.

async def create_test_schedule(text: str) -> ScheduleHandle:
     // BREAKS HERE WHEN TRYING TO CONNECT HOST IS VALID
    client = await Client.connect("my_temporal_host")

    handle = await client.create_schedule(
        "workflow-schedule-id",
        Schedule(
            action=ScheduleActionStartWorkflow(
                MyWF.run,
                text,
                id="test",
                task_queue="queue",
            ),
            spec=ScheduleSpec(
                start_at=datetime.now() + timedelta(seconds=30)
            ),
            state=ScheduleState(
                note="note"
            ),
        ),
    )

    return handle

So you’re having trouble connecting a client or you can connect a client and create the schedule but are not seeing it occur. If you are having general client connection trouble, can you start a new thread? If you are having schedule trouble, look in the UI (or describe programmatically) and see when the next time to run is or if it ran.

I see that schedule is running in the UI.
and here is MarkerRecorder:

{
  "data": {
    "payloads": [
      {
        "2023-06-23T15:15:22.945770677Z": {
          "Nominal": "0001-01-01T00:00:00Z",
          "Next": "0001-01-01T00:00:00Z"
        }
      }
    ]
  },
  "side-effect-id": {
    "payloads": [
      1
    ]
  }
}

Start Time:
2023-06-23 UTC 15:15:22.93

Now is 55min and it still did not fired the wf.

ScheduleSpec.start_at is not the start time, it is the time which nothing will start before. You still have to provide calendars or intervals. See the create schedule dev guide and the ScheduleSpec API doc.