-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom-thread-executor): Create record-model for custom schedule…
…d-executors
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
api/src/main/java/io/github/aivruu/packetboard/thread/CustomThreadExecutorModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// This file is part of packet-board, licensed under the GNU License. | ||
// | ||
// Copyright (c) 2024 aivruu | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
package io.github.aivruu.packetboard.thread; | ||
|
||
import io.github.aivruu.packetboard.board.BoardRepositoryModel; | ||
import io.github.aivruu.packetboard.board.CachedBoardModel; | ||
import io.github.aivruu.packetboard.repository.CachableModel; | ||
import io.github.aivruu.packetboard.repository.RepositoryModel; | ||
import io.github.aivruu.packetboard.thread.status.ThreadShutdownStatusProvider; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* This class is used as model for implementations that need to run periodically-tasks in a separate thread | ||
* during plugin's runtime. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public abstract class CustomThreadExecutorModel implements Runnable, CachableModel { | ||
private final String id; | ||
protected final RepositoryModel<CachedBoardModel> boardRepository; | ||
private final ScheduledExecutorService executorService; | ||
protected byte index = -1; | ||
|
||
/** | ||
* Creates a new {@link CustomThreadExecutorModel} with the given parameters. | ||
* | ||
* @param id this thread-executor's identifier. | ||
* @param boardRepository a {@link BoardRepositoryModel} instance. | ||
* @param executorService the {@link ScheduledExecutorService} for this thread-executor instance. | ||
* @since 1.0.0 | ||
*/ | ||
public CustomThreadExecutorModel(final String id, final RepositoryModel<CachedBoardModel> boardRepository, final ScheduledExecutorService executorService) { | ||
this.id = id; | ||
this.boardRepository = boardRepository; | ||
this.executorService = executorService; | ||
} | ||
|
||
/** | ||
* Returns this thread-executor's identifier. | ||
* | ||
* @return This {@link CustomThreadExecutorModel}'s id. | ||
* @since 1.0.0 | ||
*/ | ||
@Override | ||
public String id() { | ||
return this.id; | ||
} | ||
|
||
/** | ||
* Schedules and run this thread-executor at a fixed-rate using the given period-rate value. | ||
* | ||
* @param periodRate the value for this thread-executor's execution period. | ||
* @since 1.0.0 | ||
*/ | ||
public void schedule(final byte periodRate) { | ||
this.executorService.scheduleAtFixedRate(this, 0, periodRate, TimeUnit.SECONDS); | ||
} | ||
|
||
/** | ||
* Shutdowns this thread-executor and provides an status for this operation. | ||
* | ||
* @return The {@link ThreadShutdownStatusProvider} with the status for the operation. | ||
* - {@link ThreadShutdownStatusProvider#withShutdownFailure()} if an exception ocurred during thread's | ||
* shutdown-process. | ||
* <p> | ||
* - {@link ThreadShutdownStatusProvider#withShutdownSuccess()} if the executor was shutdown correctly and | ||
* during thread's termination-time {@code (5 seconds)}. | ||
* <p> | ||
* - {@link ThreadShutdownStatusProvider#withShutdownImmediate()} if the executor couldn't terminate before | ||
* given termination-time. | ||
* @since 1.0.0 | ||
*/ | ||
public ThreadShutdownStatusProvider shutdown() { | ||
try { | ||
this.executorService.shutdown(); | ||
final var terminatedBeforeTimeout = this.executorService.awaitTermination(5, TimeUnit.SECONDS); | ||
if (terminatedBeforeTimeout) { | ||
return ThreadShutdownStatusProvider.withShutdownSuccess(); | ||
} | ||
this.executorService.shutdownNow(); | ||
return ThreadShutdownStatusProvider.withShutdownImmediate(); | ||
} catch (final InterruptedException exception) { | ||
exception.printStackTrace(); | ||
return ThreadShutdownStatusProvider.withShutdownFailure(); | ||
} | ||
} | ||
} |