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
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
const AtomicInteger = Java.type("java.util.concurrent.atomic.AtomicInteger")
|
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 taskId: any
|
|
|
|
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.taskId = new AtomicInteger(0)
|
|
|
|
this.tasks = {}
|
|
|
|
this.executor = thread_pool.create({
|
|
|
|
groupName: '@ccms/spring'
|
|
|
|
})
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
create0(func: Function): task.Task {
|
|
|
|
return new SpringTask(this.pluginInstance, func, 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-09-22 10:42:39 +00:00
|
|
|
constructor(plugin: any, func: Function, taskManager: SpringTaskManager) {
|
|
|
|
super(plugin, func)
|
|
|
|
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)
|
|
|
|
} catch (ex) {
|
|
|
|
Thread.currentThread().interrupt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (this.running.get()) {
|
|
|
|
try {
|
2020-06-02 09:50:47 +00:00
|
|
|
this.func(...args)
|
2020-05-26 07:53:41 +00:00
|
|
|
} catch (t) {
|
|
|
|
console.error("Task exec error:", t)
|
|
|
|
console.ex(t)
|
|
|
|
}
|
|
|
|
// If we have a interval of 0 or less, only run once
|
|
|
|
if (this.interval <= 0) { break }
|
|
|
|
try {
|
|
|
|
Thread.sleep(this.interval)
|
|
|
|
} catch (ex) {
|
|
|
|
Thread.currentThread().interrupt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.cancel()
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
cancel0(): any {
|
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-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
|
|
|
return {
|
|
|
|
cancel: () => {
|
2020-09-22 10:42:39 +00:00
|
|
|
return this.cancel0()
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|