@Override public Task get(String id) { Map<String, String> taskMap = retry(() -> redisClientDelegate.withCommandsClient(client -> { return client.hgetAll("task:" + id); }), format("Getting task ID %s", id)); boolean oldTask = redisClientDelegatePrevious.isPresent() && (taskMap == null || taskMap.isEmpty()); if (oldTask) { try { taskMap = redisClientDelegatePrevious.get().withCommandsClient(client -> { return client.hgetAll("task:" + id); }); } catch (Exception e) { // Failed to hit old redis, let's not blow up on that return null; } } if (taskMap.containsKey("id") && taskMap.containsKey("startTimeMs")) { return new JedisTask( taskMap.get("id"), Long.parseLong(taskMap.get("startTimeMs")), this, taskMap.get("ownerId"), oldTask ); } 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); }