File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,25 @@ export class TinyDO extends WsServerDurableObject<Env> {
40
40
}
41
41
42
42
async alarm ( alarmInfo ?: AlarmInvocationInfo ) {
43
- this . ctx . storage . setAlarm ( Date . now ( ) + this . currentInterval )
43
+ const currentHour = new Date ( ) . getUTCHours ( )
44
+ let nextAlarm = Date . now ( ) + this . currentInterval
45
+
46
+ // If it's no longer daytime, set the alarm for 06:00 UTC
47
+ if ( currentHour < 6 || currentHour > 21 ) {
48
+ const nextAlarmDate = new Date ( )
49
+ nextAlarmDate . setUTCHours ( 6 , 0 , 0 , 0 )
50
+ nextAlarm = nextAlarmDate . getTime ( )
51
+
52
+ if ( currentHour > 21 ) {
53
+ console . log ( "It's too late in the evening. Setting alarm for tomorrow" , nextAlarmDate . toISOString ( ) )
54
+ nextAlarmDate . setUTCDate ( nextAlarmDate . getUTCDate ( ) + 1 )
55
+ nextAlarm = nextAlarmDate . getTime ( )
56
+ } else {
57
+ console . log ( "It's too early in the morning. Setting alarm for 06:00 UTC" , nextAlarmDate . toISOString ( ) )
58
+ }
59
+ }
60
+
61
+ this . ctx . storage . setAlarm ( nextAlarm )
44
62
await this . sync ( )
45
63
}
46
64
You can’t perform that action at this time.
0 commit comments