public T fromAsyncJob(AsyncJob<S> in) { return this .accountId(in.getAccountId()) .cmd(in.getCmd()) .created(in.getCreated()) .id(in.getId()) .instanceId(in.getInstanceId()) .instanceType(in.getInstanceType()) .progress(in.getProgress()) .result(in.getResult()) .resultCode(in.getResultCode()) .resultType(in.getResultType()) .status(in.getStatus()) .userId(in.getUserId()) .error(in.getError()); }
public boolean apply(String jobId) { logger.trace(">> looking for status on job %s", checkNotNull(jobId, "jobId")); AsyncJob<?> job = refresh(jobId); if (job == null) { return false; } logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus()); if (job.hasFailed()) { throw new AsyncJobException(String.format("job %s failed with exception %s", job.toString(), job.getError().toString())); } return job.hasSucceed(); }
public boolean hasSucceed() { return getError() == null && getResultCode() == ResultCode.SUCCESS && getStatus() == Status.SUCCEEDED; }
private void checkJob(AsyncJob<?> query) { assert query.getStatus().code() >= 0 : query; assert query.getResultCode().code() >= 0 : query; assert query.getProgress() >= 0 : query; if (query.getResultCode() == ResultCode.SUCCESS) { if (query.getResult() != null) { assertEquals(query.getResult().getClass().getPackage(), AsyncJob.class.getPackage()); } } else if (query.getResultCode() == ResultCode.FAIL) { assert query.getResult() == null : query; assert query.getError() != null : query; } else { assert query.getResult() == null : query; } }
@Test(enabled = true) public void testListAsyncJobs() throws Exception { Set<AsyncJob<?>> response = client.getAsyncJobClient().listAsyncJobs(); assert null != response; long asyncJobCount = response.size(); assertTrue(asyncJobCount >= 0); for (AsyncJob<?> asyncJob : response) { assert asyncJob.getCmd() != null : asyncJob; assert asyncJob.getUserId() != null : asyncJob; checkJob(asyncJob); AsyncJob<?> query = client.getAsyncJobClient().getAsyncJob(asyncJob.getId()); assertEquals(query.getId(), asyncJob.getId()); assert query.getResultType() != null : query; checkJob(query); } }
public AsyncJob<?> apply(AsyncJob<Map<String, JsonBall>> toParse) { AsyncJob<?> result = toParse; if (toParse.getResult() != null) { if (toParse.getResult().size() == 1) { @SuppressWarnings({"unchecked", "rawtypes"}) Builder<?,Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse); if (toParse.getResult().containsKey("success")) { builder.result(null); } else { Entry<String, JsonBall> entry = Iterables.get(toParse.getResult().entrySet(), 0); if ("template".equals(entry.getKey())) { } else if (toParse.getResult().containsKey("errorcode")) { @SuppressWarnings({"unchecked", "rawtypes"}) Builder<?, Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse); builder.error(AsyncJobError.builder().errorCode(ErrorCode.fromValue(toParse.getResult().get("errorcode").toString())) .errorText(toParse.getResult().containsKey("errortext") ? toParse.getResult().get("errortext").toString().replace("\"", "") : null) .build()); result = builder.build(); } else if (toParse.getResult().size() > 1) { logger.warn("unexpected size of async job result; expecting a map with a single element", toParse.getResult());
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobClient().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
AsyncJob<Object> job = getAsyncJobClient().getAsyncJob(jobId); LOG.debug("waiting: " + job); if (job.hasFailed()) throw new IllegalStateException("Failed job: " + job); if (job.hasSucceed()) { Status status = job.getStatus(); if (Status.FAILED.equals(status)) throw new IllegalStateException("Failed job: " + job); if (Status.SUCCEEDED.equals(status)) return responseId;
public AsyncJob<Object> waitForJobSuccess(String job) { AsyncJob<Object> result = waitForJobDone(job); if (result.hasSucceed()) { return result; } else { throw new IllegalStateException(String.format("job %s failed: %s", job, result)); } }
@Test public void testJobComplete() { AsyncJob<?> job = AsyncJob.builder().id("100") .status(Status.SUCCEEDED).resultCode(ResultCode.SUCCESS).build(); expect((Object) asyncJobClient.getAsyncJob(job.getId())).andReturn(job); replay(client, asyncJobClient); assertTrue(new JobComplete(client).apply(job.getId())); verify(client, asyncJobClient); }
public AsyncJob<Object> waitForJobDone(String job) { do { AsyncJob<Object> j = getAsyncJobClient().getAsyncJob(job); if (j.getStatus() != Status.IN_PROGRESS) return j; LOG.debug("cloudstack waiting on job " + job + ": " + j); } while (true); }
public boolean hasFailed() { return getError() != null || getResultCode() == ResultCode.FAIL || getStatus() == Status.FAILED; }
private void checkJob(AsyncJob<?> query) { assert query.getStatus().code() >= 0 : query; assert query.getResultCode().code() >= 0 : query; assert query.getProgress() >= 0 : query; if (query.getResultCode() == ResultCode.SUCCESS) { if (query.getResult() != null) { assertEquals(query.getResult().getClass().getPackage(), AsyncJob.class.getPackage()); } } else if (query.getResultCode() == ResultCode.FAIL) { assert query.getResult() == null : query; assert query.getError() != null : query; } else { assert query.getResult() == null : query; } }
@Test(enabled = true) public void testListAsyncJobs() throws Exception { Set<AsyncJob<?>> response = client.getAsyncJobApi().listAsyncJobs(); assert null != response; long asyncJobCount = response.size(); assertTrue(asyncJobCount >= 0); for (AsyncJob<?> asyncJob : response) { assert asyncJob.getCmd() != null : asyncJob; assert asyncJob.getUserId() != null : asyncJob; checkJob(asyncJob); AsyncJob<?> query = client.getAsyncJobApi().getAsyncJob(asyncJob.getId()); assertEquals(query.getId(), asyncJob.getId()); assert query.getResultType() != null : query; checkJob(query); } }
public AsyncJob<?> apply(AsyncJob<Map<String, JsonBall>> toParse) { AsyncJob<?> result = toParse; if (toParse.getResult() != null) { if (toParse.getResult().size() == 1) { @SuppressWarnings({"unchecked", "rawtypes"}) Builder<?,Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse); if (toParse.getResult().containsKey("success")) { builder.result(null); } else { Entry<String, JsonBall> entry = Iterables.get(toParse.getResult().entrySet(), 0); if ("template".equals(entry.getKey())) { } else if (toParse.getResult().containsKey("errorcode")) { @SuppressWarnings({"unchecked", "rawtypes"}) Builder<?, Object> builder = AsyncJob.Builder.fromAsyncJobUntyped((AsyncJob) toParse); builder.error(AsyncJobError.builder().errorCode(ErrorCode.fromValue(toParse.getResult().get("errorcode").toString())) .errorText(toParse.getResult().containsKey("errortext") ? toParse.getResult().get("errortext").toString().replace("\"", "") : null) .build()); result = builder.build(); } else if (toParse.getResult().size() > 1) { logger.warn("unexpected size of async job result; expecting a map with a single element", toParse.getResult());
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobClient().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
public AsyncJob<Object> waitForJob(String job) { AsyncJob<Object> result = waitForJobDone(job); if (!result.hasSucceed()) { LOG.warn("job {} failed: {}", job, result); } return result; }
@Test public void testJobComplete() { AsyncJob<?> job = AsyncJob.builder().id("100") .status(Status.SUCCEEDED).resultCode(ResultCode.SUCCESS).build(); expect((Object) asyncJobClient.getAsyncJob(job.getId())).andReturn(job); replay(client, asyncJobClient); assertTrue(new JobComplete(client).apply(job.getId())); verify(client, asyncJobClient); }
public T fromAsyncJob(AsyncJob<S> in) { return this .accountId(in.getAccountId()) .cmd(in.getCmd()) .created(in.getCreated()) .id(in.getId()) .instanceId(in.getInstanceId()) .instanceType(in.getInstanceType()) .progress(in.getProgress()) .result(in.getResult()) .resultCode(in.getResultCode()) .resultType(in.getResultType()) .status(in.getStatus()) .userId(in.getUserId()) .error(in.getError()); }
public boolean apply(String jobId) { logger.trace(">> looking for status on job %s", checkNotNull(jobId, "jobId")); AsyncJob<?> job = refresh(jobId); if (job == null) { return false; } logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus()); if (job.hasFailed()) { throw new AsyncJobException(String.format("job %s failed with exception %s", job.toString(), job.getError().toString())); } return job.hasSucceed(); }