Skip to content

Commit 934006d

Browse files
committed
link schedule to cron tab when applicable
1 parent 834bcdf commit 934006d

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

common/src/main/protobuf/model/schedule.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ message Schedule {
2929
optional bool repeatedly = 5;
3030
optional google.protobuf.Timestamp scheduledDate = 6 [(scalapb.field).type = "java.util.Date"];
3131
optional google.protobuf.Timestamp lastTriggered = 7 [(scalapb.field).type = "java.util.Date"];
32+
optional string cronTab = 8;
3233
}
3334

3435
// CronTab

common/src/main/scala/app/softnetwork/scheduler/model/package.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ package object model {
103103
.withKey(cronTab.key)
104104
.withScheduledDate(date)
105105
.withRepeatedly(true)
106+
.withCronTab(cronTab.uuid)
106107
)
107108
case _ => None
108109
}

core/src/main/scala/app/softnetwork/scheduler/persistence/typed/SchedulerBehavior.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ private[scheduler] trait SchedulerBehavior
377377
ct.entityId != ALL_KEY && ct.persistenceId == cronTab.persistenceId && ct.key == cronTab.key
378378
)
379379
.map(ct => CronTabRemovedEvent(ct.persistenceId, ct.entityId, ct.key))
380-
.toList
380+
.toList ++ scheduler.schedules
381+
.filter(_.getCronTab == cmd.uuid)
382+
.map(s => ScheduleRemovedEvent(s.persistenceId, s.entityId, s.key))
381383
} else {
382384
List.empty
383385
}

testkit/src/test/scala/app/softnetwork/scheduler/handlers/SchedulerHandlerSpec.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package app.softnetwork.scheduler.handlers
22

3-
import akka.actor.testkit.typed.scaladsl.TestProbe
43
import akka.actor.typed.ActorSystem
54
import app.softnetwork.persistence._
65
import app.softnetwork.scheduler.message.SampleMessages.{AddSample, SampleAdded}
76
import app.softnetwork.scheduler.scalatest.SchedulerWithSampleTestKit
87
import org.scalatest.wordspec.AnyWordSpecLike
98
import app.softnetwork.scheduler.message._
10-
import app.softnetwork.scheduler.persistence.query.SampleScheduleTriggered
119
import app.softnetwork.scheduler.persistence.typed.SampleBehavior
1210
import org.softnetwork.akka.model.{CronTab, Schedule}
1311

@@ -25,14 +23,14 @@ class SchedulerHandlerSpec
2523
implicit lazy val ec: ExecutionContextExecutor = system.executionContext
2624

2725
"Scheduler" must {
26+
val cronTab = CronTab(SampleBehavior.persistenceId, ALL_KEY, "cron", "* * * * *")
2827
"add Cron Tab" in {
2928
// add Sample[sample] entity
3029
SampleHandler ? ("sample", AddSample) assert {
3130
case SampleAdded => succeed
3231
case _ => fail()
3332
}
3433
// add cron tab for all Sample entity
35-
val cronTab = CronTab(SampleBehavior.persistenceId, ALL_KEY, "cron", "* * * * *")
3634
this !? AddCronTab(cronTab) assert {
3735
case _: CronTabAdded => succeed
3836
case other => fail(other.getClass)
@@ -55,11 +53,31 @@ class SchedulerHandlerSpec
5553
case Some(schedule) =>
5654
assert(schedule.repeatedly.getOrElse(false))
5755
assert(schedule.getScheduledDate.equals(schedule.getLastTriggered))
56+
assert(schedule.getCronTab == cronTab.uuid)
5857
case _ => fail("schedule not found")
5958
}
6059
case _ => fail()
6160
}
6261
}
62+
"remove Cron Tab" in {
63+
this !? RemoveCronTab(cronTab.persistenceId, cronTab.entityId, cronTab.key) assert {
64+
case _: CronTabRemoved => succeed
65+
case other => fail(other.getClass)
66+
}
67+
this !? LoadScheduler assert {
68+
case r: SchedulerLoaded =>
69+
val scheduler = r.scheduler
70+
logger.info(scheduler.toProtoString)
71+
assert(!scheduler.cronTabs.exists(ct => ct.uuid == cronTab.uuid))
72+
scheduler.schedules.find(s =>
73+
s.persistenceId == SampleBehavior.persistenceId && s.entityId == "sample" && s.key == cronTab.key
74+
) match {
75+
case Some(_) => fail()
76+
case _ => succeed
77+
}
78+
case _ => fail()
79+
}
80+
}
6381
"remove schedule that is not repeatable and has already been triggered" in {
6482
val schedule = Schedule("p", "0", "add", 1, None, None, None)
6583
// add schedule which has to be triggered

0 commit comments

Comments
 (0)