Skip to content

Commit

Permalink
[CCP-243] - Expose redis cache to engage email DA and use for datafee…
Browse files Browse the repository at this point in the history
…d calls (#1696)

* add RequestCache to perform for engage

* datafeed cache + refactor

* ResponseError

* Fix set cache

* fix cache

* Add tests + lower stats cardinality

* Update api-lookups.ts

* udpate tests

* fix parsing + add feature toggle check

* fix tests after feature toggle check

* add cache uniqueness tests

* up timeout to 3s

* Rename requestCache -> dataFeedCache

---------

Co-authored-by: Joe Ayoub <[email protected]>
  • Loading branch information
ryanrouleau and joe-ayoub-segment authored Nov 15, 2023
1 parent ef6e62f commit 7954581
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 131 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ The `perform` method accepts two arguments, (1) the request client instance (ext
- `features` - The features available in the request based on the customer's sourceID. Features can only be enabled and/or used by internal Twilio/Segment employees. Features cannot be used for Partner builds.
- `statsContext` - An object, containing a `statsClient` and `tags`. Stats can only be used by internal Twilio/Segment employees. Stats cannot be used for Partner builds.
- `logger` - Logger can only be used by internal Twilio/Segment employees. Logger cannot be used for Partner builds.
- `dataFeedCache` - DataFeedCache can only be used by internal Twilio/Segment employees. DataFeedCache cannot be used for Partner builds.
- `transactionContext` - An object, containing transaction variables and a method to update transaction variables which are required for few segment developed actions. Transaction Context cannot be used for Partner builds.
- `stateContext` - An object, containing context variables and a method to get and set context variables which are required for few segment developed actions. State Context cannot be used for Partner builds.
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/create-test-integration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTestEvent } from './create-test-event'
import { StateContext, Destination, TransactionContext } from './destination-kit'
import { mapValues } from './map-values'
import type { DestinationDefinition, StatsContext, Logger } from './destination-kit'
import type { DestinationDefinition, StatsContext, Logger, DataFeedCache } from './destination-kit'
import type { JSONObject } from './json-object'
import type { SegmentEvent } from './segment-event'
import { AuthTokens } from './destination-kit/parse-settings'
Expand Down Expand Up @@ -38,11 +38,12 @@ interface InputData<Settings> {
auth?: AuthTokens
/**
* The features available in the request based on the customer's sourceID;
* `features`, `stats`, `logger` , `transactionContext` and `stateContext` are for internal Twilio/Segment use only.
* `features`, `stats`, `logger`, `dataFeedCache`, and `transactionContext` and `stateContext` are for internal Twilio/Segment use only.
*/
features?: Features
statsContext?: StatsContext
logger?: Logger
dataFeedCache?: DataFeedCache
transactionContext?: TransactionContext
stateContext?: StateContext
}
Expand Down Expand Up @@ -71,6 +72,7 @@ class TestDestination<T, AudienceSettings = any> extends Destination<T, Audience
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
}: InputData<T>
Expand All @@ -92,6 +94,7 @@ class TestDestination<T, AudienceSettings = any> extends Destination<T, Audience
features: features ?? {},
statsContext: statsContext ?? ({} as StatsContext),
logger: logger ?? ({ info: noop, error: noop } as Logger),
dataFeedCache: dataFeedCache ?? ({} as DataFeedCache),
transactionContext: transactionContext ?? ({} as TransactionContext),
stateContext: stateContext ?? ({} as StateContext)
})
Expand All @@ -113,6 +116,7 @@ class TestDestination<T, AudienceSettings = any> extends Destination<T, Audience
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
}: Omit<InputData<T>, 'event'> & { events?: SegmentEvent[] }
Expand All @@ -138,6 +142,7 @@ class TestDestination<T, AudienceSettings = any> extends Destination<T, Audience
features: features ?? {},
statsContext: statsContext ?? ({} as StatsContext),
logger: logger ?? ({} as Logger),
dataFeedCache: dataFeedCache ?? ({} as DataFeedCache),
transactionContext: transactionContext ?? ({} as TransactionContext),
stateContext: stateContext ?? ({} as StateContext)
})
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/destination-kit/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { validateSchema } from '../schema-validation'
import { AuthTokens } from './parse-settings'
import { IntegrationError } from '../errors'
import { removeEmptyValues } from '../remove-empty-values'
import { Logger, StatsContext, TransactionContext, StateContext } from './index'
import { Logger, StatsContext, TransactionContext, StateContext, DataFeedCache } from './index'

