@Override public Task getByClientRequestId(String clientRequestId) { final String clientRequestKey = getClientRequestKey(clientRequestId); String existingTask = retry(() -> redisClientDelegate.withCommandsClient(client -> { return client.get(clientRequestKey); }), format("Getting task by client request ID %s", clientRequestId)); if (existingTask == null) { if (redisClientDelegatePrevious.isPresent()) { try { existingTask = redisClientDelegatePrevious.get().withCommandsClient(client -> { return client.get(clientRequestKey); }); } catch (Exception e) { // Failed to hit old redis, let's not blow up on that existingTask = null; } } } if (existingTask != null) { return get(existingTask); } return null; }
@Override public Task create(String phase, String status, String clientRequestId) { String taskKey = getClientRequestKey(clientRequestId); String taskId = UUID.randomUUID().toString(); JedisTask task = new JedisTask(taskId, System.currentTimeMillis(), this, ClouddriverHostname.ID, false); addToHistory(DefaultTaskStatus.create(phase, status, TaskState.STARTED), task); set(taskId, task); Long newTask = retry(() -> redisClientDelegate.withCommandsClient(client -> { return client.setnx(taskKey, taskId); }), "Registering task with index"); if (newTask != 0) { return task; } // There's an existing taskId for this key, clean up what we just created and get the existing task addToHistory(DefaultTaskStatus.create(phase, "Duplicate of " + clientRequestId, TaskState.FAILED), task); return getByClientRequestId(clientRequestId); }