package be.iminds.ilabt.jfed.highlevel.jobs;

import be.iminds.ilabt.jfed.experiment.tasks.ExperimentTaskStatus;
import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;

import javax.annotation.Nonnull;

/**
 * User: twalcari
 * Date: 11/25/2015
 * Time: 10:12
 */
public abstract class SingleCallState<T extends Task> extends State {
    protected final T task;
    protected TaskExecution<T> taskExecution = null;

    protected SingleCallState(String name, T task) {
        super(name);
        this.task = task;
    }

    protected SingleCallState(T task) {
        super(task.getName());
        this.task = task;
    }

    @Nonnull
    public ExperimentTaskStatus executeState(Job<?> job) throws InterruptedException {
        this.taskExecution = job.submitTaskAndWait(task);


        return taskExecution.getState() == TaskExecution.TaskState.SUCCESS ?
                ExperimentTaskStatus.SUCCESS :
                ExperimentTaskStatus.FAILED;
    }

    public T getTask() {
        return task;
    }

    public TaskExecution<T> getTaskExecution() {
        return taskExecution;
    }
}