type MaybePromise<T> = T | Promise<T>
type RequestClient = ReturnType<typeof createRequestClient>
Expand Down Expand Up @@ -157,6 +157,7 @@ interface ExecuteBundle<T = unknown, Data = unknown, AudienceSettings = any, Act
features?: Features | undefined
statsContext?: StatsContext | undefined
logger?: Logger | undefined
dataFeedCache?: DataFeedCache | undefined
transactionContext?: TransactionContext
stateContext?: StateContext
}
Expand Down Expand Up @@ -236,6 +237,7 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings =
features: bundle.features,
statsContext: bundle.statsContext,
logger: bundle.logger,
dataFeedCache: bundle.dataFeedCache,
transactionContext: bundle.transactionContext,
stateContext: bundle.stateContext,
audienceSettings: bundle.audienceSettings
Expand Down Expand Up @@ -286,6 +288,7 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings =
features: bundle.features,
statsContext: bundle.statsContext,
logger: bundle.logger,
dataFeedCache: bundle.dataFeedCache,
transactionContext: bundle.transactionContext,
stateContext: bundle.stateContext
}
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/destination-kit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ interface EventInput<Settings> {
readonly features?: Features
readonly statsContext?: StatsContext
readonly logger?: Logger
readonly dataFeedCache?: DataFeedCache
readonly transactionContext?: TransactionContext
readonly stateContext?: StateContext
}
Expand All @@ -285,6 +286,7 @@ interface BatchEventInput<Settings> {
readonly features?: Features
readonly statsContext?: StatsContext
readonly logger?: Logger
readonly dataFeedCache?: DataFeedCache
readonly transactionContext?: TransactionContext
readonly stateContext?: StateContext
}
Expand All @@ -300,6 +302,7 @@ interface OnEventOptions {
features?: Features
statsContext?: StatsContext
logger?: Logger
readonly dataFeedCache?: DataFeedCache
transactionContext?: TransactionContext
stateContext?: StateContext
/** Handler to perform synchronization. If set, the refresh access token method will be synchronized across
Expand Down Expand Up @@ -353,6 +356,11 @@ export interface Logger {
withTags(extraTags: any): void
}

export interface DataFeedCache {
setRequestResponse(requestId: string, response: string, expiryInSeconds: number): Promise<void>
getRequestResponse(requestId: string): Promise<string>
}

export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
readonly definition: DestinationDefinition<Settings>
readonly name: string
Expand Down Expand Up @@ -529,6 +537,7 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
}: EventInput<Settings>
Expand All @@ -552,6 +561,7 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
})
Expand All @@ -567,6 +577,7 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
}: BatchEventInput<Settings>
Expand All @@ -591,6 +602,7 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
features,
statsContext,
logger,
dataFeedCache,
transactionContext,
stateContext
})
Expand Down Expand Up @@ -627,6 +639,7 @@ export class Destination<Settings = JSONObject, AudienceSettings = JSONObject> {
features: options?.features || {},
statsContext: options?.statsContext || ({} as StatsContext),
logger: options?.logger,
dataFeedCache: options?.dataFeedCache,
transactionContext: options?.transactionContext,
stateContext: options?.stateContext
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/destination-kit/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StateContext, Logger, StatsContext, TransactionContext, ActionHookType } from './index'
import { StateContext, Logger, StatsContext, TransactionContext, DataFeedCache, ActionHookType } from './index'
import type { RequestOptions } from '../request-client'
import type { JSONObject } from '../json-object'
import { AuthTokens } from './parse-settings'
Expand Down Expand Up @@ -46,6 +46,7 @@ export interface ExecuteInput<
readonly features?: Features
readonly statsContext?: StatsContext
readonly logger?: Logger
readonly dataFeedCache?: DataFeedCache
readonly transactionContext?: TransactionContext
readonly stateContext?: StateContext
}
Expand Down
Loading

0 comments on commit 7954581

Please sign in to comment.