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

Develop #204

Merged
merged 28 commits into from
Jan 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
de42351
ci: konecty docker
7sete7 Dec 12, 2024
b8030d8
Merge pull request #200 from konecty/main
7sete7 Dec 18, 2024
537d6d5
refactor: relocate storage
7sete7 Dec 20, 2024
7164f28
feat: add queue support
7sete7 Dec 20, 2024
235b1fb
fix: incorrect types
7sete7 Dec 20, 2024
99a98df
chore: upload lockfile
7sete7 Dec 20, 2024
819fcfa
chore: types
7sete7 Jan 2, 2025
157eb0e
feat: create history outside konsistent
7sete7 Jan 2, 2025
d95f6cf
feat: create an general event manager
7sete7 Jan 3, 2025
613e80f
feat: send builtin konsistent message
7sete7 Jan 3, 2025
8126f3b
refactor: remove duplicate code
7sete7 Jan 3, 2025
fa497fb
fix: incorrect type
7sete7 Jan 3, 2025
7acc848
refactor: duplicate code
7sete7 Jan 3, 2025
085edb8
feat: shutdown resources
7sete7 Jan 3, 2025
d7c57e5
feat: handle casual connection errors
7sete7 Jan 3, 2025
db33d7c
feat: get konsistent config from namespace
7sete7 Jan 3, 2025
c484e5b
refactor: duplicatted code
7sete7 Jan 3, 2025
8c33b2c
fix: send message retries
7sete7 Jan 7, 2025
573a5f4
fix: send to konsistent after the transaction committed
7sete7 Jan 8, 2025
9b6147a
feat: konsistent write ahead log
7sete7 Jan 10, 2025
a82f7d0
chore: remove unecessary validation logs
7sete7 Jan 13, 2025
97a2aa9
fix: validate field as array
7sete7 Jan 13, 2025
411420b
Merge branch 'main' into feat/queue
7sete7 Jan 14, 2025
fa327c6
Merge branch 'ci/konecty-docker' into feat/queue
7sete7 Jan 14, 2025
ac80b5d
feat: add rabbit to docker compose
7sete7 Jan 14, 2025
7d88fe9
Merge branch 'feat/history' into feat/queue
7sete7 Jan 14, 2025
d7db64a
fix: missing close bracket
7sete7 Jan 14, 2025
d2c0ae3
Merge pull request #201 from konecty/feat/queue
7sete7 Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: create history outside konsistent
  • Loading branch information
7sete7 committed Jan 2, 2025
commit 157eb0eeec06111326931570155fc492a95137d1
42 changes: 27 additions & 15 deletions src/imports/data/data.js
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ import { getUserSafe } from '@imports/auth/getUser';
import { TRANSACTION_OPTIONS } from '@imports/consts';
import { find } from "@imports/data/api";
import { client } from '@imports/database';
import createHistory from '@imports/konsistent/createHistory';
import processIncomingChange from '@imports/konsistent/processIncomingChange';
import objectsDiff from '@imports/utils/objectsDiff';
import { dateToString, stringToDate } from '../data/dateParser';
@@ -50,7 +51,6 @@ import { errorReturn, successReturn } from '../utils/return';
import populateDetailFields from './populateDetailFields/fromArray';



