-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivities.cs
65 lines (52 loc) · 2.3 KB
/
Activities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace TemporalioDebugActivity;
using Microsoft.Extensions.Logging;
using Temporalio.Activities;
using Temporalio.DebugActivity.Workflow.Models;
public class Activities
{
[Activity]
public Task<Distance> GetDistanceAsync(Address address)
{
var logger = ActivityExecutionContext.Current.Logger;
logger.LogInformation("GetDistance invoked; determining distance to customer address");
// this is a simulation, which calculates a fake (but consistent)
// distance for a customer address based on its length. The value
// will therefore be different when called with different addresses,
// but will be the same across all invocations with the same address.
var kilometers = address.Line1.Length + address.Line2.Length - 10;
if (kilometers < 1)
{
kilometers = 5;
}
var distance = new Distance(kilometers);
logger.LogDebug("GetDistance complete. Distance: {Distance}", distance.Kilometers);
return Task.FromResult(distance);
}
[Activity]
public Task<OrderConfirmation> SendBillAsync(Bill bill)
{
var logger = ActivityExecutionContext.Current.Logger;
logger.LogInformation("SendBill invoked. Customer: {Customer}, Amount: {Amount}", bill.CustomerId, bill.Amount);
var chargeAmount = bill.Amount;
// This month's special offer: Get $5 off all orders over $30
if (bill.Amount > 3000)
{
logger.LogInformation("Applying discount");
chargeAmount = -500; // reduce amount charged by 500 cents
}
// reject invalid amounts before calling the payment processor
if (chargeAmount < 0)
{
throw new ArgumentException($"Invalid charge amount: {chargeAmount} (must be above zero)");
}
// pretend we called a payment processing service here :-)
var confirmation = new OrderConfirmation(
OrderNumber: bill.OrderNumber,
Status: "SUCCESS",
ConfirmationNumber: "AB9923",
BillingTimestamp: DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
Amount: chargeAmount);
logger.LogDebug("SendBill complete. ConfirmationNumber: {Confirmation}", confirmation.ConfirmationNumber);
return Task.FromResult(confirmation);
}
}