mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-22 00:10:37 +00:00
Fix model persistence store (#3806)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
parent
835588cfd3
commit
c8d569621d
@ -216,7 +216,7 @@ export async function createClient (
|
|||||||
const oldOnConnect: ((apply: boolean) => void) | undefined = conn.onConnect
|
const oldOnConnect: ((apply: boolean) => void) | undefined = conn.onConnect
|
||||||
conn.onConnect = async () => {
|
conn.onConnect = async () => {
|
||||||
// Find all new transactions and apply
|
// Find all new transactions and apply
|
||||||
lastTxTime = await loadModel(conn, lastTxTime, allowedPlugins, configs, hierarchy, model, true)
|
lastTxTime = await loadModel(conn, lastTxTime, allowedPlugins, configs, hierarchy, model, true, txPersistence)
|
||||||
if (lastTxTime === -1) {
|
if (lastTxTime === -1) {
|
||||||
// We need full refresh
|
// We need full refresh
|
||||||
await oldOnConnect?.(false)
|
await oldOnConnect?.(false)
|
||||||
@ -257,6 +257,32 @@ export async function createClient (
|
|||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function tryLoadModel (conn: ClientConnection, lastTxTime: number): Promise<Tx[]> {
|
||||||
|
let res: Tx[] = []
|
||||||
|
try {
|
||||||
|
res = await conn.loadModel(lastTxTime)
|
||||||
|
} catch (err: any) {
|
||||||
|
res = await conn.findAll(
|
||||||
|
core.class.Tx,
|
||||||
|
{ objectSpace: core.space.Model, modifiedOn: { $gt: lastTxTime } },
|
||||||
|
{ sort: { _id: SortingOrder.Ascending, modifiedOn: SortingOrder.Ascending } }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore Employee accounts.
|
||||||
|
function isPersonAccount (tx: Tx): boolean {
|
||||||
|
return (
|
||||||
|
(tx._class === core.class.TxCreateDoc ||
|
||||||
|
tx._class === core.class.TxUpdateDoc ||
|
||||||
|
tx._class === core.class.TxRemoveDoc) &&
|
||||||
|
((tx as TxCUD<Doc>).objectClass === 'contact:class:PersonAccount' ||
|
||||||
|
(tx as TxCUD<Doc>).objectClass === 'core:class:Account')
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
async function loadModel (
|
async function loadModel (
|
||||||
conn: ClientConnection,
|
conn: ClientConnection,
|
||||||
lastTxTime: Timestamp,
|
lastTxTime: Timestamp,
|
||||||
@ -271,28 +297,25 @@ async function loadModel (
|
|||||||
|
|
||||||
let ltxes: Tx[] = []
|
let ltxes: Tx[] = []
|
||||||
if (lastTxTime === 0 && persistence !== undefined) {
|
if (lastTxTime === 0 && persistence !== undefined) {
|
||||||
ltxes = await persistence.load()
|
const memTxes = await persistence.load()
|
||||||
lastTxTime = getLastTxTime(ltxes)
|
const systemTx = memTxes.find((it) => it.modifiedBy === core.account.System && !isPersonAccount(it))
|
||||||
|
if (systemTx !== undefined) {
|
||||||
|
const exists = await conn.findAll(systemTx._class, { _id: systemTx._id }, { limit: 1 })
|
||||||
|
if (exists.length > 0) {
|
||||||
|
lastTxTime = getLastTxTime(memTxes)
|
||||||
|
ltxes = memTxes
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let atxes: Tx[] = []
|
let atxes: Tx[] = []
|
||||||
try {
|
atxes = await tryLoadModel(conn, lastTxTime)
|
||||||
atxes = await conn.loadModel(lastTxTime)
|
|
||||||
} catch (err: any) {
|
|
||||||
atxes = await conn.findAll(
|
|
||||||
core.class.Tx,
|
|
||||||
{ objectSpace: core.space.Model },
|
|
||||||
{ sort: { _id: SortingOrder.Ascending, modifiedOn: SortingOrder.Ascending } }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reload && atxes.length > modelTransactionThreshold) {
|
if (reload && atxes.length > modelTransactionThreshold) {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
if (atxes.length < modelTransactionThreshold) {
|
atxes = ltxes.concat(atxes)
|
||||||
atxes = ltxes.concat(atxes)
|
|
||||||
}
|
|
||||||
|
|
||||||
await persistence?.store(atxes)
|
await persistence?.store(atxes)
|
||||||
|
|
||||||
@ -300,17 +323,6 @@ async function loadModel (
|
|||||||
const userTx: Tx[] = []
|
const userTx: Tx[] = []
|
||||||
console.log('find' + (lastTxTime >= 0 ? 'full model' : 'model diff'), atxes.length, Date.now() - t)
|
console.log('find' + (lastTxTime >= 0 ? 'full model' : 'model diff'), atxes.length, Date.now() - t)
|
||||||
|
|
||||||
// Ignore Employee accounts.
|
|
||||||
function isPersonAccount (tx: Tx): boolean {
|
|
||||||
return (
|
|
||||||
(tx._class === core.class.TxCreateDoc ||
|
|
||||||
tx._class === core.class.TxUpdateDoc ||
|
|
||||||
tx._class === core.class.TxRemoveDoc) &&
|
|
||||||
((tx as TxCUD<Doc>).objectClass === 'contact:class:PersonAccount' ||
|
|
||||||
(tx as TxCUD<Doc>).objectClass === 'contact:class:Account')
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
atxes.forEach((tx) => (tx.modifiedBy === core.account.System && !isPersonAccount(tx) ? systemTx : userTx).push(tx))
|
atxes.forEach((tx) => (tx.modifiedBy === core.account.System && !isPersonAccount(tx) ? systemTx : userTx).push(tx))
|
||||||
|
|
||||||
if (allowedPlugins != null) {
|
if (allowedPlugins != null) {
|
||||||
|
@ -63,19 +63,19 @@ export default async () => {
|
|||||||
filterModel ? [...getPlugins(), ...(getMetadata(clientPlugin.metadata.ExtraPlugins) ?? [])] : undefined,
|
filterModel ? [...getPlugins(), ...(getMetadata(clientPlugin.metadata.ExtraPlugins) ?? [])] : undefined,
|
||||||
{
|
{
|
||||||
load: async () => {
|
load: async () => {
|
||||||
// if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
// const dta = localStorage.getItem('stored_model_' + token) ?? null
|
const dta = localStorage.getItem('stored_model_' + token) ?? null
|
||||||
// if (dta === null) {
|
if (dta === null) {
|
||||||
// return []
|
return []
|
||||||
// }
|
}
|
||||||
// return JSON.parse(dta)
|
return JSON.parse(dta)
|
||||||
// }
|
}
|
||||||
return []
|
return []
|
||||||
},
|
},
|
||||||
store: async (txes) => {
|
store: async (txes) => {
|
||||||
// if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
// localStorage.setItem('stored_model_' + token, JSON.stringify(txes))
|
localStorage.setItem('stored_model_' + token, JSON.stringify(txes))
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user