-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Shared handlers simplifying a bit the demo
Signed-off-by: slinkydeveloper <[email protected]>
- Loading branch information
1 parent
2c90484
commit 933633d
Showing
11 changed files
with
218 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 0 additions & 141 deletions
141
...food-ordering/app/restate-app/src/main/kotlin/dev/restate/sdk/examples/DeliveryManager.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...food-ordering/app/restate-app/src/main/kotlin/dev/restate/sdk/examples/OrderETAService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH | ||
* | ||
* This file is part of the Restate examples, | ||
* which is released under the MIT license. | ||
* | ||
* You can find a copy of the license in the file LICENSE | ||
* in the root directory of this repository or package or at | ||
* https://github.com/restatedev/examples/ | ||
*/ | ||
package dev.restate.sdk.examples | ||
|
||
import dev.restate.sdk.annotation.Handler | ||
import dev.restate.sdk.annotation.Shared | ||
import dev.restate.sdk.annotation.VirtualObject | ||
import dev.restate.sdk.common.TerminalException | ||
import dev.restate.sdk.examples.utils.GeoUtils | ||
import dev.restate.sdk.kotlin.KtStateKey | ||
import dev.restate.sdk.kotlin.ObjectContext | ||
import dev.restate.sdk.kotlin.SharedObjectContext | ||
|
||
/** Virtual object tracking the ETA, keyed by order-id */ | ||
@VirtualObject | ||
class OrderETAService { | ||
companion object { | ||
private val ORDER_ETA = KtStateKey.json<Long>("order-eta") | ||
private val DELIVERY_LOCATIONS = KtStateKey.json<DeliveryLocations>("delivery-locations") | ||
private val DELIVERY_IS_PICKED_UP = KtStateKey.json<Unit>("order-is-picked-up") | ||
} | ||
|
||
/** Gets called by the webUI frontend to display the status of an order. */ | ||
@Shared | ||
suspend fun get(ctx: SharedObjectContext): Long { | ||
return ctx.get(ORDER_ETA) ?: -1L | ||
} | ||
|
||
@Handler | ||
suspend fun notifyDeliveryLocations(ctx: ObjectContext, locations: DeliveryLocations) { | ||
ctx.set(DELIVERY_LOCATIONS, locations) | ||
} | ||
|
||
/** | ||
* Updates the location of the order. Gets called by | ||
* DriverService.HandleDriverLocationUpdateEvent() (digital twin of the driver) when the driver | ||
* has moved to a new location. | ||
*/ | ||
@Handler | ||
suspend fun notifyDeliveryPickup(ctx: ObjectContext) { | ||
ctx.set(DELIVERY_IS_PICKED_UP, Unit) | ||
} | ||
|
||
/** | ||
* Updates the location of the order. Gets called by | ||
* DriverService.HandleDriverLocationUpdateEvent() (digital twin of the driver) when the driver | ||
* has moved to a new location. | ||
*/ | ||
@Handler | ||
suspend fun notifyDriverLocationUpdate(ctx: ObjectContext, newLocation: Location) { | ||
// Retrieve the delivery information for this delivery | ||
val locations = | ||
ctx.get(DELIVERY_LOCATIONS) | ||
?: throw TerminalException( | ||
"Driver is doing a delivery but there is no ongoing delivery.") | ||
|
||
val isOrderPickedUp = ctx.get(DELIVERY_IS_PICKED_UP) != null | ||
|
||
// Parse the new location, and calculate the ETA of the delivery to the customer | ||
val eta = | ||
if (isOrderPickedUp) GeoUtils.calculateEtaMillis(newLocation, locations.customer) | ||
else | ||
(GeoUtils.calculateEtaMillis(newLocation, locations.restaurant) + | ||
GeoUtils.calculateEtaMillis(locations.restaurant, locations.customer)) | ||
|
||
// Update the ETA of the order | ||
ctx.set(ORDER_ETA, eta) | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
...d-ordering/app/restate-app/src/main/kotlin/dev/restate/sdk/examples/OrderStatusService.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.