Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shutdownAsync flushes the whole queue #191

Merged
merged 16 commits into from
Feb 26, 2024
28 changes: 22 additions & 6 deletions posthog-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,21 +563,37 @@ export abstract class PostHogCoreStateless {
)
}

async shutdownAsync(): Promise<void> {
async shutdownAsync(shutdownTimeout?: number): Promise<void> {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(this._flushTimer)
try {
await this.flushAsync()
await Promise.all(
Object.values(this.pendingPromises).map((x) =>
x.catch(() => {
// ignore errors as we are shutting down and can't deal with them anyways.
})
)
)
// flush again to make sure we send all events, some of which might've been added
// while we were waiting for the pending promises to resolve
// For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
await this.flushAsync()

const timeout = shutdownTimeout ?? 30000
const startTime = Date.now()
while (true) {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
const queue = this.getPersistedProperty<PostHogQueueItem[]>(PostHogPersistedProperty.Queue) || []

if (queue.length === 0) {
break
}

// flush again to make sure we send all events, some of which might've been added
// while we were waiting for the pending promises to resolve
// For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
await this.flushAsync()

// If we've been waiting for more than the shutdownTimeout, stop it
const now = Date.now()
if (startTime + timeout >= now) {
break
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will silently break instead of throwing an error or something since its an intended behavior.

}
}
} catch (e) {
if (!isPostHogFetchError(e)) {
throw e
Expand Down
4 changes: 2 additions & 2 deletions posthog-node/src/posthog-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
void this.shutdownAsync()
}

async shutdownAsync(): Promise<void> {
async shutdownAsync(shutdownTimeout?: number): Promise<void> {
this.featureFlagsPoller?.stopPoller()
return super.shutdownAsync()
return super.shutdownAsync(shutdownTimeout)
}

private addLocalPersonAndGroupProperties(
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/src/native-deps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OptionalReactNativeDeviceInfo } from './optional/OptionalReactNativeDev
import { PostHogCustomAppProperties, PostHogCustomAsyncStorage } from './types'

export const getAppProperties = (): PostHogCustomAppProperties => {
var deviceType = 'Mobile'
let deviceType = 'Mobile'

if (Platform.OS === 'macos' || Platform.OS === 'windows') {
deviceType = 'Desktop'
Expand Down
Loading