From 56492f7cc17bb354706fa82e9d874ab5b39f6656 Mon Sep 17 00:00:00 2001 From: Shane Duvall Date: Thu, 10 Mar 2022 15:18:10 -0600 Subject: [PATCH 1/6] Node Issue #309, update to eliminate confusion of certain flush processes --- .../catalog/libraries/server/node/index.md | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index 730e65f7d3..575d8fd369 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -437,6 +437,85 @@ analytics.flush(function(err, batch){ }); ``` +## Long running process + +You should call client.track(...) and know that events will be queued and eventually sent to Segment, also to prevent losing messages make sure that you capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown. + +```js +import { randomUUID } from 'crypto'; +import Analytics from 'analytics-node' + +const WRITE_KEY = '...'; + +const analytics = new Analytics(WRITE_KEY, { flushAt: 10 }); + +analytics.track({ + anonymousId: randomUUID(), + event: 'Test event', + properties: { + name: 'Test event', + timestamp: new Date() + } +}); + +const exitGracefully = async (code) => { + console.log('Flushing events'); + await analytics.flush(function(err, batch) { + console.log('Flushed, and now this program can exit!'); + process.exit(code); + }); +}; + +[ + 'beforeExit', 'uncaughtException', 'unhandledRejection', + 'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', + 'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', + 'SIGUSR2', 'SIGTERM', +].forEach(evt => process.on(evt, exitGracefully)); + +function logEvery2Seconds(i) { + setTimeout(() => { + console.log('Infinite Loop Test n:', i); + logEvery2Seconds(++i); + }, 2000); +} + +logEvery2Seconds(0); +``` + +# Short lived process + +Short-lived functions have a predictably short and linear lifecycle so use a queue big enough to hold all messages and then +await for flush to complete it's work. + + +```js +import { randomUUID } from 'crypto'; +import Analytics from 'analytics-node' + + +async function lambda() +{ + const WRITE_KEY = '...'; + const analytics = new Analytics(WRITE_KEY, { flushAt: 20 }); + analytics.flushed = true; + + analytics.track({ + anonymousId: randomUUID(), + event: 'Test event', + properties: { + name: 'Test event', + timestamp: new Date() + } + }); + await analytics.flush(function(err, batch) { + console.log('Flushed, and now this program can exit!'); + }); +} + +lambda(); +``` + ## Multiple Clients From d9d6a795fc8fcd8fd56281ff7c1285ea54f9b896 Mon Sep 17 00:00:00 2001 From: Shane Duvall Date: Thu, 10 Mar 2022 15:21:54 -0600 Subject: [PATCH 2/6] Node Issue #309 Block header update --- src/connections/sources/catalog/libraries/server/node/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index 575d8fd369..dd4318d4c4 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -483,7 +483,7 @@ function logEvery2Seconds(i) { logEvery2Seconds(0); ``` -# Short lived process +## Short lived process Short-lived functions have a predictably short and linear lifecycle so use a queue big enough to hold all messages and then await for flush to complete it's work. From 26361b9400aa5fe4d19a3c0214ddf1bd357118a3 Mon Sep 17 00:00:00 2001 From: Shane Duvall Date: Fri, 11 Mar 2022 10:48:29 -0600 Subject: [PATCH 3/6] In relation to Java Issue #314; update the documentation to reflect updated syntax --- .../catalog/libraries/server/java/index.md | 15 +++++++-------- .../catalog/libraries/server/java/quickstart.md | 15 +++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/connections/sources/catalog/libraries/server/java/index.md b/src/connections/sources/catalog/libraries/server/java/index.md index 7bdee1de35..153dc32e69 100644 --- a/src/connections/sources/catalog/libraries/server/java/index.md +++ b/src/connections/sources/catalog/libraries/server/java/index.md @@ -38,7 +38,7 @@ Add to `pom.xml`: or if you're using Gradle: ```bash -compile 'com.segment.analytics.java:analytics:+' +implementation 'com.segment.analytics.java:analytics:+' ``` ### Initialize the SDK @@ -74,14 +74,13 @@ We recommend calling `identify` a single time when the user's account is first c Example `identify` call: ```java +Map map = new HashMap(); +map.put("name", "Michael Bolton"); +map.put("email", "mbolton@example.com"); + analytics.enqueue(IdentifyMessage.builder() - .userId("f4ca124298") - .traits(ImmutableMap.builder() - .put("name", "Michael Bolton") - .put("email", "mbolton@example.com") - .build() - ) -); + .userId("f4ca124298") + .traits(map)); ``` This call is identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with `name` and `email` traits. diff --git a/src/connections/sources/catalog/libraries/server/java/quickstart.md b/src/connections/sources/catalog/libraries/server/java/quickstart.md index 35edefb988..97666556f2 100644 --- a/src/connections/sources/catalog/libraries/server/java/quickstart.md +++ b/src/connections/sources/catalog/libraries/server/java/quickstart.md @@ -40,7 +40,7 @@ Here's what it would look like with Maven: *or if you're using Gradle:* ```bash -compile 'com.segment.analytics.java:analytics:+' +implementation 'com.segment.analytics.java:analytics:+' ``` ## Step 3: Initialize the SDK @@ -71,14 +71,13 @@ The `identify` message is how you tell Segment who the current user is. It inclu Here's what a basic call to `identify` a user might look like: ```java +Map map = new HashMap(); +map.put("name", "Michael Bolton"); +map.put("email", "mbolton@example.com"); + analytics.enqueue(IdentifyMessage.builder() - .userId("f4ca124298") - .traits(ImmutableMap.builder() - .put("name", "Michael Bolton") - .put("email", "mbolton@example.com") - .build() - ) -); + .userId("f4ca124298") + .traits(map)); ``` **Note:** The enqueue method takes a `MessageBuilder` instance and not a `Message` instance directly. This is to allow you to use a `MessageTransformer` that applies to all incoming messages and transform or add data. From 7b5cb31222098a78f75a1e5eae3f2026f87d4d00 Mon Sep 17 00:00:00 2001 From: "Shane L. Duvall" Date: Wed, 16 Mar 2022 12:42:54 -0500 Subject: [PATCH 4/6] Update src/connections/sources/catalog/libraries/server/node/index.md Co-authored-by: rchinn-segment <93161299+rchinn-segment@users.noreply.github.com> --- src/connections/sources/catalog/libraries/server/node/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index dd4318d4c4..89ff20abe3 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -439,7 +439,7 @@ analytics.flush(function(err, batch){ ## Long running process -You should call client.track(...) and know that events will be queued and eventually sent to Segment, also to prevent losing messages make sure that you capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown. +You should call `client.track(...)` and know that events will be queued and eventually sent to Segment. To prevent losing messages, be sure to capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown. ```js import { randomUUID } from 'crypto'; From 0202e9ed7ac029cb45550a7a74c4ccccb636e7d5 Mon Sep 17 00:00:00 2001 From: "Shane L. Duvall" Date: Wed, 16 Mar 2022 12:43:07 -0500 Subject: [PATCH 5/6] Update src/connections/sources/catalog/libraries/server/node/index.md Co-authored-by: rchinn-segment <93161299+rchinn-segment@users.noreply.github.com> --- src/connections/sources/catalog/libraries/server/node/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index 89ff20abe3..5acfa7df62 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -485,7 +485,7 @@ logEvery2Seconds(0); ## Short lived process -Short-lived functions have a predictably short and linear lifecycle so use a queue big enough to hold all messages and then +Short-lived functions have a predictably short and linear lifecycle, so use a queue big enough to hold all messages and then await flush to complete its work. await for flush to complete it's work. From f3203a393382c9678b79bd02f9d61bf44818c3b1 Mon Sep 17 00:00:00 2001 From: "Shane L. Duvall" Date: Wed, 16 Mar 2022 12:43:13 -0500 Subject: [PATCH 6/6] Update src/connections/sources/catalog/libraries/server/node/index.md Co-authored-by: rchinn-segment <93161299+rchinn-segment@users.noreply.github.com> --- src/connections/sources/catalog/libraries/server/node/index.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/connections/sources/catalog/libraries/server/node/index.md b/src/connections/sources/catalog/libraries/server/node/index.md index 5acfa7df62..30b6b85f86 100644 --- a/src/connections/sources/catalog/libraries/server/node/index.md +++ b/src/connections/sources/catalog/libraries/server/node/index.md @@ -486,7 +486,6 @@ logEvery2Seconds(0); ## Short lived process Short-lived functions have a predictably short and linear lifecycle, so use a queue big enough to hold all messages and then await flush to complete its work. -await for flush to complete it's work. ```js