Skip to content

Commit bd354fc

Browse files
committed
run on startup
1 parent f0dbe5b commit bd354fc

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/notifications/NotificationPollingService.kt

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
77
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
88
import com.fasterxml.jackson.module.kotlin.readValue
99
import com.intellij.openapi.Disposable
10+
import com.intellij.openapi.application.ApplicationManager
1011
import com.intellij.openapi.application.PathManager
1112
import com.intellij.openapi.components.Service
1213
import com.intellij.util.Alarm
@@ -27,56 +28,30 @@ private const val NOTIFICATION_ENDPOINT = "https://idetoolkits-hostedfiles.amazo
2728
private const val MAX_RETRIES = 3
2829
private const val RETRY_DELAY_MS = 1000L
2930

30-
@Service
31-
class NotificationPollingService : Disposable {
32-
/**
33-
* Data class representing the structure of notifications from the endpoint
34-
*/
35-
data class NotificationFile(
36-
val notifications: List<Notification>,
37-
val version: String,
38-
)
39-
40-
data class Notification(
41-
val id: String,
42-
val message: String,
43-
val criteria: NotificationCriteria,
44-
)
45-
46-
data class NotificationCriteria(
47-
val minVersion: String?,
48-
val maxVersion: String?,
49-
val regions: List<String>?,
50-
val ideType: String?,
51-
val pluginVersion: String?,
52-
val os: String?,
53-
val authType: String?,
54-
val authRegion: String?,
55-
val authState: String?,
56-
val authScopes: List<String>?,
57-
val installedPlugins: List<String>?,
58-
val computeEnvironment: String?,
59-
val messageType: String?,
60-
)
31+
32+
interface NotificationPollingService {
33+
fun startPolling()
34+
fun dispose()
35+
}
36+
37+
@Service(Service.Level.APP)
38+
class NotificationPollingServiceImpl : NotificationPollingService, Disposable {
39+
6140
private val mapper = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
62-
private val LOG = getLogger<NotificationPollingService>()
63-
private var currentETag: String? = null
41+
private var currentETag: String? = null // todo Persistant state? add an init possibly
6442
private val alarm = AlarmFactory.getInstance().create(Alarm.ThreadToUse.POOLED_THREAD, this)
65-
66-
init {
67-
startPolling()
68-
}
43+
private val pollingIntervalMs = Duration.ofMinutes(10).toMillis()
6944

7045
override fun dispose() {
7146
alarm.dispose()
7247
}
7348

74-
private fun startPolling() {
49+
override fun startPolling() {
7550
pollForNotifications()
7651

7752
alarm.addRequest(
7853
{ startPolling() },
79-
Duration.ofMinutes(10).toMillis()
54+
pollingIntervalMs
8055
)
8156
}
8257

@@ -99,7 +74,7 @@ class NotificationPollingService : Disposable {
9974
}
10075

10176
// Download and process new notifications
102-
val notifications = downloadAndProcessNotifications(newETag)
77+
val notifications = downloadAndProcessNotifications()
10378
currentETag = newETag
10479
return notifications
10580
} catch (e: Exception) {
@@ -125,7 +100,7 @@ class NotificationPollingService : Disposable {
125100
request.connection.headerFields["ETag"]?.firstOrNull() ?: ""
126101
}
127102

128-
private fun downloadAndProcessNotifications(newETag: String): NotificationFile {
103+
private fun downloadAndProcessNotifications(): NotificationFile {
129104
val content = HttpRequests.request(NOTIFICATION_ENDPOINT)
130105
.userAgent("AWS Toolkit for JetBrains")
131106
.readString()
@@ -214,5 +189,35 @@ class NotificationPollingService : Disposable {
214189

215190
companion object {
216191
private const val NOTIFICATIONS_RESOURCE_PATH = "/software/aws/toolkits/resources/notifications.json"
192+
private val LOG = getLogger<NotificationPollingServiceImpl>()
193+
fun getInstance(): NotificationPollingService =
194+
ApplicationManager.getApplication().getService(NotificationPollingService::class.java)
217195
}
218196
}
197+
198+
data class NotificationFile(
199+
val notifications: List<Notification>,
200+
val version: String,
201+
)
202+
203+
data class Notification(
204+
val id: String,
205+
val message: String,
206+
val criteria: NotificationCriteria,
207+
)
208+
209+
data class NotificationCriteria(
210+
val minVersion: String?,
211+
val maxVersion: String?,
212+
val regions: List<String>?,
213+
val ideType: String?,
214+
val pluginVersion: String?,
215+
val os: String?,
216+
val authType: String?,
217+
val authRegion: String?,
218+
val authState: String?,
219+
val authScopes: List<String>?,
220+
val installedPlugins: List<String>?,
221+
val computeEnvironment: String?,
222+
val messageType: String?,
223+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.core.notifications
5+
6+
import com.intellij.ide.util.RunOnceUtil
7+
import com.intellij.openapi.application.ApplicationManager
8+
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.startup.ProjectActivity
10+
11+
class NotificationServiceInitializer : ProjectActivity {
12+
13+
override suspend fun execute(project: Project) {
14+
val service = ApplicationManager.getApplication().getService(NotificationPollingService::class.java)
15+
RunOnceUtil.runOnceForApp(this::class.qualifiedName.toString()) {
16+
service.startPolling()
17+
}
18+
}
19+
}

plugins/core/src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<!-- each plugin needs its own instance of these -->
2323
<applicationService serviceImplementation="migration.software.aws.toolkits.jetbrains.core.coroutines.PluginCoroutineScopeTracker"/>
2424
<projectService serviceImplementation="migration.software.aws.toolkits.jetbrains.core.coroutines.PluginCoroutineScopeTracker"/>
25+
<postStartupActivity implementation = "software.aws.toolkits.jetbrains.core.notifications.NotificationServiceInitializer"/>
26+
<applicationService
27+
serviceInterface="software.aws.toolkits.jetbrains.core.notifications.NotificationPollingService"
28+
serviceImplementation="software.aws.toolkits.jetbrains.core.notifications.NotificationPollingServiceImpl"/>
2529
</extensions>
2630
<projectListeners>
2731
<listener class="software.aws.toolkits.jetbrains.services.telemetry.OpenedFileTypesMetricsListener" topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>

0 commit comments

Comments
 (0)