export async function getNextUserFromQueue({ authTokenId, document, queueId, contextUser }) {
const { success, data: user, errors } = await getUserSafe(authTokenId, contextUser);
if (success === false) {
@@ -881,6 +881,12 @@ export async function create({ authTokenId, document, data, contextUser, upsert,
}

if (resultRecord != null) {
const historyResult = await createHistory(document, 'create', resultRecord._id, user, new Date(), resultRecord, dbSession);
if (historyResult === false) {
await dbSession.abortTransaction();
return errorReturn(`[${document}] Error creating history`);
}

if (MetaObject.Namespace.plan?.useExternalKonsistent !== true) {
try {
tracingSpan?.addEvent('Processing sync Konsistent');
@@ -1349,24 +1355,30 @@ export async function update({ authTokenId, document, data, contextUser, tracing
await runScriptAfterSave({ script: metaObject.scriptAfterSave, data: updatedRecords, user, extraData: { original: existsRecords } });
}

if (MetaObject.Namespace.plan?.useExternalKonsistent !== true) {
try {
logger.debug('Processing Konsistent');
tracingSpan?.addEvent('Processing sync Konsistent');
for await (const record of updatedRecords) {
const original = existsRecords.find(r => r._id === record._id);
const newRecord = omit(record, ['_id', '_createdAt', '_createdBy', '_updatedAt', '_updatedBy']);

for await (const record of updatedRecords) {
const original = existsRecords.find(r => r._id === record._id);
const newRecord = omit(record, ['_id', '_createdAt', '_createdBy', '_updatedAt', '_updatedBy']);
const changedProps = objectsDiff(original, newRecord);

const changedProps = objectsDiff(original, newRecord);
await processIncomingChange(document, record, 'update', user, changedProps, dbSession);
}
} catch (e) {
logger.error(e, `Error on processIncomingChange ${document}: ${e.message}`);
tracingSpan?.addEvent('Error on Konsistent', { error: e.message });
const historyResult = await createHistory(document, 'update', record._id, user, new Date(), changedProps, dbSession);
if (historyResult === false) {
await dbSession.abortTransaction();
return errorReturn(`[${document}] Error creating history`);
}

return errorReturn(`[${document}] Error on Konsistent: ${e.message}`);
if (MetaObject.Namespace.plan?.useExternalKonsistent !== true) {
logger.debug('Processing Konsistent');
tracingSpan?.addEvent('Processing sync Konsistent');
try {
await processIncomingChange(document, record, 'update', user, changedProps, dbSession);
} catch (e) {
logger.error(e, `Error on processIncomingChange ${document}: ${e.message}`);
tracingSpan?.addEvent('Error on Konsistent', { error: e.message });
await dbSession.abortTransaction();

return errorReturn(`[${document}] Error on Konsistent: ${e.message}`);
}
}
}

10 changes: 6 additions & 4 deletions src/imports/konsistent/createHistory.js
Original file line number Diff line number Diff line change
@@ -7,21 +7,22 @@ import { v4 as uuidV4 } from 'uuid';

export default async function createHistory(metaName, action, id, updatedBy, updatedAt, changed, dbSession) {
if (Object.keys(changed).length === 0 || updatedAt == null || updatedBy == null) {
return;
return true;
}

const keysToIgnore = ['_updatedAt', '_createdAt', '_deletedAt', '_updatedBy', '_createdBy', '_deletedBy'];
const meta = MetaObject.Meta[metaName];

const history = MetaObject.Collections[`${metaName}.History`];
if (!history) {
return logger.error(`Can't get History collection from ${metaName}`);
logger.error(`Can't get History collection from ${metaName}`);
return false;
}

const historyData = omitBy(changed, (value, key) => keysToIgnore.includes(key) || meta.fields[key]?.ignoreHistory);
if (Object.keys(historyData).length === 0) {
logger.trace(`No history data for ${metaName}`);
return;
return true;
}

const userDescriptionFields = ["_id"].concat(get(meta, "fields._user.descriptionFields", ["name", "active"]));
@@ -36,8 +37,9 @@ export default async function createHistory(metaName, action, id, updatedBy, upd

try {
await history.insertOne(historyItem, { session: dbSession });
return true;
} catch (e) {
logger.error(e, 'Error on create history');
await dbSession?.abortTransaction();
return false;
}
}
4 changes: 0 additions & 4 deletions src/imports/konsistent/processIncomingChange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { logger } from '@imports/utils/logger';
import createHistory from './createHistory';
import processReverseLookups from './processReverseLookups';
import * as References from './updateReferences';

@@ -34,9 +33,6 @@ export default async function processIncomingChange(

await References.updateRelations(metaName, action, incomingChange._id, incomingChange, dbSession);
logTimeSpent(startTime, `Updated relation references for ${metaName}`);

await createHistory(metaName, action, incomingChange._id, user, new Date(), changedProps, dbSession);
logTimeSpent(startTime, `Created history for ${metaName}`);
} catch (e) {
if ((e as MongoServerError).codeName === 'NoSuchTransaction') {
logger.trace('Transaction was already closed');
Loading