-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathDfpDataCacheLifecycle.scala
127 lines (119 loc) · 3.77 KB
/
DfpDataCacheLifecycle.scala
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package dfp
import app.LifecycleComponent
import common.dfp.{GuAdUnit, GuCreativeTemplate, GuCustomField, GuCustomTargeting}
import common._
import play.api.inject.ApplicationLifecycle
import scala.concurrent.{ExecutionContext, Future}
class DfpDataCacheLifecycle(
appLifecycle: ApplicationLifecycle,
jobScheduler: JobScheduler,
creativeTemplateAgent: CreativeTemplateAgent,
adUnitAgent: AdUnitAgent,
advertiserAgent: AdvertiserAgent,
customFieldAgent: CustomFieldAgent,
orderAgent: OrderAgent,
placementAgent: PlacementAgent,
customTargetingAgent: CustomTargetingAgent,
dfpDataCacheJob: DfpDataCacheJob,
customTargetingKeyValueJob: CustomTargetingKeyValueJob,
dfpAdUnitCacheJob: DfpAdUnitCacheJob,
dfpMobileAppAdUnitCacheJob: DfpMobileAppAdUnitCacheJob,
dfpFacebookIaAdUnitCacheJob: DfpFacebookIaAdUnitCacheJob,
dfpTemplateCreativeCacheJob: DfpTemplateCreativeCacheJob,
pekkoAsync: PekkoAsync,
)(implicit ec: ExecutionContext)
extends LifecycleComponent {
appLifecycle.addStopHook { () =>
Future {
jobs foreach { job =>
jobScheduler.deschedule(job.name)
}
}
}
trait Job[T] {
val name: String
val interval: Int
def run(): Future[T]
}
val jobs = Set(
new Job[DataCache[String, GuAdUnit]] {
val name = "DFP-AdUnits-Update"
val interval = 30
def run() = adUnitAgent.refresh()
},
new Job[DataCache[String, GuCustomField]] {
val name = "DFP-CustomFields-Update"
val interval = 30
def run() = customFieldAgent.refresh()
},
new Job[DataCache[Long, GuCustomTargeting]] {
val name = "DFP-CustomTargeting-Update"
val interval = 30
def run() = customTargetingAgent.refresh()
},
new Job[Unit] {
val name: String = "DFP-CustomTargeting-Store"
val interval: Int = 15
def run() = customTargetingKeyValueJob.run()
},
new Job[DataCache[Long, Seq[String]]] {
val name = "DFP-Placements-Update"
val interval = 30
def run() = placementAgent.refresh()
},
new Job[Unit] {
val name: String = "DFP-Cache"
val interval: Int = 2
def run(): Future[Unit] = dfpDataCacheJob.run()
},
new Job[Unit] {
val name: String = "DFP-Ad-Units-Update"
val interval: Int = 60
def run(): Future[Unit] = dfpAdUnitCacheJob.run(pekkoAsync)
},
new Job[Unit] {
val name: String = "DFP-Mobile-Apps-Ad-Units-Update"
val interval: Int = 60
def run(): Future[Unit] = dfpMobileAppAdUnitCacheJob.run(pekkoAsync)
},
new Job[Unit] {
val name: String = "DFP-Facebook-IA-Ad-Units-Update"
val interval: Int = 60
def run(): Future[Unit] = dfpFacebookIaAdUnitCacheJob.run(pekkoAsync)
},
new Job[Seq[GuCreativeTemplate]] {
val name: String = "DFP-Creative-Templates-Update"
val interval: Int = 15
def run() = creativeTemplateAgent.refresh()
},
new Job[Unit] {
val name: String = "DFP-Template-Creatives-Cache"
val interval: Int = 2
def run() = dfpTemplateCreativeCacheJob.run()
},
new Job[Unit] {
val name = "DFP-Order-Advertiser-Update"
val interval: Int = 300
def run() = {
Future.sequence(Seq(advertiserAgent.refresh(), orderAgent.refresh())).map(_ => ())
}
},
)
override def start(): Unit = {
jobs foreach { job =>
jobScheduler.deschedule(job.name)
jobScheduler.scheduleEveryNMinutes(job.name, job.interval) {
job.run().map(_ => ())
}
}
pekkoAsync.after1s {
dfpDataCacheJob.refreshAllDfpData()
creativeTemplateAgent.refresh()
dfpTemplateCreativeCacheJob.run()
customTargetingKeyValueJob.run()
advertiserAgent.refresh()
orderAgent.refresh()
customFieldAgent.refresh()
}
}
}