use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.
the class ProgramClient method stopAll.
/**
* Stops all currently running programs.
*/
public void stopAll(NamespaceId namespace) throws IOException, UnauthenticatedException, InterruptedException, TimeoutException, UnauthorizedException, ApplicationNotFoundException, BadRequestException {
List<ApplicationRecord> allApps = applicationClient.list(namespace);
for (ApplicationRecord applicationRecord : allApps) {
ApplicationId appId = new ApplicationId(namespace.getNamespace(), applicationRecord.getName(), applicationRecord.getAppVersion());
List<ProgramRecord> programRecords = applicationClient.listPrograms(appId);
for (ProgramRecord programRecord : programRecords) {
try {
ProgramId program = appId.program(programRecord.getType(), programRecord.getName());
String status = this.getStatus(program);
if (!status.equals("STOPPED")) {
try {
this.stop(program);
} catch (IOException ioe) {
// ProgramClient#stop calls RestClient, which throws an IOException if the HTTP response code is 400,
// which can be due to the program already being stopped when calling stop on it.
// Most likely, there was a race condition that the program stopped between the time we checked its
// status and calling the stop method.
LOG.warn("Program {} is already stopped, proceeding even though the following exception is raised.", program, ioe);
}
this.waitForStatus(program, ProgramStatus.STOPPED, 60, TimeUnit.SECONDS);
}
} catch (ProgramNotFoundException e) {
// IGNORE
}
}
}
}
use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.
the class ProgramClient method getProgramLogs.
/**
* Gets the logs of a program.
*
* @param program the program
* @param start start time of the time range of desired logs
* @param stop end time of the time range of desired logs
* @return the logs of the program
* @throws IOException if a network error occurred
* @throws NotFoundException if the application or program could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public String getProgramLogs(ProgramId program, long start, long stop) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
String path = String.format("apps/%s/%s/%s/logs?start=%d&stop=%d&escape=false", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), start, stop);
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
return new String(response.getResponseBody(), Charsets.UTF_8);
}
use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.
the class ProgramClient method stop.
/**
* Stops a program.
*
* @param programId the program to stop
* @throws IOException if a network error occurred
* @throws ProgramNotFoundException if the program with specified name could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws BadRequestException if an attempt is made to stop a program that is either not running or
* was started by a workflow
*/
public void stop(ProgramId programId) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException, BadRequestException {
String path = String.format("apps/%s/versions/%s/%s/%s/stop", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram());
URL url = config.resolveNamespacedURLV3(programId.getNamespaceId(), path);
// Required to add body even if runtimeArgs is null to avoid 411 error for Http POST
HttpRequest.Builder request = HttpRequest.post(url).withBody("");
HttpResponse response = restClient.execute(request.build(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(programId);
}
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseCode() + ": " + response.getResponseBodyAsString());
}
}
use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.
the class ProgramClient method getRuntimeArgs.
/**
* Gets the runtime args of a program.
*
* @param program the program
* @return runtime args of the program
* @throws IOException if a network error occurred
* @throws ProgramNotFoundException if the application or program could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public Map<String, String> getRuntimeArgs(ProgramId program) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
}).getResponseObject();
}
use of io.cdap.cdap.common.ProgramNotFoundException in project cdap by caskdata.
the class PreferencesClient method getProgramPreferences.
/**
* Returns the Preferences stored at the Program Level.
*
* @param program Program Id
* @param resolved Set to True if collapsed/resolved properties are desired
* @return map of key-value pairs
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws ProgramNotFoundException if the requested program is not found
*/
public Map<String, String> getProgramPreferences(ProgramId program, boolean resolved) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
String res = Boolean.toString(resolved);
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), String.format("/apps/%s/%s/%s/preferences?resolved=%s", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), res));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
}).getResponseObject();
}
Aggregations