use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class WsAgentPingRequestFactory method createRequest.
/**
* Creates request which can check if workspace agent is pinging.
*
* @param machine
* machine instance
* @return instance of {@link HttpJsonRequest}
* @throws ServerException
* if internal server error occurred
*/
public HttpJsonRequest createRequest(Machine machine) throws ServerException {
Map<String, ? extends Server> servers = machine.getRuntime().getServers();
Server wsAgentServer = servers.get(Constants.WS_AGENT_PORT);
if (wsAgentServer == null) {
LOG.error("{} WorkspaceId: {}, DevMachine Id: {}, found servers: {}", WS_AGENT_SERVER_NOT_FOUND_ERROR, machine.getWorkspaceId(), machine.getId(), servers);
throw new ServerException(WS_AGENT_SERVER_NOT_FOUND_ERROR);
}
String wsAgentPingUrl = wsAgentServer.getProperties().getInternalUrl();
if (isNullOrEmpty(wsAgentPingUrl)) {
LOG.error(WS_AGENT_URL_IS_NULL_OR_EMPTY_ERROR);
throw new ServerException(WS_AGENT_URL_IS_NULL_OR_EMPTY_ERROR);
}
// we will always obtain not found response
if (!wsAgentPingUrl.endsWith("/")) {
wsAgentPingUrl = wsAgentPingUrl.concat("/");
}
return httpJsonRequestFactory.fromUrl(wsAgentPingUrl).setMethod(HttpMethod.GET).setTimeout(wsAgentPingConnectionTimeoutMs);
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class CheEnvironmentEngine method addMachine.
private void addMachine(MachineImpl machine) throws ServerException {
Instance instance = new NoOpMachineInstance(machine);
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(machine.getWorkspaceId())) {
ensurePreDestroyIsNotExecuted();
EnvironmentHolder environmentHolder = environments.get(machine.getWorkspaceId());
if (environmentHolder != null && environmentHolder.status != EnvStatus.STOPPING) {
environmentHolder.machines.add(instance);
} else {
throw new ServerException(format("Can't add machine into environment. Environment of workspace '%s' is missing", machine.getWorkspaceId()));
}
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class CheEnvironmentEngine method replaceMachine.
private void replaceMachine(Instance machine) throws ServerException {
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.writeLock(machine.getWorkspaceId())) {
ensurePreDestroyIsNotExecuted();
EnvironmentHolder environmentHolder = environments.get(machine.getWorkspaceId());
if (environmentHolder != null) {
for (int i = 0; i < environmentHolder.machines.size(); i++) {
if (environmentHolder.machines.get(i).getId().equals(machine.getId())) {
environmentHolder.machines.set(i, machine);
return;
}
}
}
}
// if this area is reachable then environment/machine is not found and machine should be stopped
try {
machine.destroy();
} catch (MachineException e) {
LOG.error(e.getLocalizedMessage(), e);
}
// should not happen
throw new ServerException(format("Machine with ID '%s' and name '%s' has been stopped because its configuration is not found in the environment of workspace '%s'", machine.getId(), machine.getConfig().getName(), machine.getWorkspaceId()));
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class CheEnvironmentEngine method startInstance.
private Instance startInstance(boolean recover, MessageConsumer<MachineLogMessage> environmentLogger, MachineImpl machine, MachineStarter machineStarter) throws ServerException, EnvironmentException {
LineConsumer machineLogger = null;
Instance instance = null;
try {
addMachine(machine);
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
machineLogger = getMachineLogger(environmentLogger, machine.getId(), machine.getConfig().getName());
MachineImpl originMachine = new MachineImpl(machine);
try {
MachineSourceImpl machineSource = null;
if (recover) {
try {
SnapshotImpl snapshot = snapshotDao.getSnapshot(machine.getWorkspaceId(), machine.getEnvName(), machine.getConfig().getName());
machineSource = snapshot.getMachineSource();
// Snapshot image location has SHA-256 digest which needs to be removed,
// otherwise it will be pulled without tag and cause problems
String imageName = machineSource.getLocation();
if (imageName.contains("@sha256:")) {
machineSource.setLocation(imageName.substring(0, imageName.indexOf('@')));
}
} catch (NotFoundException e) {
try {
machineLogger.writeLine("Failed to boot machine from snapshot: snapshot not found. " + "Machine will be created from origin source.");
} catch (IOException ignore) {
}
}
}
instance = machineStarter.startMachine(machineLogger, machineSource);
} catch (SourceNotFoundException e) {
if (recover) {
LOG.error("Image of snapshot for machine " + machine.getConfig().getName() + " not found. " + "Machine will be created from origin source.");
machine = originMachine;
instance = machineStarter.startMachine(machineLogger, null);
} else {
throw e;
}
}
replaceMachine(instance);
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(machine.getWorkspaceId()));
return instance;
} catch (ApiException | RuntimeException e) {
boolean interrupted = Thread.interrupted();
removeMachine(machine.getWorkspaceId(), machine.getId());
if (instance != null) {
try {
instance.destroy();
} catch (Exception destroyingExc) {
LOG.error(destroyingExc.getLocalizedMessage(), destroyingExc);
}
}
if (machineLogger != null) {
try {
machineLogger.writeLine("[ERROR] " + e.getLocalizedMessage());
} catch (IOException ioEx) {
LOG.error(ioEx.getLocalizedMessage(), ioEx);
}
try {
machineLogger.close();
} catch (IOException ioEx) {
LOG.error(ioEx.getLocalizedMessage(), ioEx);
}
}
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.ERROR).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
if (interrupted) {
Thread.currentThread().interrupt();
}
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.core.ServerException in project che by eclipse.
the class CheEnvironmentEngine method queuePeekOrFail.
/**
* Gets head config from the queue associated with the given {@code workspaceId}.
*
* <p>Note that this method won't actually poll the queue.
*
* <p>Fails if environment start was interrupted by stop(queue doesn't exist).
*
* @return machine config which is in the queue head, or null
* if there are no machine configs left
* @throws ServerException
* if queue doesn't exist which means that {@link #stop(String)} executed
* before all the machines started
* @throws ServerException
* if pre destroy has been invoked before peek config retrieved
*/
private String queuePeekOrFail(String workspaceId) throws ServerException {
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
ensurePreDestroyIsNotExecuted();
EnvironmentHolder environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.startQueue == null) {
throw new ServerException("Workspace " + workspaceId + " start interrupted. Workspace was stopped before all its machines were started");
}
return environmentHolder.startQueue.peek();
}
}
Aggregations