Rescheduling commands
gornik opened this issue · 3 comments
It is currently not possible to schedule a command using schedule_uuid
that was previously cancelled:
iex> Scheduler.schedule_once(seat_id, %TimeoutSeatReservation{...}, ~N[2020-01-01 12:00:00])
:ok
iex> Scheduler.cancel_schedule(seat_id)
:ok
iex> Scheduler.schedule_once(seat_id, %TimeoutSeatReservation{...}, ~N[2020-01-01 14:00:00])
{:error, :already_scheduled}
This is because command handlers for scheduling are allowed only when the schedule aggregate instance does not exist yet:
def execute(%Schedule{schedule_uuid: nil} = schedule, %ScheduleOnce{} = once) do
...
end
def execute(%Schedule{}, %ScheduleOnce{}), do: {:error, :already_scheduled}
This means that if we need rescheduling, we need to generate a new random schedule_uuid
for each new schedule and track it client side. This makes it a bit more difficult and doesn't guarantee schedule uniqueness (in the example above that we have a single active schedule per single seat), e.g.:
iex> Scheduler.schedule_once(seat_id, %TimeoutSeatReservation{...}, ~N[2020-01-01 12:00:00])
:ok
iex> Scheduler.cancel_schedule(seat_id)
:ok
iex> Scheduler.schedule_once(seat_id, %TimeoutSeatReservation{...}, ~N[2020-01-01 14:00:00])
:ok
iex> Scheduler.schedule_once(seat_id, %TimeoutSeatReservation{...}, ~N[2020-01-01 16:00:00])
{:error, :already_scheduled}
I believe it might also be useful to have some kind of rescheduling of a command, something like:
Scheduler.reschedule(seat_id, ~N[2020-01-01 14:00:00])
As you've discovered the current behaviour is by design (a command or batch may only be scheduled once), but it could be changed. I'm happy to accept a PR to add a Scheduler.reschedule
function.
Thank you! I'll try to prepare a PR with support for rescheduling.
Regarding allowing a schedule to be scheduled again after cancellation: I think if we have rescheduling implemented, this would no longer be necessary, but still worth adding. Let me know what you prefer.
My preference would be to only allow rescheduling when the schedule is active.