Skip to content

Commit eb6ee2e

Browse files
Merge pull request #212 from amulyavarote/feature/actors_reminder_ttl
Added TTL param to actor timer and reminder
2 parents 7d9298a + 3f08897 commit eb6ee2e

File tree

13 files changed

+203
-9
lines changed

13 files changed

+203
-9
lines changed

daprdocs/content/en/js-sdk-docs/js-actors/_index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ async function start()
158158
await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
159159
callback: "method-to-excute-on-actor",
160160
dueTime: Temporal.Duration.from({ seconds: 2 }),
161-
period: Temporal.Duration.from({ seconds: 1 })
161+
period: Temporal.Duration.from({ seconds: 1 }),
162+
ttl: Temporal.Duration.from({ seconds: 1 }),
162163
});
163164

164165
// Delete the timer
@@ -184,6 +185,7 @@ async function start()
184185
await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
185186
dueTime: Temporal.Duration.from({ seconds: 2 }),
186187
period: Temporal.Duration.from({ seconds: 1 }),
188+
ttl: Temporal.Duration.from({ seconds: 1 }),
187189
data: 100
188190
});
189191

src/actors/client/ActorClient/ActorClientGRPC.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ export default class ActorClientGRPC implements IClientActor {
137137
msgService.setDueTime(reminder.dueTime.toString());
138138
}
139139

