Skip to content

Commit 832f28c

Browse files
authored
Implements a readiness endpoint (#82)
* Implements a readiness endpoint When this converter-application is deployed to a Kubernetes (or Kubernetes-like) environment, a readiness endpoint can be crucial for ensuring that the application has started and is ready to accept new connections. The same endpoint can also be used in other contexts, like e.g. for testing that another application is able to reach the converter-application (used like a ping endpoint basically). This new /ready endpoint checks that the officeManager in the converter service is running and can execute tasks, and responds with 200 OK if so. Otherwise it responds with 503 Service Unavailable * Updates docs for the readiness endpoint
1 parent a098076 commit 832f28c

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ curl -F file=@src/test/resources/testfiles/template.dotx "localhost:14080/conver
2626
Check the controller to understand the different endpoints
2727

2828
- `/conversion?format=html` as multipart with `file` as the file to convert and `format` for the target format
29+
- `/ready` is a readiness GET-endpoint that returns 200 OK when the application is ready, otherwise 503 Service Unavailable. This *might* create some load, and should be used and monitored accordingly.
2930

3031

3132
### Configuration

src/main/kotlin/de/kontextwork/converter/module/convert/ConverterService.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ class ConverterService(
4343
.execute()
4444
return outputStream
4545
}
46+
47+
fun isReady(): Boolean {
48+
if (!officeManager.isRunning) {
49+
return false
50+
}
51+
52+
// Execute a dummy task
53+
officeManager.execute { }
54+
55+
// If the above no-op task executed successfully, the application is ready
56+
return true
57+
}
4658
}

src/main/kotlin/de/kontextwork/converter/module/convert/controller/ConversionController.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.springframework.http.HttpHeaders
1111
import org.springframework.http.HttpStatus
1212
import org.springframework.http.MediaType
1313
import org.springframework.http.ResponseEntity
14+
import org.springframework.web.bind.annotation.GetMapping
1415
import org.springframework.web.bind.annotation.PostMapping
1516
import org.springframework.web.bind.annotation.RequestMapping
1617
import org.springframework.web.bind.annotation.RequestParam
@@ -62,5 +63,12 @@ class ConversionController(
6263
return ResponseEntity.ok().headers(headers).body(convertedFile.toByteArray())
6364
}
6465

65-
66+
@GetMapping("/ready")
67+
fun isReady(): ResponseEntity<Void> {
68+
return if (converterService.isReady()) {
69+
ResponseEntity.ok().build()
70+
} else {
71+
ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build()
72+
}
73+
}
6674
}

0 commit comments

Comments
 (0)