org.jetlinks.rule.engine.defaults.AbstractTaskExecutor Maven / Gradle / Ivy
package org.jetlinks.rule.engine.defaults;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.jetlinks.rule.engine.api.task.ExecutionContext;
import org.jetlinks.rule.engine.api.task.Task;
import org.jetlinks.rule.engine.api.task.TaskExecutor;
import reactor.core.Disposable;
import java.util.function.BiConsumer;
@Slf4j
public abstract class AbstractTaskExecutor implements TaskExecutor {
@Getter
protected ExecutionContext context;
@Getter
protected Task.State state = Task.State.shutdown;
protected Disposable disposable;
private BiConsumer stateListener = (from, to) -> {
log.debug("task [{}] state changed from {} to {}.",
context.getJob(),
from,
to);
};
public AbstractTaskExecutor(ExecutionContext context) {
this.context = context;
}
@Override
public abstract String getName();
protected abstract Disposable doStart();
protected void changeState(Task.State state) {
stateListener.accept(this.state, this.state = state);
}
@Override
public void start() {
if (state == Task.State.running && !disposable.isDisposed()) {
return;
}
disposable = doStart();
changeState(Task.State.running);
}
@Override
public void reload() {
}
@Override
public void pause() {
changeState(Task.State.paused);
}
@Override
public void shutdown() {
if (disposable != null) {
disposable.dispose();
}
changeState(Task.State.shutdown);
}
@Override
public void onStateChanged(BiConsumer listener) {
this.stateListener = this.stateListener.andThen(listener);
}
@Override
public void validate() {
}
}