140+
if (reminder.ttl) {
141+
msgService.setTtl(reminder.ttl.toString());
142+
}
143+
140144
return new Promise((resolve, reject) => {
141145
const client = this.client.getClient();
142146
client.registerActorReminder(msgService, (err, _res) => {
@@ -191,6 +195,10 @@ export default class ActorClientGRPC implements IClientActor {
191195
msgService.setDueTime(timer.dueTime.toString());
192196
}
193197

198+
if (timer.ttl) {
199+
msgService.setTtl(timer.ttl.toString());
200+
}
201+
194202
return new Promise((resolve, reject) => {
195203
const client = this.client.getClient();
196204
client.registerActorTimer(msgService, (err, _res) => {

src/actors/client/ActorClient/ActorClientHTTP.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class ActorClientHTTP implements IClientActor {
6060
body: JSON.stringify({
6161
period: reminder.period.toString().toLocaleLowerCase().replace('pt', ''),
6262
dueTime: reminder?.dueTime?.toString()?.toLocaleLowerCase().replace('pt', ''),
63+
ttl: reminder?.ttl?.toString()?.toLocaleLowerCase().replace('pt', ''),
6364
data: reminder.data
6465
}),
6566
});
@@ -85,6 +86,7 @@ export default class ActorClientHTTP implements IClientActor {
8586
body: JSON.stringify({
8687
period: timer.period.toString().toLocaleLowerCase().replace('pt', ''),
8788
dueTime: timer?.dueTime?.toString()?.toLocaleLowerCase().replace('pt', ''),
89+
ttl: timer?.ttl?.toString()?.toLocaleLowerCase().replace('pt', ''),
8890
data: timer.data,
8991
callback: timer.callback
9092
}),

src/actors/runtime/AbstractActor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ export default abstract class AbstractActor {
8080
* @param reminderName name of the reminder
8181
* @param state the state to be send along with the reminder trigger
8282
* @param dueTime due time for the first trigger
83+
* @param ttl time to duration after which the reminder will be expired and deleted
8384
* @param period frequency for the triggers
8485
* @param <Type> Type of the state object
8586
* @return Async void response
8687
*/
87-
async registerActorReminder<_Type>(reminderName: string, dueTime: Temporal.Duration, period: Temporal.Duration, state?: any) {
88+
async registerActorReminder<_Type>(reminderName: string, dueTime: Temporal.Duration, period: Temporal.Duration, ttl?: Temporal.Duration, state?: any) {
8889
await this.actorClient.actor.registerActorReminder(this.actorType, this.id, reminderName, {
8990
period,
9091
dueTime,
92+
ttl,
9193
data: state
9294
});
9395
}
@@ -96,11 +98,12 @@ export default abstract class AbstractActor {
9698
await this.actorClient.actor.unregisterActorReminder(this.actorType, this.id, reminderName);
9799
}
98100

99-
async registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period: Temporal.Duration, state?: any) {
101+
async registerActorTimer(timerName: string, callback: string, dueTime: Temporal.Duration, period: Temporal.Duration, ttl?: Temporal.Duration, state?: any) {
100102
// Register the timer in the sidecar
101103
return await this.actorClient.actor.registerActorTimer(this.actorType, this.id, timerName, {
102104
period,
103105
dueTime,
106+
ttl,
104107
data: state,
105108
callback
106109
});

src/actors/runtime/ActorReminderData.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ export default class ActorReminderData {
2020
readonly reminderName: string;
2121
readonly state: string | object | undefined;
2222
readonly dueTime: number;
23+
readonly ttl: number | undefined;
2324
readonly period: number;
2425

2526
/**
2627
* @param reminderName the name of the actor reminder
2728
* @param state the state data passed to receiveReminder callback
2829
* @param dueTime the amount of time to delay before invoking the reminder for the first time
30+
* @param ttl time to duration after which the reminder will be expired and deleted
2931
* @param period the time interval between reminder invocations after the first invocation
3032
*/
31-
constructor(reminderName: string, dueTime: number, period: number, state?: string | object) {
33+
constructor(reminderName: string, dueTime: number, period: number, ttl?: number, state?: string | object) {
3234
this.reminderName = reminderName;
3335
this.dueTime = dueTime;
36+
this.ttl = ttl;
3437
this.period = period;
3538
this.state = state;
3639
}
@@ -47,6 +50,10 @@ export default class ActorReminderData {
4750
return this.dueTime;
4851
}
4952

53+
getTtl(): number | undefined {
54+
return this.ttl;
55+
}
56+
5057
getPeriod(): number {
5158
return this.period;
5259
}
@@ -58,6 +65,7 @@ export default class ActorReminderData {
5865
return {
5966
reminderName: this.reminderName,
6067
dueTime: this.dueTime,
68+
ttl: this.ttl,
6169
period: this.period,
6270
data: this.state
6371
}
@@ -68,10 +76,11 @@ export default class ActorReminderData {
6876

6977
const data = obj?.data;
7078
const dueTime = obj?.dueTime;
79+
const ttl = obj?.ttl;
7180
const period = obj?.period;
7281

7382
const deserializedData = serializer.deserialize(data);
7483

75-
return new ActorReminderData(reminderName, dueTime, period, deserializedData);
84+
return new ActorReminderData(reminderName, dueTime, ttl, period, deserializedData);
7685
}
7786
}

src/types/ActorReminder.type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export type ActorReminderType = {
1717
period: Temporal.Duration; // e.g. 0h0m9s0ms
1818
dueTime?: Temporal.Duration; // e.g. 1m or 0h0m0s0ms defaults to 0s
1919
data?: any; // the data to pass
20+
ttl?: Temporal.Duration; // e.g. 1m
2021
}

src/types/ActorTimer.type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export type ActorTimerType = {
1717
period: Temporal.Duration; // e.g. 0h0m9s0ms
1818
dueTime?: Temporal.Duration; // e.g. 1m or 0h0m0s0ms defaults to 0s
1919
data?: any; // the data to pass
20+
ttl?: Temporal.Duration; // e.g. 1m
2021
callback: string; // which method to execute as callback method
2122
}

test/actor/DemoActorReminder2Impl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default class DemoActorReminder2Impl extends AbstractActor implements Dem
1818
counter = 0;
1919

2020
async init(): Promise<string> {
21-
await super.registerActorReminder("my-reminder-name", Temporal.Duration.from({ seconds: 2 }), Temporal.Duration.from({ seconds: 1 }), 123);
21+
await super.registerActorReminder("my-reminder-name", Temporal.Duration.from({ seconds: 2 }), Temporal.Duration.from({ seconds: 2 }),
22+
Temporal.Duration.from({ seconds: 1 }), 123);
2223
return "Actor Initialized";
2324
}
2425

test/actor/DemoActorReminderImpl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default class DemoActorReminderImpl extends AbstractActor implements Demo
1818
counter = 0;
1919

2020
async init(): Promise<string> {
21-
await super.registerActorReminder("my-reminder-name", Temporal.Duration.from({ seconds: 2 }), Temporal.Duration.from({ seconds: 1 }), 123);
21+
await super.registerActorReminder("my-reminder-name", Temporal.Duration.from({ seconds: 2 }), Temporal.Duration.from({ seconds: 3 }),
22+
undefined, 123);
2223
return "Actor Initialized";
2324
}
2425

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2022 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { AbstractActor, Temporal } from "../../src";
15+
import DemoActorReminderInterface from "./DemoActorReminderInterface";
16+
17+
export default class DemoActorReminderTtlImpl extends AbstractActor implements DemoActorReminderInterface {
18+
counter = 0;
19+
20+
async init(): Promise<string> {
21+
await super.registerActorReminder("my-reminder-name",
22+
Temporal.Duration.from({ seconds: 2 }), //dueTime
23+
Temporal.Duration.from({ seconds: 2 }), //period
24+
Temporal.Duration.from({ seconds: 1 }), //ttl
25+
123);
26+
return "Actor Initialized";
27+
}
28+
29+
async count(): Promise<void> {
30+
this.counter++;
31+
}
32+
33+
async getCounter(): Promise<number> {
34+
return this.counter;
35+
}
36+
37+
async removeReminder(): Promise<void> {
38+
return this.unregisterActorReminder("my-reminder-name");
39+
}
40+
41+
/**
42+
* @override
43+
* @param data
44+
*/
45+
async receiveReminder(data: string): Promise<void> {
46+
this.counter += parseInt(data);
47+
}
48+
}

0 commit comments

Comments
 (0)