Skip to content

Commit 691d131

Browse files
committed
etag singleton state component
1 parent c5718f7 commit 691d131

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

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

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import software.aws.toolkits.core.utils.getLogger
1919
import software.aws.toolkits.jetbrains.core.DefaultRemoteResourceResolverProvider
2020
import software.aws.toolkits.jetbrains.core.RemoteResourceResolverProvider
2121
import java.io.InputStream
22-
import java.nio.file.Path
23-
import java.nio.file.Paths
2422
import java.time.Duration
2523

2624
private const val NOTIFICATION_ENDPOINT = "" // TODO: Replace with actual endpoint
@@ -38,13 +36,23 @@ object NotificationFileValidator : RemoteResolveParser {
3836
}
3937
}
4038

41-
@Service(Service.Level.APP)
42-
class NotificationPollingService :
43-
PersistentStateComponent<NotificationPollingService.State>,
44-
Disposable {
39+
object NotificationState : PersistentStateComponent<NotificationState.State> {
40+
data class State(
41+
var etag: String? = null,
42+
)
43+
44+
private var myState = State()
45+
46+
override fun getState(): State = myState
47+
48+
override fun loadState(state: State) {
49+
myState = state
50+
}
51+
}
4552

53+
@Service(Service.Level.APP)
54+
class NotificationPollingService : Disposable {
4655
private val observers = mutableListOf<(Unit) -> Unit>()
47-
private var state = State()
4856
private val alarm = AlarmFactory.getInstance().create(Alarm.ThreadToUse.POOLED_THREAD, this)
4957
private val pollingIntervalMs = Duration.ofMinutes(10).toMillis()
5058
private val resourceResolver: RemoteResourceResolverProvider = DefaultRemoteResourceResolverProvider()
@@ -76,7 +84,7 @@ class NotificationPollingService :
7684
while (retryCount < MAX_RETRIES) {
7785
try {
7886
val newETag = getNotificationETag()
79-
if (newETag == state.currentETag) {
87+
if (newETag == NotificationState.getState().etag) {
8088
RunOnceUtil.runOnceForApp(this::class.qualifiedName.toString()) {
8189
// try startup notifications regardless of file change
8290
notifyObservers()
@@ -87,8 +95,7 @@ class NotificationPollingService :
8795
.resolve(notificationsResource)
8896
.toCompletableFuture()
8997
.get()
90-
state.currentETag = newETag
91-
state.cachedFilePath = resolvedPath.toString()
98+
NotificationState.getState().etag = newETag
9299
return true
93100
} catch (e: Exception) {
94101
lastException = e
@@ -111,42 +118,27 @@ class NotificationPollingService :
111118
request.connection.headerFields["ETag"]?.firstOrNull() ?: ""
112119
}
113120

114-
// Helper method to get Path from stored String
115-
fun getCachedPath(): Path? =
116-
state.cachedFilePath?.let { Paths.get(it) }
121+
private fun emitFailureMetric(exception: Exception?) {
122+
// todo: add metric
123+
}
117124

118125
fun addObserver(observer: (Unit) -> Unit) {
119126
observers.add(observer)
120127
}
121128

122129
private fun notifyObservers() {
123-
observers.forEach {observer ->
130+
observers.forEach { observer ->
124131
observer(Unit)
125132
}
126133
}
127134

128-
private fun emitFailureMetric(exception: Exception?) {
129-
// todo: add metric
135+
override fun dispose() {
136+
alarm.dispose()
130137
}
131138

132139
companion object {
133140
private val LOG = getLogger<NotificationPollingService>()
134141
fun getInstance(): NotificationPollingService =
135142
ApplicationManager.getApplication().getService(NotificationPollingService::class.java)
136143
}
137-
138-
data class State(
139-
var currentETag: String? = null,
140-
var cachedFilePath: String? = null,
141-
)
142-
143-
override fun getState(): State = state
144-
145-
override fun loadState(state: State) {
146-
this.state = state
147-
}
148-
149-
override fun dispose() {
150-
alarm.dispose()
151-
}
152144
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.fasterxml.jackson.module.kotlin.readValue
99
import com.intellij.openapi.application.PathManager
1010
import com.intellij.openapi.project.Project
1111
import software.aws.toolkits.core.utils.inputStream
12-
import java.nio.file.Path
1312
import java.nio.file.Paths
1413

1514
object NotificationMapperUtil {

0 commit comments

Comments
 (0)