2020-05-26 07:53:41 +00:00
|
|
|
import { task, plugin } from '@ccms/api'
|
|
|
|
import { inject, provideSingleton } from '@ccms/container'
|
2020-06-20 08:39:03 +00:00
|
|
|
import thread_pool from '@ccms/common/dist/thread-pool'
|
2020-05-26 07:53:41 +00:00
|
|
|
|
|
|
|
const AtomicBoolean = Java.type("java.util.concurrent.atomic.AtomicBoolean")
|
|
|
|
const Thread = Java.type('java.lang.Thread')
|
|
|
|
|
|
|
|
@provideSingleton(task.TaskManager)
|
2020-09-22 10:42:39 +00:00
|
|
|
export class SpringTaskManager extends task.TaskManager {
|
2020-05-26 07:53:41 +00:00
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any
|
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
private tasks: { [s: string]: task.Cancelable }
|
|
|
|
private executor: java.util.concurrent.ThreadPoolExecutor
|
2020-06-20 08:39:03 +00:00
|
|
|
|
2020-05-26 07:53:41 +00:00
|
|
|
constructor() {
|
2020-09-22 10:42:39 +00:00
|
|
|
super()
|
|
|
|
this.tasks = {}
|
|
|
|
this.executor = thread_pool.create({
|
|
|
|
groupName: '@ccms/spring'
|
|
|
|
})
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 01:40:34 +00:00
|
|
|
create0(owner: plugin.Plugin, func: Function, id: number): task.Task {
|
|
|
|
return new SpringTask(owner, func, id, this)
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return func()
|
|
|
|
}
|
2020-09-22 10:42:39 +00:00
|
|
|
disable0() {
|
|
|
|
Object.values(this.tasks).forEach((task) => task?.cancel())
|
|
|
|
this.executor.shutdown()
|
|
|
|
}
|
|
|
|
nextId() {
|
|
|
|
return this.taskId.incrementAndGet()
|
|
|
|
}
|
|
|
|
submit(id: number, task: SpringTask, func: Function) {
|
|
|
|
this.tasks[id] = task
|
|
|
|
this.executor.execute(func as any)
|
|
|
|
}
|
|
|
|
cancel(id: number) {
|
|
|
|
delete this.tasks[id]
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SpringTask extends task.Task {
|
2020-09-22 10:42:39 +00:00
|
|
|
private id: number
|
|
|
|
private taskManager: SpringTaskManager
|
2020-05-26 07:53:41 +00:00
|
|
|
private running = new AtomicBoolean(true)
|
|
|
|
|
2020-11-13 01:40:34 +00:00
|
|
|
constructor(owner: plugin.Plugin, func: Function, id: number, taskManager: SpringTaskManager) {
|
|
|
|
super(owner, func, id)
|
2020-09-22 10:42:39 +00:00
|
|
|
this.id = taskManager.nextId()
|
|
|
|
this.taskManager = taskManager
|
|
|
|
}
|
|
|
|
|
2020-06-02 09:50:47 +00:00
|
|
|
run(...args: any[]) {
|
2020-05-26 07:53:41 +00:00
|
|
|
if (this.laterTime > 0) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(this.laterTime)
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-05-26 07:53:41 +00:00
|
|
|
Thread.currentThread().interrupt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (this.running.get()) {
|
|
|
|
try {
|
2020-06-02 09:50:47 +00:00
|
|
|
this.func(...args)
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
|
|
|
console.error("Task exec error:", ex)
|
|
|
|
console.ex(ex)
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
// If we have a interval of 0 or less, only run once
|
|
|
|
if (this.interval <= 0) { break }
|
|
|
|
try {
|
|
|
|
Thread.sleep(this.interval)
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-05-26 07:53:41 +00:00
|
|
|
Thread.currentThread().interrupt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.cancel()
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:40:34 +00:00
|
|
|
cancel0() {
|
2020-05-26 07:53:41 +00:00
|
|
|
var wasRunning = this.running.getAndSet(false)
|
|
|
|
if (wasRunning) {
|
2020-09-22 10:42:39 +00:00
|
|
|
this.taskManager.cancel(this.id)
|
2020-11-13 01:40:34 +00:00
|
|
|
return true
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
2020-11-13 01:40:34 +00:00
|
|
|
return false
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
submit0(...args: any[]) {
|
|
|
|
this.taskManager.submit(this.id, this, () => this.run(...args))
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
}
|