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()); }
public void addToHistory(DefaultTaskStatus status, JedisTask task) { String historyId = "taskHistory:" + task.getId(); Map<String, String> data = new HashMap<>(); data.put("phase", status.getPhase()); data.put("status", status.getStatus()); data.put("state", status.getState().toString()); String hist; try { hist = mapper.writeValueAsString(data); } catch (JsonProcessingException e) { throw new RuntimeException("Failed converting task history to json", e); } retry(() -> redisClientDelegate.withCommandsClient(client -> { client.rpush(historyId, hist); client.expire(historyId, TASK_TTL); if (status.isCompleted()) { client.srem(RUNNING_TASK_KEY, task.getId()); } }), format("Adding status history to task %s: %s", task.getId(), status)); }
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"))); }