Skip to content

Commit

Permalink
observer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
samgst-amazon committed Nov 21, 2024
1 parent 5506ca8 commit dd72d29
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package software.aws.toolkits.jetbrains.core.notifications
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.intellij.ide.util.RunOnceUtil
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
Expand All @@ -15,7 +16,6 @@ import com.intellij.util.AlarmFactory
import com.intellij.util.io.HttpRequests
import software.aws.toolkits.core.utils.RemoteResolveParser
import software.aws.toolkits.core.utils.RemoteResource
import software.aws.toolkits.core.utils.debug
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.jetbrains.core.DefaultRemoteResourceResolverProvider
Expand All @@ -31,6 +31,7 @@ private const val RETRY_DELAY_MS = 1000L

interface NotificationPollingService {
fun startPolling()
fun addObserver(observer: (Path) -> Unit)
fun dispose()
}

Expand All @@ -52,6 +53,7 @@ class NotificationPollingServiceImpl :
PersistentStateComponent<NotificationPollingServiceImpl.State>,
Disposable {

private val observers = mutableListOf<(Path) -> Unit>()
private var state = State()
private val alarm = AlarmFactory.getInstance().create(Alarm.ThreadToUse.POOLED_THREAD, this)
private val pollingIntervalMs = Duration.ofMinutes(10).toMillis()
Expand All @@ -62,24 +64,13 @@ class NotificationPollingServiceImpl :
override val remoteResolveParser: RemoteResolveParser = NotificationFileValidator
}

data class State(
var currentETag: String? = null,
var cachedFilePath: String? = null
)

override fun getState(): State = state

override fun loadState(state: State) {
this.state = state
}

override fun dispose() {
alarm.dispose()
}

override fun startPolling() {
pollForNotifications()
// todo notify observers
val newNotifications = pollForNotifications()
if (newNotifications) {
getCachedPath()?.let { path ->
notifyObservers(path)
}
}
alarm.addRequest(
{ startPolling() },
pollingIntervalMs
Expand All @@ -98,7 +89,12 @@ class NotificationPollingServiceImpl :
try {
val newETag = getNotificationETag()
if (newETag == state.currentETag) {
LOG.debug { "No updates available for notifications" }
RunOnceUtil.runOnceForApp(this::class.qualifiedName.toString()) {
// try startup notifications regardless of file change
getCachedPath()?.let { path ->
notifyObservers(path)
}
}
return false
}
val resolvedPath = resourceResolver.get()
Expand Down Expand Up @@ -133,9 +129,14 @@ class NotificationPollingServiceImpl :
fun getCachedPath(): Path? =

Check notice on line 129 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/notifications/NotificationPollingService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'getCachedPath' could be private
state.cachedFilePath?.let { Paths.get(it) }

/**
* Emits telemetry metric for polling failures
*/
override fun addObserver(observer: (Path) -> Unit) {
observers.add(observer)
}

private fun notifyObservers(path: Path) {
observers.forEach { it(path) }
}

private fun emitFailureMetric(exception: Exception?) {
// todo: add metric
}
Expand All @@ -145,4 +146,19 @@ class NotificationPollingServiceImpl :
fun getInstance(): NotificationPollingService =
ApplicationManager.getApplication().getService(NotificationPollingService::class.java)
}

data class State(
var currentETag: String? = null,
var cachedFilePath: String? = null,
)

override fun getState(): State = state

override fun loadState(state: State) {
this.state = state
}

override fun dispose() {
alarm.dispose()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
package software.aws.toolkits.jetbrains.core.notifications

import com.intellij.ide.util.RunOnceUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity

class NotificationServiceInitializer : ProjectActivity {

Check warning on line 10 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/notifications/NotificationServiceInitializer.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Extension class should be final and non-public

Extension class should not be public

Check warning on line 10 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/notifications/NotificationServiceInitializer.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Extension class should be final and non-public

Extension class should not be public

override suspend fun execute(project: Project) {
val service = ApplicationManager.getApplication().getService(NotificationPollingService::class.java)
val service = NotificationPollingServiceImpl.getInstance()
RunOnceUtil.runOnceForApp(this::class.qualifiedName.toString()) {
service.startPolling()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
package software.aws.toolkits.jetbrains.core.notifications

import com.intellij.openapi.project.Project
import java.nio.file.Path

class ProcessNotificationsBase {
init {
// TODO: install a listener for the polling class
NotificationPollingServiceImpl.getInstance().addObserver { path ->
getNotificationsFromFile(path)
}
}

fun getNotificationsFromFile() {
fun getNotificationsFromFile(path: Path) {

Check notice on line 16 in plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/notifications/ProcessNotificationsBase.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'getNotificationsFromFile' could be private
// TODO: returns a notification list
}

Expand Down

0 comments on commit dd72d29

Please sign in to comment.