1
1
package com .appsmith .server .solutions ;
2
2
3
+ import com .appsmith .server .services .ConfigService ;
4
+ import lombok .RequiredArgsConstructor ;
3
5
import lombok .extern .slf4j .Slf4j ;
4
6
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
5
7
import org .springframework .http .MediaType ;
19
21
@ Component
20
22
@ ConditionalOnProperty (prefix = "is" , name = "self-hosted" )
21
23
@ Slf4j
24
+ @ RequiredArgsConstructor
22
25
public class PingScheduledTask {
23
26
27
+ private final ConfigService configService ;
28
+
24
29
public static final URI GET_IP_URI = URI .create ("https://api6.ipify.org" );
25
30
26
31
/**
27
32
* Gets the external IP address of this server and pings a data point to indicate that this server instance is live.
33
+ * We use an initial delay of two minutes to roughly wait for the application along with the migrations are finished
34
+ * and ready.
28
35
*/
29
36
// Number of milliseconds between the start of each scheduled calls to this method.
30
- @ Scheduled (fixedRate = 6 * 60 * 60 * 1000 /* six hours */ )
37
+ @ Scheduled (initialDelay = 2 * 60 * 1000 /* two minutes */ , fixedRate = 6 * 60 * 60 * 1000 /* six hours */ )
31
38
public void pingSchedule () {
32
- getInstallationId ()
33
- .flatMap (this ::doPing )
34
- .doOnError (error -> log .debug ("Error pinging home" , error ))
39
+ Mono .zip (getInstanceId (), getAddress ())
40
+ .flatMap (tuple -> doPing (tuple .getT1 (), tuple .getT2 ()))
35
41
.subscribeOn (Schedulers .single ())
36
42
.subscribe ();
37
43
}
38
44
45
+ private Mono <String > getInstanceId () {
46
+ return configService .getByName ("instance-id" )
47
+ .map (config -> config .getConfig ().getAsString ("value" ));
48
+ }
49
+
39
50
/**
40
51
* This method hits an API endpoint that returns the external IP address of this server instance.
52
+ *
41
53
* @return A publisher that yields the IP address.
42
54
*/
43
- private Mono <String > getInstallationId () {
55
+ private Mono <String > getAddress () {
44
56
return WebClient
45
57
.create ()
46
58
.get ()
@@ -52,10 +64,12 @@ private Mono<String> getInstallationId() {
52
64
/**
53
65
* Given a unique ID (called a `userId` here), this method hits the Segment API to save a data point on this server
54
66
* instance being live.
55
- * @param userId A unique identifier for this server instance (usually, the external IP address of the server).
67
+ *
68
+ * @param instanceId A unique identifier for this server instance, usually generated at the server's first start.
69
+ * @param ipAddress The external IP address of this instance's machine.
56
70
* @return A publisher that yields the string response of recording the data point.
57
71
*/
58
- private Mono <String > doPing (String userId ) {
72
+ private Mono <String > doPing (String instanceId , String ipAddress ) {
59
73
// Note: Hard-coding Segment auth header and the event name intentionally. These are not intended to be
60
74
// environment specific values, instead, they are common values for all self-hosted environments. As such, they
61
75
// are not intended to be configurable.
@@ -66,7 +80,9 @@ private Mono<String> doPing(String userId) {
66
80
.header ("Authorization" , "Basic QjJaM3hXRThXdDRwYnZOWDRORnJPNWZ3VXdnYWtFbk06" )
67
81
.contentType (MediaType .APPLICATION_JSON )
68
82
.body (BodyInserters .fromValue (Map .of (
69
- "userId" , userId ,
83
+ "userId" , instanceId ,
84
+ "context" , Map .of ("ip" , ipAddress ),
85
+ "properties" , Map .of ("ip" , ipAddress ),
70
86
"event" , "Instance Active"
71
87
)))
72
88
.retrieve ()
0 commit comments