Skip to content

Commit

Permalink
Updated icon
Browse files Browse the repository at this point in the history
Fixed AlreadyDisposedException in scheduled tasks
  • Loading branch information
ikarimullin committed Dec 10, 2022
1 parent 136e3ac commit 0ebaaa1
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public boolean canClose(@NotNull final Project project) {
Util.showWorklogDialog(project, branchName, branchName);
state.getTimer(branchName, project).pause(project);
}
final TimerUpdater updater = TimerUpdater.getInstance(project);
if (updater != null) {
updater.cancel();
}
final JiraWorklogStartupActivity startupActivity = JiraWorklogStartupActivity.getInstance(project);
if (startupActivity != null) {
startupActivity.cancel();
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.changes.shelf.ShelveChangesManager;
import com.intellij.serviceContainer.AlreadyDisposedException;
import com.intellij.util.concurrency.AppExecutorUtil;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class JiraWorklogStartupActivity extends ShelveChangesManager.PostStartupActivity {

private static final Logger logger = Logger.getInstance(JiraWorklogStartupActivity.class);

private ScheduledFuture<?> schedule;

public static JiraWorklogStartupActivity getInstance(final Project project) {
return project.getService(JiraWorklogStartupActivity.class);
}

@Override
public void runActivity(@NotNull final Project project) {
logger.info("Executing Jira Worklog Plugin startup actions...");
Expand All @@ -30,7 +38,7 @@ public void runActivity(@NotNull final Project project) {
init(currentBranch, state, project);
} else {
logger.info("Current branch not found. Scheduling periodic task, which will try to initialize it");
AppExecutorUtil.getAppScheduledExecutorService().schedule(
this.schedule = AppExecutorUtil.getAppScheduledExecutorService().schedule(
() -> initFromCurrentBranch(project),
5L,
TimeUnit.SECONDS
Expand All @@ -40,9 +48,16 @@ public void runActivity(@NotNull final Project project) {
TimerUpdater.getInstance(project).setup(project);
}

private static void initFromCurrentBranch(final @NotNull Project project) {
final String branch = Util.getCurrentBranch(project);
final JiraWorklogPluginState state = JiraWorklogPluginState.getInstance(project);
private void initFromCurrentBranch(final @NotNull Project project) {
final String branch;
final JiraWorklogPluginState state;
try {
branch = Util.getCurrentBranch(project);
state = JiraWorklogPluginState.getInstance(project);
} catch (final AlreadyDisposedException ex) {
cancel();
return;
}
if (branch != null) {
logger.info(String.format("Found current branch %s inside periodic task", branch));
//noinspection SynchronizationOnLocalVariableOrMethodParameter
Expand All @@ -59,7 +74,13 @@ private static void initFromCurrentBranch(final @NotNull Project project) {
}
}

private static void init(
void cancel() {
if (schedule != null) {
schedule.cancel(true);
}
}

private void init(
final String currentBranch,
final JiraWorklogPluginState state,
final @NotNull Project project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,39 @@

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.serviceContainer.AlreadyDisposedException;
import com.intellij.util.concurrency.AppExecutorUtil;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class TimerUpdater {

private static final Logger logger = Logger.getInstance(TimerUpdater.class);

private int numUpdates;
private ScheduledFuture<?> schedule;

static TimerUpdater getInstance(final Project project) {
return project.getService(TimerUpdater.class);
}

void setup(final Project project) {
final long updateInterval = Duration.ofSeconds(10).toNanos();
AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(
this.schedule = AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(
() -> {
final JiraWorklogPluginState state = JiraWorklogPluginState.getInstance(project);
final JiraWorklogPluginState state;
try {
state = JiraWorklogPluginState.getInstance(project);
} catch (final AlreadyDisposedException ex) {
cancel();
return;
}
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (state) {
final Set<Timer> activeTimers = state.getActiveTimers();
Expand Down Expand Up @@ -77,4 +86,10 @@ void setup(final Project project) {
logger.info("TimerUpdater setup");
}

void cancel() {
if (this.schedule != null) {
this.schedule.cancel(true);
}
}

}
7 changes: 5 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
<name>Jira Worklog</name>

<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
<vendor email="ilyaskarimullin1997@gmail.com" url="https://penisland.net/">texhnolyzze</vendor>
<vendor email="ilyaskarimullin1997@gmail.com" url="https://github.com/texhnolyzze">texhnolyzze</vendor>

<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
<description><![CDATA[
This plugin listens for branch changes, git push and project close events
and provides a simple form, where you can log spent time in Jira.
For git push events notification need to be enabled.
For git push events notification need to be enabled.<br><br>
<a href="https://github.com/texhnolyzze/jira-worklog-intellij-plugin">Sources/Description</a>
]]></description>

<!-- Product and plugin compatibility requirements.
Expand All @@ -34,6 +36,7 @@
<projectService serviceImplementation="com.github.texhnolyzze.jiraworklogplugin.JiraClient"/>
<prePushHandler implementation="com.github.texhnolyzze.jiraworklogplugin.VcsHandler"/>
<projectService serviceImplementation="com.github.texhnolyzze.jiraworklogplugin.TimerUpdater"/>
<projectService serviceImplementation="com.github.texhnolyzze.jiraworklogplugin.JiraWorklogStartupActivity"/>
</extensions>
<projectListeners>
<listener class="com.github.texhnolyzze.jiraworklogplugin.VcsHandler"
Expand Down
135 changes: 123 additions & 12 deletions src/main/resources/META-INF/pluginIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ebaaa1

Please sign in to comment.