-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.groovy
107 lines (88 loc) · 2.94 KB
/
app.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.greglturnquist.springmonitor
import groovy.util.logging.*
import org.joda.time.*
import org.springframework.integration.file.tail.OSDelegatingFileTailingMessageProducer
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
import org.springframework.scheduling.config.ScheduledTaskRegistrar
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.TaskScheduler
import org.springframework.messaging.simp.SimpMessagingTemplate
@Grab("joda-time:joda-time:2.3")
@Slf4j
@Configuration
class Monitor {
@Bean
OSDelegatingFileTailingMessageProducer tailer() {
def tailer = new OSDelegatingFileTailingMessageProducer()
tailer.file = new File('sim.log')
tailer.outputChannel = tailChannel()
tailer
}
@Bean
MessageChannel tailChannel() {
new DirectChannel()
}
}
@Configuration
@Slf4j
@MessageEndpoint
@EnableScheduling
class MonitorService implements SchedulingConfigurer {
@Autowired
TaskScheduler taskScheduler
@Autowired
SimpMessagingTemplate template
DateTime latest = new DateTime()
@ServiceActivator(inputChannel = "tailChannel")
void handle(String data) {
if (data.contains("LightningData")) {
latest = new DateTime(Date.parse("yyyy-MM-dd HH:mm:ss.SSS", data[0..22]))
}
}
@Scheduled(fixedRate = 5000L)
void check() {
def now = new DateTime()
def duration = new Duration(latest, now)
if (duration.toStandardSeconds().seconds >= 10) {
def status = "It has been ${duration.toStandardSeconds().seconds} seconds since last update!"
log.error(status)
template.convertAndSend("/topic/status", new Alarm([severity: "BAD", description: status]))
} else {
def status = "It has been ${duration.toStandardSeconds().seconds} seconds since last update"
log.info(status)
template.convertAndSend("/topic/status", new Alarm([severity: "GOOD", description: status]))
}
}
@Override
void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.taskScheduler = this.taskScheduler
}
}
@Slf4j
@Grab("thymeleaf-spring4")
@Controller
class HomeController {
@RequestMapping("/")
def index() {
return "index"
}
}
class Alarm {
String severity
String description
}
@Grab("org.webjars:stomp-websocket:2.3.1")
@Grab("org.webjars:sockjs-client:0.3.4")
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic")
config.setApplicationDestinationPrefixes("/app")
}
@Override
void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/spring-monitor").withSockJS()
}
}