Search in sources :

Example 1 with SaturnJobException

use of com.vip.saturn.job.exception.SaturnJobException in project Saturn by vipshop.

the class UpdateJobCronUtils method updateJobCron.

/**
 * Send update job cron request to UpdateJobCron API in Console.
 */
public static void updateJobCron(String namespace, String jobName, String cron, Map<String, String> customContext) throws SaturnJobException {
    for (int i = 0, size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size(); i < size; i++) {
        String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
        String targetUrl = consoleUri + "/rest/v1/" + namespace + "/jobs/" + jobName + "/cron";
        LogUtils.info(log, jobName, "update job cron of domain {} to url {}: {}, retry count: {}", namespace, targetUrl, cron, i);
        CloseableHttpClient httpClient = null;
        try {
            checkParameters(cron);
            Map<String, String> bodyEntity = Maps.newHashMap();
            if (customContext != null) {
                bodyEntity.putAll(customContext);
            }
            bodyEntity.put("cron", cron);
            String json = JsonUtils.toJson(bodyEntity, new TypeToken<Map<String, String>>() {
            }.getType());
            // prepare
            httpClient = HttpClientBuilder.create().build();
            HttpPut request = new HttpPut(targetUrl);
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
            request.setConfig(requestConfig);
            StringEntity params = new StringEntity(json);
            request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            request.setEntity(params);
            CloseableHttpResponse httpResponse = httpClient.execute(request);
            HttpUtils.handleResponse(httpResponse);
            return;
        } catch (SaturnJobException se) {
            LogUtils.error(log, jobName, "SaturnJobException throws: {}", se.getMessage(), se);
            throw se;
        } catch (ConnectException e) {
            LogUtils.error(log, jobName, "Fail to connect to url:{}, throws: {}", targetUrl, e.getMessage(), e);
            if (i == size - 1) {
                throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, "no available console server", e);
            }
        } catch (Exception e) {
            LogUtils.error(log, jobName, "Other exception throws: {}", e.getMessage(), e);
            throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, e.getMessage(), e);
        } finally {
            HttpUtils.closeHttpClientQuietly(httpClient);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 2 with SaturnJobException

use of com.vip.saturn.job.exception.SaturnJobException in project Saturn by vipshop.

the class RestartAndDumpService method validateFile.

private void validateFile(String name, String value) throws SaturnJobException {
    validateConfigured(name, value);
    File file = new File(value);
    if (!file.exists()) {
        throw new SaturnJobException(value + " is not existing");
    }
    if (!file.isFile()) {
        throw new SaturnJobException(value + " is not file");
    }
}
Also used : SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) File(java.io.File)

Example 3 with SaturnJobException

use of com.vip.saturn.job.exception.SaturnJobException in project Saturn by vipshop.

the class AlarmUtils method raiseAlarm.

/**
 * Send alarm request to Alarm API in Console.
 */
public static void raiseAlarm(Map<String, Object> alarmInfo, String namespace) throws SaturnJobException {
    int size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size();
    for (int i = 0; i < size; i++) {
        String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
        String targetUrl = consoleUri + "/rest/v1/" + namespace + "/alarms/raise";
        LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "raise alarm of domain {} to url {}: {}, retry count: {}", namespace, targetUrl, alarmInfo.toString(), i);
        CloseableHttpClient httpClient = null;
        try {
            checkParameters(alarmInfo);
            // prepare
            httpClient = HttpClientBuilder.create().build();
            HttpPost request = new HttpPost(targetUrl);
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
            request.setConfig(requestConfig);
            StringEntity params = new StringEntity(JsonUtils.getGson().toJson(alarmInfo, new TypeToken<Map<String, Object>>() {
            }.getType()));
            request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            request.setEntity(params);
            // send request
            CloseableHttpResponse httpResponse = httpClient.execute(request);
            // handle response
            HttpUtils.handleResponse(httpResponse);
            return;
        } catch (SaturnJobException se) {
            LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, "SaturnJobException throws: {}", se.getMessage(), se);
            throw se;
        } catch (ConnectException e) {
            LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, "Fail to connect to url:{}, throws: {}", targetUrl, e.getMessage(), e);
            if (i == size - 1) {
                throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, "no available console server", e);
            }
        } catch (Exception e) {
            LogUtils.error(log, LogEvents.ExecutorEvent.COMMON, "Other exception throws: {}", e.getMessage(), e);
            throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, e.getMessage(), e);
        } finally {
            HttpUtils.closeHttpClientQuietly(httpClient);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Map(java.util.Map) SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 4 with SaturnJobException

use of com.vip.saturn.job.exception.SaturnJobException in project Saturn by vipshop.

the class HttpUtils method handleResponse.

public static void handleResponse(CloseableHttpResponse httpResponse) throws IOException, SaturnJobException {
    int status = httpResponse.getStatusLine().getStatusCode();
    if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
        return;
    }
    if (status >= HttpStatus.SC_BAD_REQUEST && status <= HttpStatus.SC_INTERNAL_SERVER_ERROR) {
        String responseBody = EntityUtils.toString(httpResponse.getEntity());
        if (StringUtils.isNotBlank(responseBody)) {
            JsonElement message = JsonUtils.getJsonParser().parse(responseBody).getAsJsonObject().get("message");
            String errMsg = message == JsonNull.INSTANCE || message == null ? "" : message.getAsString();
            throw new SaturnJobException(SaturnJobException.ILLEGAL_ARGUMENT, errMsg);
        } else {
            throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, "internal server error");
        }
    } else {
        // if have unexpected status, then throw RuntimeException directly.
        String errMsg = "unexpected status returned from Saturn Server.";
        throw new SaturnJobException(SaturnJobException.SYSTEM_ERROR, errMsg);
    }
}
Also used : SaturnJobException(com.vip.saturn.job.exception.SaturnJobException) JsonElement(com.google.gson.JsonElement)

Aggregations

SaturnJobException (com.vip.saturn.job.exception.SaturnJobException)4 ConnectException (java.net.ConnectException)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 StringEntity (org.apache.http.entity.StringEntity)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 JsonElement (com.google.gson.JsonElement)1 TypeToken (com.google.gson.reflect.TypeToken)1 File (java.io.File)1 Map (java.util.Map)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1