package automately.core.data; import com.hazelcast.nio.ObjectDataInput; import com.hazelcast.nio.ObjectDataOutput; import io.jsync.app.core.persistence.IdentifiedDataType; 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 IdentifiedDataType { /** * This is the configuration for the job. */ public JsonObject config = new JsonObject(); // By default results are empty; /** * 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; /** * The status field defines the current "action" that is being executed for the job. */ public String status = "queued"; // This is the default job status /** * 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(service); dataOutput.writeUTF(serviceName); dataOutput.writeObject(serviceConfig); dataOutput.writeUTF(userToken); dataOutput.writeUTF(status); dataOutput.writeObject(results); dataOutput.writeObject(created); dataOutput.writeObject(updated); } @Override public void readData(ObjectDataInput dataInput) throws IOException { token = dataInput.readUTF(); config = dataInput.readObject(); service = dataInput.readBoolean(); serviceName = dataInput.readUTF(); serviceConfig = dataInput.readObject(); userToken = dataInput.readUTF(); status = dataInput.readUTF(); results = dataInput.readObject(); created = dataInput.readObject(); updated = 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.results = json.getObject("results", this.results); this.config = json.getObject("config", this.config); 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); // THIS IS REQUIRED 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())); // THIS IS REQUIRED 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())); } } @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.removeField("factoryId"); ret.removeField("id"); return ret; } @Override public int getFactoryId() { return IdentifiedDataTypeFactory.FACTORY_ID; } @Override public int getId() { return IdentifiedDataTypeFactory.JOB_TYPE; } }