Skip to content

Commit 2677003

Browse files
committed
Use a unique instance ID for pinging home (#566)
* Use a unique instance ID for pinging home * Fix event name
1 parent dde947e commit 2677003

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.appsmith.server.domains;
22

33
import com.appsmith.external.models.BaseDomain;
4+
import lombok.AllArgsConstructor;
45
import lombok.Getter;
56
import lombok.NoArgsConstructor;
67
import lombok.Setter;
@@ -12,6 +13,7 @@
1213
@Setter
1314
@ToString
1415
@NoArgsConstructor
16+
@AllArgsConstructor
1517
@Document
1618
public class Config extends BaseDomain {
1719
JSONObject config;

app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,4 +916,12 @@ public void updateErroneousActionIdsInPage(MongoTemplate mongoTemplate) {
916916
}
917917
}
918918

919+
@ChangeSet(order = "025", id = "generate-unique-id-for-instance", author = "")
920+
public void generateUniqueIdForInstance(MongoTemplate mongoTemplate) {
921+
mongoTemplate.insert(new Config(
922+
new JSONObject(Map.of("value", new ObjectId().toHexString())),
923+
"instance-id"
924+
));
925+
}
926+
919927
}

app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/PingScheduledTask.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.appsmith.server.solutions;
22

3+
import com.appsmith.server.services.ConfigService;
4+
import lombok.RequiredArgsConstructor;
35
import lombok.extern.slf4j.Slf4j;
46
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
57
import org.springframework.http.MediaType;
@@ -19,28 +21,38 @@
1921
@Component
2022
@ConditionalOnProperty(prefix = "is", name = "self-hosted")
2123
@Slf4j
24+
@RequiredArgsConstructor
2225
public class PingScheduledTask {
2326

27+
private final ConfigService configService;
28+
2429
public static final URI GET_IP_URI = URI.create("https://api6.ipify.org");
2530

2631
/**
2732
* 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.
2835
*/
2936
// 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 */)
3138
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()))
3541
.subscribeOn(Schedulers.single())
3642
.subscribe();
3743
}
3844

45+
private Mono<String> getInstanceId() {
46+
return configService.getByName("instance-id")
47+
.map(config -> config.getConfig().getAsString("value"));
48+
}
49+
3950
/**
4051
* This method hits an API endpoint that returns the external IP address of this server instance.
52+
*
4153
* @return A publisher that yields the IP address.
4254
*/
43-
private Mono<String> getInstallationId() {
55+
private Mono<String> getAddress() {
4456
return WebClient
4557
.create()
4658
.get()
@@ -52,10 +64,12 @@ private Mono<String> getInstallationId() {
5264
/**
5365
* Given a unique ID (called a `userId` here), this method hits the Segment API to save a data point on this server
5466
* 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.
5670
* @return A publisher that yields the string response of recording the data point.
5771
*/
58-
private Mono<String> doPing(String userId) {
72+
private Mono<String> doPing(String instanceId, String ipAddress) {
5973
// Note: Hard-coding Segment auth header and the event name intentionally. These are not intended to be
6074
// environment specific values, instead, they are common values for all self-hosted environments. As such, they
6175
// are not intended to be configurable.
@@ -66,7 +80,9 @@ private Mono<String> doPing(String userId) {
6680
.header("Authorization", "Basic QjJaM3hXRThXdDRwYnZOWDRORnJPNWZ3VXdnYWtFbk06")
6781
.contentType(MediaType.APPLICATION_JSON)
6882
.body(BodyInserters.fromValue(Map.of(
69-
"userId", userId,
83+
"userId", instanceId,
84+
"context", Map.of("ip", ipAddress),
85+
"properties", Map.of("ip", ipAddress),
7086
"event", "Instance Active"
7187
)))
7288
.retrieve()

0 commit comments

Comments
 (0)