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')
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
const taskId = new AtomicInteger(0)
|
|
|
|
const tasks: { [key: number]: task.Cancelable } = {}
|
|
|
|
const executor = thread_pool.create({
|
|
|
|
groupName: '@ccms/spring'
|
|
|
|
})
|
2020-05-26 07:53:41 +00:00
|
|
|
|
|
|
|
@provideSingleton(task.TaskManager)
|
|
|
|
export class SpringTaskManager implements task.TaskManager {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
private innerTaskId: any
|
|
|
|
private innerTasks: { [s: string]: task.Cancelable }
|
|
|
|
private innerExecutor: java.util.concurrent.ThreadPoolExecutor
|
|
|
|
|
2020-05-26 07:53:41 +00:00
|
|
|
constructor() {
|
2020-06-20 08:39:03 +00:00
|
|
|
this.innerTaskId = taskId
|
|
|
|
this.innerTasks = tasks
|
|
|
|
this.innerExecutor = executor
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
create(func: Function): task.Task {
|
|
|
|
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !') }
|
|
|
|
return new SpringTask(this.pluginInstance, func)
|
|
|
|
}
|
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return func()
|
|
|
|
}
|
|
|
|
disable() {
|
2020-06-20 08:39:03 +00:00
|
|
|
Object.values(this.innerTasks).forEach((task) => task?.cancel())
|
|
|
|
this.innerExecutor.shutdown()
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SpringTask extends task.Task {
|
2020-06-20 08:39:03 +00:00
|
|
|
public id = taskId.incrementAndGet()
|
2020-05-26 07:53:41 +00:00
|
|
|
private running = new AtomicBoolean(true)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(): any {
|
|
|
|
var wasRunning = this.running.getAndSet(false)
|
|
|
|
if (wasRunning) {
|
2020-06-02 09:50:47 +00:00
|
|
|
delete tasks[this.id]
|
2020-05-26 07:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 09:50:47 +00:00
|
|
|
submit(...args: any[]): task.Cancelable {
|
2020-05-26 07:53:41 +00:00
|
|
|
tasks[this.id] = this
|
2020-06-20 08:39:03 +00:00
|
|
|
executor.execute((() => this.run(...args)) as any)
|
2020-05-26 07:53:41 +00:00
|
|
|
return {
|
|
|
|
cancel: () => {
|
|
|
|
return this.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|