it.infn.ct.futuregateway.apiserver.inframanager.state.Running.java Source code

Java tutorial

Introduction

Here is the source code for it.infn.ct.futuregateway.apiserver.inframanager.state.Running.java

Source

/***********************************************************************
 * Copyright (c) 2015:
 * Istituto Nazionale di Fisica Nucleare (INFN), Italy
 * Consorzio COMETA (COMETA), Italy
 *
 * See http://www.infn.it and and http://www.consorzio-cometa.it for details on
 * the copyright holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***********************************************************************/

package it.infn.ct.futuregateway.apiserver.inframanager.state;

import it.infn.ct.futuregateway.apiserver.inframanager.CustomJobFactory;
import it.infn.ct.futuregateway.apiserver.inframanager.InfrastructureException;
import it.infn.ct.futuregateway.apiserver.resources.Task;
import it.infn.ct.futuregateway.apiserver.storage.Storage;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ogf.saga.error.BadParameterException;
import org.ogf.saga.error.DoesNotExistException;
import org.ogf.saga.error.NoSuccessException;
import org.ogf.saga.error.NotImplementedException;
import org.ogf.saga.error.TimeoutException;
import org.ogf.saga.job.Job;
import org.ogf.saga.task.State;

/**
 * Concrete state <i>Running</i> for the task.
 * When a task is in Running state the associated action will be to check the
 * state of the activity on the remote infrastructure and eventually move to
 * the next state.
 */
public class Running extends TaskState {
    /**
     * Logger object. Based on apache commons logging.
     */
    private final Log log = LogFactory.getLog(Running.class);

    /**
     * Reference to the task.
     */
    private Task task;

    /**
     * Builds the Running concrete state and associates the task.
     * @param aTask The associated task
     */
    public Running(final Task aTask) {
        this.task = aTask;
    }

    @Override
    public final void action(final ExecutorService anExecutorService, final BlockingQueue<Task> aBlockingQueue,
            final Storage aStorage) {

        try {
            final Job job = CustomJobFactory.createJob(this.task, aStorage);
            final State state = job.getState();

            this.task.updateCheckTime();
            switch (state) {
            case DONE:
                this.task.setState(Task.STATE.DONE);
                break;
            case RUNNING:
                try {
                    aBlockingQueue.put(this.task);
                } catch (InterruptedException ex) {
                    this.log.error(ex.getMessage());
                    this.task.setState(Task.STATE.ABORTED);
                }
                break;
            case CANCELED:
                this.task.setState(Task.STATE.CANCELLED);
                break;
            case FAILED:
            case NEW:
            case SUSPENDED:
                this.task.setState(Task.STATE.ABORTED);
                break;
            default:
                this.log.error("Task: " + this.task.getId() + " is in a invalid state: " + state);
                this.task.setState(Task.STATE.ABORTED);
                break;
            }
        } catch (InfrastructureException | BadParameterException | DoesNotExistException | NotImplementedException
                | TimeoutException | NoSuccessException ex) {
            final String msg = ex.getMessage();
            this.log.error("Error checking job status: " + msg);
        }

    }

}