Commit c841e3d8 authored by Vishal Lal's avatar Vishal Lal Committed by Vishal Lal

Initial checkin for healthcheck. Add 10s timeout limit for Mongo

parent 038527e2
package works.weave.socks.cart.configuration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mongodb.MongoClientOptions;
@Configuration
@AutoConfigureBefore(MongoAutoConfiguration.class)
public class MongoConfiguration {
@Bean
public MongoClientOptions optionsProvider() {
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
optionsBuilder.serverSelectionTimeout(10000);
MongoClientOptions options = optionsBuilder.build();
return options;
}
}
......@@ -10,6 +10,7 @@ import works.weave.socks.cart.cart.CartDAO;
import works.weave.socks.cart.cart.CartResource;
import works.weave.socks.cart.entities.Cart;
@RestController
@RequestMapping(path = "/carts")
public class CartsController {
......
package works.weave.socks.cart.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import works.weave.socks.cart.entities.HealthCheck;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class HealthCheckController {
@Autowired
private MongoTemplate mongoTemplate;
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.GET, path = "/health")
public
@ResponseBody
Map<String, List<HealthCheck>> getHealth() {
Map<String, List<HealthCheck>> map = new HashMap<String, List<HealthCheck>>();
List<HealthCheck> healthChecks = new ArrayList<HealthCheck>();
Date dateNow = Calendar.getInstance().getTime();
HealthCheck app = new HealthCheck("cart", "OK", dateNow);
HealthCheck database = new HealthCheck("cart-db", "OK", dateNow);
try {
mongoTemplate.executeCommand("{ buildInfo: 1 }");
} catch (Exception e) {
database.setStatus("err");
}
healthChecks.add(app);
healthChecks.add(database);
map.put("health", healthChecks);
return map;
}
}
package works.weave.socks.cart.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Calendar;
import java.util.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
public class HealthCheck {
private String service;
private String status;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private Date date = Calendar.getInstance().getTime();
public HealthCheck() {
}
public HealthCheck(String service, String status, Date date) {
this.service = service;
this.status = status;
this.date = date;
}
@Override
public String toString() {
return "HealthCheck{" +
"service='" + service + '\'' +
", status='" + status + '\'' +
", date='" + date +
'}';
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
server.port=${port:8081}
spring.data.mongodb.uri=mongodb://${db:cart-db}:27017/data
endpoints.health.enabled=false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment