class DispatcherJob implements AutoCloseableAsync {

	public CompletableFuture<Acknowledge> cancel(Time timeout) {
		synchronized (lock) {
			if (isInitialized()) {
				return getJobMasterGateway().thenCompose(jobMasterGateway -> jobMasterGateway.cancel(timeout));
			} else {
				log.info("Cancellation during initialization requested for job {}. Job will be cancelled once JobManager has been initialized.", jobId);

				// cancel job
				CompletableFuture<Acknowledge> cancelFuture = jobManagerRunnerFuture
					.thenCompose(JobManagerRunner::getJobMasterGateway)
					.thenCompose(jobMasterGateway -> jobMasterGateway.cancel(timeout));
				cancelFuture.whenComplete((ignored, cancelThrowable) -> {
					if (cancelThrowable != null) {
						log.warn("Cancellation of job {} failed", jobId, cancelThrowable);
					}
				});
				jobStatus = DispatcherJobStatus.CANCELLING;
				return cancelFuture;
			}
		}
	}

	private  DispatcherJob(
			CompletableFuture<JobManagerRunner> jobManagerRunnerFuture,
			JobID jobId,
			String jobName,
			long initializationTimestamp);

}