| 
4 | 4 | 
 
  | 
5 | 5 | package io.gitpod.jetbrains.remote.listeners  | 
6 | 6 | 
 
  | 
 | 7 | +import com.intellij.notification.Notification  | 
 | 8 | +import com.intellij.notification.NotificationAction  | 
 | 9 | +import com.intellij.notification.NotificationType  | 
 | 10 | +import com.intellij.notification.Notifications  | 
 | 11 | +import com.intellij.openapi.actionSystem.AnActionEvent  | 
 | 12 | +import com.intellij.openapi.application.ApplicationManager  | 
7 | 13 | import com.intellij.openapi.diagnostic.thisLogger  | 
8 | 14 | import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId  | 
9 | 15 | import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener  | 
10 |  | -import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter  | 
11 | 16 | import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType  | 
12 |  | -import com.intellij.openapi.progress.ProgressIndicator  | 
13 |  | -import com.intellij.openapi.progress.ProgressManager  | 
14 |  | -import com.intellij.openapi.progress.Task  | 
15 |  | -import kotlinx.coroutines.delay  | 
16 |  | -import java.util.concurrent.CompletableFuture  | 
17 |  | -import java.util.concurrent.TimeUnit  | 
 | 17 | +import java.io.File  | 
18 | 18 | 
 
  | 
19 | 19 | class GradleSyncListener : ExternalSystemTaskNotificationListener {  | 
20 | 20 |     override fun onStart(id: ExternalSystemTaskId, workingDir: String?) {  | 
21 |  | -        thisLogger().warn("===========hwen.onStart.1 ${id.projectSystemId} ${id.type} ${workingDir}")  | 
22 | 21 |         if (id.projectSystemId.toString() != "GRADLE" || id.type != ExternalSystemTaskType.RESOLVE_PROJECT) {  | 
23 | 22 |             return  | 
24 | 23 |         }  | 
25 |  | -        thisLogger().warn("===========hwen.onStart.2 gradle")  | 
26 |  | - | 
27 |  | -        Thread.sleep(90000L)  | 
28 |  | -        thisLogger().warn("===========hwen.onStart.3")  | 
29 |  | - | 
30 |  | -        // 使用后台任务执行延迟  | 
31 |  | -        ProgressManager.getInstance().run(  | 
32 |  | -            object : Task.Backgroundable(null, "Waiting before Gradle sync", true) {  | 
33 |  | -                override fun run(indicator: ProgressIndicator) {  | 
34 |  | -                    try {  | 
35 |  | -                        indicator.isIndeterminate = false  | 
36 |  | - | 
37 |  | -                        for (i in 1..10) {  | 
38 |  | -                            if (indicator.isCanceled) {  | 
39 |  | -                                thisLogger().warn("===========hwen.4 Delay was cancelled by user")  | 
40 |  | -                                return  | 
41 |  | -                            }  | 
42 |  | - | 
43 |  | -                            indicator.fraction = i / 10.0  | 
44 |  | -                            indicator.text = "Waiting ${10 - i} seconds before Gradle sync..."  | 
45 |  | - | 
46 |  | -                            CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS)  | 
47 |  | -                                .execute {}.run {  }  | 
48 |  | -                        }  | 
 | 24 | +        val lockFile = File("/tmp/gitpod-gradle.lock")  | 
 | 25 | +        if (!lockFile.exists()) {  | 
 | 26 | +            return  | 
 | 27 | +        }  | 
49 | 28 | 
 
  | 
50 |  | -                        thisLogger().warn("===========hwen.5 Delay completed, continuing with sync")  | 
51 |  | -                    } catch (e: Exception) {  | 
52 |  | -                        thisLogger().error("===========hwen.6 Error during delay", e)  | 
53 |  | -                    }  | 
54 |  | -                }  | 
 | 29 | +        val notification = Notification(  | 
 | 30 | +            "gitpod",  | 
 | 31 | +            "Gitpod: Pause gradle sync",  | 
 | 32 | +            "Pausing Gradle Sync, execute <code style='color: orange;'>gp jetbrains gradle resume</code> to unblock all builtin Gradle Sync",  | 
 | 33 | +            NotificationType.INFORMATION  | 
 | 34 | +        )  | 
 | 35 | +        var isCancelled = false  | 
 | 36 | +        notification.addAction(object : NotificationAction("Cancel") {  | 
 | 37 | +            override fun actionPerformed(e: AnActionEvent, notification: Notification) {  | 
 | 38 | +                isCancelled = true  | 
 | 39 | +                notification.expire()  | 
 | 40 | +            }  | 
 | 41 | +        })  | 
 | 42 | +        Notifications.Bus.notify(notification)  | 
55 | 43 | 
 
  | 
56 |  | -                override fun onCancel() {  | 
57 |  | -                    thisLogger().warn("==============hwen.7 Delay was cancelled")  | 
58 |  | -                    super.onCancel()  | 
59 |  | -                }  | 
 | 44 | +        while (lockFile.exists()) {  | 
 | 45 | +            if (isCancelled) {  | 
 | 46 | +                thisLogger().warn("gitpod: gradle sync pausing is cancelled")  | 
 | 47 | +                break  | 
60 | 48 |             }  | 
61 |  | -        )  | 
 | 49 | +            Thread.sleep(1000)  | 
 | 50 | +        }  | 
 | 51 | +        thisLogger().warn("gitpod: gradle sync pausing finished")  | 
 | 52 | +        ApplicationManager.getApplication().invokeLater {  | 
 | 53 | +            notification.expire()  | 
 | 54 | +        }  | 
62 | 55 |     }  | 
63 | 56 | }  | 
0 commit comments