Skip to content

Commit

Permalink
Added Logger for Job service (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimaaBetah authored May 15, 2024
1 parent 2989bef commit 43efcc4
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 12 deletions.
25 changes: 25 additions & 0 deletions services/jobs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,30 @@
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.workup</groupId>
Expand Down Expand Up @@ -80,6 +101,10 @@
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -19,6 +20,14 @@ public static void main(String[] args) {
SpringApplication.run(JobsApplication.class, args);
}

@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> {
System.out.println("ApplicationRunner is executing");
// Configurator.setLevel("com.workup.jobs", org.apache.logging.log4j.Level.ERROR);
};
}

@Bean
public Queue myQueue() {
return new Queue(ServiceQueueNames.JOBS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class AcceptProposalCommand
extends JobCommand<AcceptProposalRequest, AcceptProposalResponse> {
private static final Logger logger = LogManager.getLogger(AcceptProposalCommand.class);

@Override
public AcceptProposalResponse Run(AcceptProposalRequest request) {
logger.info("[x] Accepting proposal with id: " + request.getProposalId());
try {
UUID proposalId = UUID.fromString(request.getProposalId());
ProposalPrimaryKey proposalPrimaryKey =
ProposalPrimaryKey.builder().withId(proposalId).withJobId(request.getJobId()).build();
Optional<Proposal> proposals = proposalRepository.findById(proposalPrimaryKey);
if (proposals.isEmpty()) {
logger.info("[x] Proposal not found with id: " + proposalId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Proposal not found")
.build();
}
Proposal acceptedProposal = proposals.get();
if (acceptedProposal.getStatus() != ProposalStatus.PENDING) {
logger.info("[x] Proposal is not pending with id: " + proposalId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Proposal is not pending")
Expand All @@ -45,19 +51,22 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) {
UUID jobId = UUID.fromString(request.getJobId());
Optional<Job> job = jobRepository.findById(jobId);
if (job.isEmpty()) {
logger.info("[x] Job not found with id: " + jobId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Job not found")
.build();
}
Job acceptedJob = job.get();
if (!acceptedJob.isActive()) {
logger.info("[x] Job is no longer active with id: " + jobId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("This job is no longer active!")
.build();
}
if (!acceptedJob.getClientId().equals(request.getUserId())) {
logger.info("[x] User is not the owner of this proposal's job with id: " + jobId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.UNAUTHORIZED)
.withErrorMessage("User is not the owner of this proposal's job!")
Expand All @@ -67,6 +76,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) {
proposalRepository.save(acceptedProposal);
jobRepository.save(acceptedJob);
String contractId = initiateContract(acceptedProposal, acceptedJob, request);
logger.info("[x] Contract Initiated with id" + contractId);
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withJob(AcceptedJobInfo.builder().withId(request.getJobId()).withIsActive(false).build())
Expand All @@ -79,7 +89,7 @@ public AcceptProposalResponse Run(AcceptProposalRequest request) {
.withContractId(contractId)
.build();
} catch (Exception e) {
e.printStackTrace();
logger.error("[x] An error occurred while accepting job proposal", e.getMessage());
return AcceptProposalResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while accepting job proposal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import com.workup.shared.enums.HttpStatusCode;
import java.util.Date;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CreateJobCommand extends JobCommand<CreateJobRequest, CreateJobResponse> {
private static final Logger logger = LogManager.getLogger(CreateJobCommand.class);

@Override
public CreateJobResponse Run(CreateJobRequest request) {
logger.info("[x] Saving job '" + request.getTitle() + "'");
try {
Job job =
Job.builder()
Expand All @@ -27,13 +31,13 @@ public CreateJobResponse Run(CreateJobRequest request) {
.withUpdatedAt(new Date())
.build();
Job savedJob = jobRepository.save(job);
System.out.println(" [x] Saved Job '" + savedJob.getTitle());
logger.info("[x] Job saved with id: " + savedJob.getId());
return CreateJobResponse.builder()
.withStatusCode(HttpStatusCode.CREATED)
.withJobId(savedJob.getId().toString())
.build();
} catch (Exception e) {
e.printStackTrace();
logger.error("[x] An error occurred while saving job", e.getMessage());
return CreateJobResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while saving job")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CreateProposalCommand
extends JobCommand<CreateProposalRequest, CreateProposalResponse> {
private static final Logger logger = LogManager.getLogger(CreateProposalCommand.class);

@Override
public CreateProposalResponse Run(CreateProposalRequest request) {
logger.info("[x] Saving proposal for job '" + request.getJobId() + "'");
try {
Optional<Job> job = jobRepository.findById(UUID.fromString(request.getJobId()));
if (!job.isPresent()) {
Expand Down Expand Up @@ -66,14 +70,13 @@ public CreateProposalResponse Run(CreateProposalRequest request) {
.withStatus(ProposalStatus.PENDING)
.build();
Proposal savedProposal = proposalRepository.save(proposal);
System.out.println(" [x] Saved Proposal '" + savedProposal.getAttachments());
System.out.println(" [x] Saved Proposal '" + savedProposal.getMilestones());
logger.info("[x] Proposal saved with id: " + savedProposal.getPrimaryKey().getId());
return CreateProposalResponse.builder()
.withStatusCode(HttpStatusCode.CREATED)
.withId(savedProposal.getPrimaryKey().getId().toString())
.build();
} catch (Exception e) {
e.printStackTrace();
logger.error("[x] An error occurred while saving proposal", e.getMessage());
return CreateProposalResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while saving proposal")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import com.workup.shared.enums.HttpStatusCode;
import java.util.Optional;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GetJobByIdCommand extends JobCommand<GetJobByIdRequest, GetJobByIdResponse> {
private static final Logger logger = LogManager.getLogger(GetJobByIdCommand.class);

@Override
public GetJobByIdResponse Run(GetJobByIdRequest request) {
logger.info("[x] Fetching job with id: " + request.getJobId());
try {
UUID jobId = UUID.fromString(request.getJobId());

Optional<Job> job = jobRepository.findById(jobId);
if (job.isPresent()) {
logger.info("[x] Job found with id: " + jobId);
return GetJobByIdResponse.builder()
.withId(job.get().getId().toString())
.withTitle(job.get().getTitle())
Expand All @@ -31,12 +36,15 @@ public GetJobByIdResponse Run(GetJobByIdRequest request) {
.withStatusCode(HttpStatusCode.OK)
.build();
} else {
logger.info("[x] Job not found with id: " + jobId);
return GetJobByIdResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Job not found")
.build();
}
} catch (Exception e) {
logger.error("[x] An error occurred while fetching job", e.getMessage());

return GetJobByIdResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while fetching job")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import com.workup.shared.commands.jobs.responses.GetMyJobsResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GetMyJobsCommand extends JobCommand<GetMyJobsRequest, GetMyJobsResponse> {
private static final Logger logger = LogManager.getLogger(GetMyJobsCommand.class);

@Override
public GetMyJobsResponse Run(GetMyJobsRequest request) {

try {
String clientId = request.getUserId();
List<Job> jobs = jobRepository.getJobsByClientId(clientId);
logger.info("[x] Jobs fetched for client: " + clientId);
return GetMyJobsResponse.builder()
.withJobs(
jobs.stream()
Expand All @@ -30,6 +35,7 @@ public GetMyJobsResponse Run(GetMyJobsRequest request) {
.withStatusCode(HttpStatusCode.OK)
.build();
} catch (Exception e) {
logger.error("[x] An error occurred while fetching jobs", e.getMessage());
return GetMyJobsResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while fetching jobs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import com.workup.shared.enums.HttpStatusCode;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GetMyProposalsCommand
extends JobCommand<GetMyProposalsRequest, GetMyProposalsResponse> {
private static final Logger logger = LogManager.getLogger(GetMyProposalsCommand.class);

@Override
public GetMyProposalsResponse Run(GetMyProposalsRequest request) {
logger.info("[x] Fetching proposals for freelancer " + request.getUserId());
try {
System.out.println(" [x] Fetching proposals for freelancer " + request.getUserId());
List<Proposal> response = proposalRepository.findByFreelancerId(request.getUserId());
System.out.println(" [x] Found " + response.size() + " proposals");

logger.info(
"[x] Found " + response.size() + " proposals for freelancer " + request.getUserId());
List<ProposalModel> proposals = new ArrayList<>();
for (Proposal proposal : response) {
ArrayList<ProposalAttachment> attachments = new ArrayList<>();
Expand Down Expand Up @@ -59,11 +62,14 @@ public GetMyProposalsResponse Run(GetMyProposalsRequest request) {
.withMilestones(milestones)
.build());
}
logger.info(
"[x] Returning " + proposals.size() + " proposals for freelancer " + request.getUserId());
return GetMyProposalsResponse.builder()
.withProposals(proposals)
.withStatusCode(HttpStatusCode.OK)
.build();
} catch (Exception e) {
logger.error("[x] An error occurred while fetching proposals", e.getMessage());
return GetMyProposalsResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while fetching proposals")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
import com.workup.shared.enums.HttpStatusCode;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GetProposalsByJobIdCommand
extends JobCommand<GetProposalsByJobIdRequest, GetProposalsByJobIdResponse> {
private static final Logger logger = LogManager.getLogger(GetProposalsByJobIdCommand.class);

@Override
public GetProposalsByJobIdResponse Run(GetProposalsByJobIdRequest request) {
logger.info("[x] Fetching proposals for job " + request.getJobId());
try {
List<Proposal> response = proposalRepository.findByJobId(request.getJobId());

logger.info("[x] Found " + response.size() + " proposals for job " + request.getJobId());
List<ProposalModel> proposals = new ArrayList<>();
for (Proposal proposal : response) {
ArrayList<ProposalAttachment> attachments = new ArrayList<>();
Expand Down Expand Up @@ -57,11 +61,13 @@ public GetProposalsByJobIdResponse Run(GetProposalsByJobIdRequest request) {
.withMilestones(milestones)
.build());
}
logger.info("[x] Returning " + proposals.size() + " proposals for job " + request.getJobId());
return GetProposalsByJobIdResponse.builder()
.withProposals(proposals)
.withStatusCode(HttpStatusCode.OK)
.build();
} catch (Exception e) {
logger.error("[x] An error occurred while fetching proposals", e.getMessage());
return GetProposalsByJobIdResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while fetching proposals")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@
import com.workup.shared.commands.jobs.responses.SearchJobsResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.nio.ByteBuffer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.cassandra.core.query.CassandraPageRequest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;

public class SearchJobsCommand extends JobCommand<SearchJobsRequest, SearchJobsResponse> {
private static final Logger logger = LogManager.getLogger(SearchJobsCommand.class);

@Override
public SearchJobsResponse Run(SearchJobsRequest request) {
logger.info("[x] Searching for jobs with query: " + request.getQuery());
try {
CassandraPageRequest pageRequest;

if (request.getPagingState() != null) {
logger.info("[x] Using paging state: " + request.getPagingState());
PageRequest pageReq = PageRequest.of(0, request.getPageLimit());
ByteBuffer byteBuffer =
com.datastax.oss.protocol.internal.util.Bytes.fromHexString(request.getPagingState());
pageRequest = CassandraPageRequest.of(pageReq, byteBuffer);
} else {
logger.info("[x] Fetching First page");
pageRequest = CassandraPageRequest.of(0, request.getPageLimit());
}

Slice<Job> result = jobRepository.searchForJob("%" + request.getQuery() + "%", pageRequest);
// log number of jibs fetched
logger.info("[x] Fetched " + result.getContent().size() + " jobs");
return SearchJobsResponse.builder()
.withJobs(
result.stream()
Expand All @@ -45,8 +53,7 @@ public SearchJobsResponse Run(SearchJobsRequest request) {
.withStatusCode(HttpStatusCode.OK)
.build();
} catch (Exception e) {
System.err.println("ERROR: " + e.getMessage());
e.printStackTrace();
logger.error("[x] An error occurred while searching for jobs", e.getMessage());
return SearchJobsResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage(e.getMessage())
Expand Down
Loading

0 comments on commit 43efcc4

Please sign in to comment.