Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging when a task is created / updated #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.gov.hmcts.juror.scheduler.mapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import uk.gov.hmcts.juror.scheduler.api.model.task.TaskDetail;
import uk.gov.hmcts.juror.scheduler.datastore.entity.TaskEntity;
import uk.gov.hmcts.juror.standard.service.exceptions.InternalServerException;

import java.util.List;

Expand All @@ -14,4 +16,12 @@ public abstract class TaskMapper {

@Mapping(target = "jobKey", source = "job.key")
public abstract TaskDetail toTask(TaskEntity taskEntity);

public String toTaskJson(ObjectMapper objectMapper, TaskEntity latestTask) {
try {
return objectMapper.writeValueAsString(toTask(latestTask));
} catch (Exception e) {
throw new InternalServerException("Error converting Task to JSON", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public interface TaskService {
void updateStatus(String jobKey, long taskId, StatusUpdate statusUpdate);


void logTaskEntity(TaskEntity task);

void deleteAllByJobKey(String jobKey);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uk.gov.hmcts.juror.scheduler.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -12,6 +14,7 @@
import uk.gov.hmcts.juror.scheduler.datastore.model.Status;
import uk.gov.hmcts.juror.scheduler.datastore.model.filter.TaskSearchFilter;
import uk.gov.hmcts.juror.scheduler.datastore.repository.TaskRepository;
import uk.gov.hmcts.juror.scheduler.mapping.TaskMapper;
import uk.gov.hmcts.juror.scheduler.service.contracts.ActionService;
import uk.gov.hmcts.juror.scheduler.service.contracts.JobService;
import uk.gov.hmcts.juror.scheduler.service.contracts.TaskService;
Expand All @@ -23,21 +26,27 @@
import java.util.Optional;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

private final TaskRepository taskRepository;

private final JobService jobService;

private final ActionService actionService;

private final TaskMapper taskMapper;
private final ObjectMapper objectMapper;

@Autowired
public TaskServiceImpl(@Lazy JobService jobService, TaskRepository taskRepository,
@Lazy ActionService actionService) {
@Lazy ActionService actionService,
TaskMapper taskMapper,
ObjectMapper objectMapper) {
this.taskRepository = taskRepository;
this.jobService = jobService;
this.actionService = actionService;
this.taskMapper = taskMapper;
this.objectMapper = objectMapper;
}


Expand All @@ -52,6 +61,7 @@ public TaskEntity createTask(APIJobDetailsEntity apiJobDetailsEntity) {
@Override
public TaskEntity saveTask(TaskEntity task) {
TaskEntity actionTaskEntity = this.actionService.taskUpdated(task);
logTaskEntity(task);
return taskRepository.saveAndFlush(actionTaskEntity);
}

Expand Down Expand Up @@ -85,7 +95,13 @@ public void updateStatus(String jobKey, long taskId, StatusUpdate statusUpdate)
if (!Collections.isEmpty(statusUpdate.getMetaData())) {
taskEntity.addMetaData(statusUpdate.getMetaData());
}
saveTask(taskEntity);
logTaskEntity(saveTask(taskEntity));
}

@Override
public void logTaskEntity(TaskEntity task) {
log.info("Task status updated for jobKey: {} data: {}",
task.getJob().getKey(), taskMapper.toTaskJson(objectMapper, task));
}

@Override
Expand Down
Loading