All Downloads are FREE. Search and download functionalities are using the official Maven repository.

automately.core.data.Job Maven / Gradle / Ivy

package automately.core.data;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import io.jcluster.core.data.DataType;
import io.jsync.json.JsonObject;
import io.jsync.json.impl.Json;
import io.jsync.utils.Token;

import java.io.IOException;
import java.util.Date;

/**
 * The Job class is a DataType used by JCluster.
 * It is used to store information about Automately jobs in the cluster.
 */
public class Job extends DataType {

    /**
     * This is the configuration for the job.
     */
    public JsonObject config = new JsonObject(); // By default results are empty;
    /**
     * A lite job is a job that can be executed from the SDK MessageBus.
     * When this is true the server will treat the job as a lite job.
     */
    public boolean lite = false;
    /**
     * The serverTag is a JsonObject that allows you to define rules that the JobServer
     * will follow only if dedicated is set to true. JobServers's can be dedicated to specific
     * users for faster execution. A simple tag would consist of {"username": "admin"} and the
     * JobServer will route that job to any job server with that tag. You can even go as far
     * as supporting specific objects in the cluster. Like this {"supportsTesserect": true}
     */
    public JsonObject serverTag = null; // Example: new JsonObject().putString("nodeId", "*");
    /**
     * A service job is a job that can pretty much be ran until it is stopped. It does not have a timeout
     * like a regular job. Service jobs also have more resources available to them.
     */
    public boolean service = false;
    /**
     * Your serviceName basically defines the service in the cluster.
     * You can only have a single service running with the same name. This
     * makes it easier to manage things. You can always create random
     * service names to make things easier
     */
    public String serviceName = "";
    /**
     * serviceConfig is a viariable that stores a JsonObject that can be used to define some useful
     * variables in a service job. These variables can be accessed by the scripts executing the jobs.
     */
    public JsonObject serviceConfig = new JsonObject();
    /**
     * The userToken defines which user this job belongs to.
     */
    public String userToken;
    /**
     * fileToken specifies the script you wish to execute. Can be null since you can define that in the config.
     */
    public String fileToken;
    /**
     * The status field defines the current "action" that is being executed for the job.
     */
    public String status = "queued"; // This is the default job status @todo we could probably use an Enum here if it translates well in the js context
    /**
     * The results of the job basically stores certain information about the execution.
     */
    public JsonObject results = new JsonObject();
    /**
     * This field basically defines when this job was created.
     */
    public Date created = new Date();
    /**
     * This field defines when the job was last updated
     */
    public Date updated = new Date();

    private String token = Token.generateToken().toHex();

    @Override
    public void writeData(ObjectDataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(token);
        dataOutput.writeObject(config);
        dataOutput.writeBoolean(lite);
        dataOutput.writeBoolean(service);
        dataOutput.writeUTF(serviceName);
        dataOutput.writeObject(serviceConfig);
        dataOutput.writeUTF(userToken);
        dataOutput.writeUTF(fileToken);
        dataOutput.writeUTF(status);
        dataOutput.writeObject(results);
        dataOutput.writeObject(created);
        dataOutput.writeObject(updated);
        dataOutput.writeObject(serverTag);
    }

    @Override
    public void readData(ObjectDataInput dataInput) throws IOException {
        token = dataInput.readUTF();
        config = dataInput.readObject();
        lite = dataInput.readBoolean();
        service = dataInput.readBoolean();
        serviceName = dataInput.readUTF();
        serviceConfig = dataInput.readObject();
        userToken = dataInput.readUTF();
        fileToken = dataInput.readUTF();
        status = dataInput.readUTF();
        results = dataInput.readObject();
        created = dataInput.readObject();
        updated = dataInput.readObject();
        serverTag = dataInput.readObject();
    }

    /**
     * The token() method will return a token for the Job.
     * @return
     */
    public String token() {
        return token;
    }

    @Override
    public void loadJson(JsonObject json) {
        if (json != null) {
            this.token = json.getString("token", this.token);
            this.userToken = json.getString("userToken", this.userToken);
            this.fileToken = json.getString("fileToken", this.fileToken);
            this.results = json.getObject("results", this.results);
            this.config = json.getObject("config", this.config);
            this.lite = json.getBoolean("lite", this.lite);
            this.service = json.getBoolean("service", this.service);
            this.serviceName = json.getString("serviceName", this.serviceName);
            this.serviceConfig.getObject("serviceConfig", this.serviceConfig);
            this.status = json.getString("status", this.status);

            // For some reason data didn't work out too well
            if(json.getValue("created") instanceof JsonObject &&
                    json.getObject("created", new JsonObject()).containsField("$numberLong")){
                json.putValue("created", Long.valueOf(json.getObject("created", new JsonObject()).getValue("$numberLong")));
            }
            this.created = new Date(json.getLong("created", created.getTime()));

            // For some reason data didn't work out too well
            if(json.getValue("updated") instanceof JsonObject &&
                    json.getObject("updated", new JsonObject()).containsField("$numberLong")){
                json.putValue("updated", Long.valueOf(json.getObject("updated", new JsonObject()).getValue("$numberLong")));
            }
            this.updated = new Date(json.getLong("updated", updated.getTime()));
            this.serverTag = json.getObject("serverTag", serverTag);
        }
    }

    @Override
    public JsonObject toJson() {
        JsonObject ret = new JsonObject(Json.encode(this));
        ret.putNumber("created", created.getTime());
        ret.putNumber("updated", updated.getTime());
        ret.putString("token", token);
        ret.putObject("config", config);
        ret.putObject("results", results);
        ret.putObject("serviceConfig", serviceConfig);
        ret.putObject("serverTag", serverTag);
        return ret;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy