@autofleet/matmon 2.0.4-beta-1 → 2.0.4-beta-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,11 +17,10 @@ const logger_1 = __importDefault(require("../logger"));
|
|
|
17
17
|
const util_1 = require("util");
|
|
18
18
|
const { AF_SERVICE_NAME } = process.env;
|
|
19
19
|
const ORM_CACHE_PREFIX = 'ormCache';
|
|
20
|
-
const INVALIDATION_HOOKS = ['
|
|
21
|
-
const BULK_HOOKS = ['beforeBulkUpdate', 'beforeBulkDestroy'];
|
|
20
|
+
const INVALIDATION_HOOKS = ['afterUpdate', 'afterDestroy'];
|
|
22
21
|
const generateInstanceKey = (name, id) => `${AF_SERVICE_NAME}:${ORM_CACHE_PREFIX}:${name}_${id}`;
|
|
23
22
|
const generateDependencyKey = (modelName, associationName, associationId) => `${AF_SERVICE_NAME}:${ORM_CACHE_PREFIX}:${modelName}_${associationName}_${associationId}_DEPENDENCIES`;
|
|
24
|
-
const getInstanceDependencyKeys = (modelOptions, instance) => newrelic_1.default.startSegment(
|
|
23
|
+
const getInstanceDependencyKeys = (modelOptions, instance) => newrelic_1.default.startSegment(`${ORM_CACHE_PREFIX}:getInstanceDependencyKeys`, true, () => {
|
|
25
24
|
const keys = modelOptions.associations
|
|
26
25
|
.filter((associationOptions) => instance[associationOptions.alias])
|
|
27
26
|
.map((associationOptions) => {
|
|
@@ -46,7 +45,13 @@ const getInstanceDependencyKeys = (modelOptions, instance) => newrelic_1.default
|
|
|
46
45
|
const handleTransactionHook = (instance, options, func) => {
|
|
47
46
|
const { transaction } = options;
|
|
48
47
|
if (transaction) {
|
|
49
|
-
transaction
|
|
48
|
+
if (!transaction[ORM_CACHE_PREFIX]) {
|
|
49
|
+
transaction[ORM_CACHE_PREFIX] = new Set();
|
|
50
|
+
}
|
|
51
|
+
if (!transaction[ORM_CACHE_PREFIX].has(instance.id)) {
|
|
52
|
+
transaction.afterCommit(() => func(instance));
|
|
53
|
+
transaction[ORM_CACHE_PREFIX].add(instance.id);
|
|
54
|
+
}
|
|
50
55
|
}
|
|
51
56
|
else {
|
|
52
57
|
func(instance);
|
|
@@ -61,7 +66,7 @@ class SequelizeAdapter {
|
|
|
61
66
|
return this.ormInstance.models[modelName];
|
|
62
67
|
}
|
|
63
68
|
getModelDependencies(modelName) {
|
|
64
|
-
return newrelic_1.default.startSegment(
|
|
69
|
+
return newrelic_1.default.startSegment(`${ORM_CACHE_PREFIX}:getModelDependencies`, true, () => {
|
|
65
70
|
const { associations } = this.ormInstance.models[modelName];
|
|
66
71
|
return [
|
|
67
72
|
...Object.keys(associations).map(association => {
|
|
@@ -90,7 +95,7 @@ class SequelizeAdapter {
|
|
|
90
95
|
}
|
|
91
96
|
}
|
|
92
97
|
injectGetWithCacheFunction(cache, modelOptions) {
|
|
93
|
-
const addDependencies = (instance) => newrelic_1.default.startSegment(
|
|
98
|
+
const addDependencies = (instance) => newrelic_1.default.startSegment(`${ORM_CACHE_PREFIX}:addDependencies`, true, () => {
|
|
94
99
|
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
95
100
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
96
101
|
this.debug('Adding dependencies', { instanceKey, dependencyKeys });
|
|
@@ -120,7 +125,7 @@ class SequelizeAdapter {
|
|
|
120
125
|
});
|
|
121
126
|
}
|
|
122
127
|
addInvalidationHooks(cache, modelOptions) {
|
|
123
|
-
const invalidateModelInstance = (hook, instance) => newrelic_1.default.startSegment(
|
|
128
|
+
const invalidateModelInstance = (hook, instance) => newrelic_1.default.startSegment(`${ORM_CACHE_PREFIX}:invalidateModelInstance`, true, () => {
|
|
124
129
|
const dependencyKeys = getInstanceDependencyKeys(modelOptions, instance);
|
|
125
130
|
const instanceKey = generateInstanceKey(modelOptions.name, instance.id);
|
|
126
131
|
this.debug(`Removing dependencies (triggered by ${hook})`, { instance, instanceKey, dependencyKeys });
|
|
@@ -130,7 +135,7 @@ class SequelizeAdapter {
|
|
|
130
135
|
removeMulti.del(instanceKey);
|
|
131
136
|
return removeMultiAsync();
|
|
132
137
|
});
|
|
133
|
-
const invalidateModelInstanceByAssociation = (hook, association, associationId) => newrelic_1.default.startSegment(
|
|
138
|
+
const invalidateModelInstanceByAssociation = (hook, association, associationId) => newrelic_1.default.startSegment(`${ORM_CACHE_PREFIX}:invalidateModelInstanceByAssociation`, true, () => __awaiter(this, void 0, void 0, function* () {
|
|
134
139
|
const dependentInstancesKeys = yield cache.getClient().smembersAsync(generateDependencyKey(modelOptions.name, association, associationId));
|
|
135
140
|
this.debug(`Invalidating dependent instances (triggered by ${hook})`, { dependentInstancesKeys });
|
|
136
141
|
const removeMulti = cache.getClient().multi();
|
|
@@ -150,7 +155,6 @@ class SequelizeAdapter {
|
|
|
150
155
|
}));
|
|
151
156
|
const model = this.getModel(modelOptions.name);
|
|
152
157
|
INVALIDATION_HOOKS.map(hook => model.addHook(hook, (instance, options) => handleTransactionHook(instance, options, instance => invalidateModelInstance(hook, instance))));
|
|
153
|
-
BULK_HOOKS.map(hook => model.addHook(hook, options => options.individualHook = true));
|
|
154
158
|
modelOptions.associations = this.getModelDependencies(modelOptions.name);
|
|
155
159
|
this.debug(`Adding Invalidations Hooks to ${modelOptions.name}'s associations`, { associations: modelOptions.associations });
|
|
156
160
|
modelOptions.associations.map((associationOptions) => {
|