Changesets · Branches · Tags · Compare
| Changeset | b7bed744edb6ff5eb01ccd9c58fe47f05f82b37d49ef2c5ee279399f8af780e4 |
|---|---|
| Author | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-02 23:24 +05:30 |
| Committer | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-02 23:24 +05:30 |
| Parents | c61dd95048cf |
1 file changed
modified extensions/cli/src/telemetry/posthogService.ts +33 −2
| @@ -1,3 +1,4 @@ | ||
| 1 | +import dns from "dns/promises"; | |
| 1 | 2 | import os from "node:os"; |
| 2 | 3 | |
| 3 | 4 | import node_machine_id from "node-machine-id"; |
| @@ -15,13 +16,36 @@ | ||
| 15 | 16 | this.uniqueId = this.getEventUserId(); |
| 16 | 17 | } |
| 17 | 18 | |
| 19 | + private _hasInternetConnection: boolean | undefined = undefined; | |
| 20 | + private async hasInternetConnection() { | |
| 21 | + const refetchConnection = async () => { | |
| 22 | + try { | |
| 23 | + await dns.lookup("app.posthog.com"); | |
| 24 | + this._hasInternetConnection = true; | |
| 25 | + } catch { | |
| 26 | + this._hasInternetConnection = false; | |
| 27 | + } | |
| 28 | + }; | |
| 29 | + | |
| 30 | + if (typeof this._hasInternetConnection !== "undefined") { | |
| 31 | + void refetchConnection(); // check in background if connection became available | |
| 32 | + return this._hasInternetConnection; | |
| 33 | + } | |
| 34 | + | |
| 35 | + await refetchConnection(); | |
| 36 | + return this._hasInternetConnection; | |
| 37 | + } | |
| 38 | + | |
| 18 | 39 | get isEnabled() { |
| 19 | 40 | return process.env.CONTINUE_CLI_ENABLE_TELEMETRY !== "0"; |
| 20 | 41 | } |
| 21 | 42 | |
| 22 | 43 | private _client: PostHogType | undefined; |
| 23 | 44 | private async getClient() { |
| 24 | - if (this.isEnabled) { | |
| 45 | + if (!(await this.hasInternetConnection())) { | |
| 46 | + this._client = undefined; | |
| 47 | + logger.warn("No internet connection, skipping telemetry"); | |
| 48 | + } else if (this.isEnabled) { | |
| 25 | 49 | if (!this._client) { |
| 26 | 50 | const { PostHog } = await import("posthog-node"); |
| 27 | 51 | this._client = new PostHog( |
| @@ -30,6 +54,7 @@ | ||
| 30 | 54 | host: "https://app.posthog.com", |
| 31 | 55 | }, |
| 32 | 56 | ); |
| 57 | + logger.debug("Initialized telemetry"); | |
| 33 | 58 | } |
| 34 | 59 | } else { |
| 35 | 60 | this._client = undefined; |
| @@ -83,7 +108,13 @@ | ||
| 83 | 108 | try { |
| 84 | 109 | const client = await this.getClient(); |
| 85 | 110 | if (client) { |
| 86 | - await client.shutdown(); | |
| 111 | + // Set a timeout for shutdown to prevent hanging | |
| 112 | + const shutdownPromise = client.shutdown(); | |
| 113 | + const timeoutPromise = new Promise((_, reject) => | |
| 114 | + setTimeout(() => reject(new Error("Shutdown timeout")), 5000), | |
| 115 | + ); | |
| 116 | + | |
| 117 | + await Promise.race([shutdownPromise, timeoutPromise]); | |
| 87 | 118 | } |
| 88 | 119 | } catch (e) { |
| 89 | 120 | logger.debug(`Failed to shutdown PostHog client: ${e}`); |