Skip to content

Commit

Permalink
Fixes #22378: Generate policies for campaigns before it starts offici…
Browse files Browse the repository at this point in the history
…ally, delete them after it stops (1 hour delay each)
  • Loading branch information
VinceMacBuche committed Feb 16, 2023
1 parent 2205353 commit 02e2945
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ class MainCampaignService(repo: CampaignEventRepository, campaignRepo: CampaignR
event.copy(state = Skipped(s"Event was cancelled because campaign is archived. ${reasonMessage}"))
)
case Enabled =>
if (event.start.isAfter(now)) {
val effectiveStart = event.start.minusHours(1)
if (effectiveStart.isAfter(now)) {
for {
_ <-
CampaignLogger.debug(
s"Scheduled Campaign event ${event.id.value} put to sleep until it should start, on ${DateFormaterService
.serialize(event.start)}"
.serialize(effectiveStart)}, one hour before official start date, to ensure policies are correctly dispatched, nothing will be applied on the node"
)
_ <- ZIO.sleep(Duration.fromMillis(event.start.getMillis - now.getMillis))
_ <- ZIO.sleep(Duration.fromMillis(effectiveStart.getMillis - now.getMillis))
} yield {
().succeed
}
Expand All @@ -192,30 +193,34 @@ class MainCampaignService(repo: CampaignEventRepository, campaignRepo: CampaignR
}
}
case Running =>
if (event.start.isAfter(now)) {
val effectiveStart = event.start.minusHours(1)
val effectiveEnd = event.end.plusHours(1)
if (effectiveStart.isAfter(now)) {
for {
// Campaign should be planned, not running
_ <-
CampaignLogger.warn(
s"Campaign event ${event.id.value} was considered Running but we are before it start date, setting state to Schedule and wait for event to start, on ${DateFormaterService
.serialize(event.start)}"
.serialize(effectiveStart)}, one hour before official start date, to ensure policies are correctly dispatched, nothing will be applied on the node"
)
_ <- repo.saveCampaignEvent(event.copy(state = Scheduled))
_ <- CampaignLogger.debug(
s"Scheduled Campaign event ${event.id.value} put to sleep until it should start, on ${DateFormaterService
.serialize(event.start)}"
)
_ <- ZIO.sleep(Duration.fromMillis(event.start.getMillis - now.getMillis))
_ <-
CampaignLogger.debug(
s"Scheduled Campaign event ${event.id.value} put to sleep until it should start, on ${DateFormaterService
.serialize(effectiveStart)}, one hour before official start date, to ensure policies are correctly dispatched, nothing will be applied on the node"
)

_ <- ZIO.sleep(Duration.fromMillis(effectiveStart.getMillis - now.getMillis))
} yield {
()
}
} else if (event.end.isAfter(now)) {
} else if (effectiveEnd.isAfter(now)) {
for {
_ <- CampaignLogger.debug(
s"Running Campaign event ${event.id.value} put to sleep until it should end, on ${DateFormaterService
.serialize(event.end)}"
.serialize(effectiveEnd)}, one hour after official end date, so that we can gather results"
)
_ <- ZIO.sleep(Duration.fromMillis(event.end.getMillis - now.getMillis))
_ <- ZIO.sleep(Duration.fromMillis(effectiveEnd.getMillis - now.getMillis))
} yield {
()
}
Expand Down

0 comments on commit 02e2945

Please sign in to comment.