Skip to content

Commit

Permalink
add sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Nov 4, 2023
1 parent 96ec024 commit b856054
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions dev/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Inngest.Dev.EventFn2 do
end)
|> IO.inspect()

step.sleep(ctx, "sleep-test", "10s")

IO.inspect("Second log")

name =
Expand Down
2 changes: 1 addition & 1 deletion lib/inngest/function/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Inngest.Function.Context do
:attempt,
:run_id,
:stack,
:steps
steps: %{}
]

@type t() :: %__MODULE__{
Expand Down
25 changes: 20 additions & 5 deletions lib/inngest/step_tool.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Inngest.StepTool do
@moduledoc false

alias Inngest.Function.{UnhashedOp, GeneratorOpCode}
alias Inngest.Function.{Context, UnhashedOp, GeneratorOpCode}

@type id() :: binary()

@spec run(map(), id(), fun()) :: nil
def run(ctx, step_id, func) do
@spec run(Context.t(), id(), fun()) :: any()
def run(%{steps: steps} = _ctx, step_id, func) do
op = %UnhashedOp{name: step_id, op: "Step"}
hashed_id = UnhashedOp.hash(op)

# check for hash
case ctx |> Map.get(:steps, %{}) |> Map.get(hashed_id) do
case Map.get(steps, hashed_id) do
nil ->
# if not, execute function
result = func.()
Expand All @@ -31,7 +31,22 @@ defmodule Inngest.StepTool do
end
end

def sleep() do
@spec sleep(Context.t(), id(), binary()) :: nil
def sleep(%{steps: steps} = _ctx, step_id, duration) do
op = %UnhashedOp{name: step_id, op: "Sleep"}
hashed_id = UnhashedOp.hash(op)

if Map.has_key?(steps, hashed_id) do
nil
else
throw(%GeneratorOpCode{
id: hashed_id,
name: duration,
display_name: step_id,
op: op.op,
data: nil
})
end
end

def sleep_until() do
Expand Down

0 comments on commit b856054

Please sign in to comment.