public DefaultTaskStatus currentState(JedisTask task) { String historyId = "taskHistory:" + task.getId(); RedisClientDelegate client = clientForTask(task); String state = retry(() -> client.withCommandsClient(c -> { return c.lindex(historyId, -1); }), format("Getting current state for task %s", task.getId())); Map<String, String> history; try { history = mapper.readValue(state, HISTORY_TYPE); } catch (IOException e) { throw new RuntimeException("Failed converting task history json to object", e); } return DefaultTaskStatus.create(history.get("phase"), history.get("status"), TaskState.valueOf(history.get("state"))); }
public List<Status> getHistory(JedisTask task) { String historyId = "taskHistory:" + task.getId(); RedisClientDelegate client = clientForTask(task); return retry(() -> client.withCommandsClient(c -> { return c.lrange(historyId, 0, -1); }), format("Getting history for task %s", task.getId())) .stream() .map(h -> { Map<String, String> history; try { history = mapper.readValue(h, HISTORY_TYPE); } catch (IOException e) { throw new RuntimeException("Could not convert history json to type", e); } return TaskDisplayStatus.create(DefaultTaskStatus.create(history.get("phase"), history.get("status"), TaskState.valueOf(history.get("state")))); }) .collect(Collectors.toList()); }
@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); }