Skip to content

Commit 6338130

Browse files
committed
fix relations for related remote objects
1 parent d616582 commit 6338130

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

src/domain/make-relations.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import domainEvents from './domain-events'
8-
import { importRemoteCache } from './import-remotes'
8+
import { importModelCache, importRemoteCache } from './import-remotes'
99

1010
const { internalCacheRequest, internalCacheResponse, externalCacheRequest } =
1111
domainEvents
@@ -57,7 +57,7 @@ export const relationType = {
5757
* @param {*} rel
5858
* @returns
5959
*/
60-
oneToOne(model, ds, rel) {
60+
oneToOne (model, ds, rel) {
6161
return this.manyToOne(model, ds, rel)
6262
},
6363

@@ -87,14 +87,14 @@ export const relationType = {
8787
const rds = ds.factory.getRestrictedDataSource(model.modelName)
8888
const relRds = ds.factory.getRestrictedDataSource(rel.modelName, {
8989
isCached: true,
90-
ephemeral: true,
90+
ephemeral: true
9191
})
9292

9393
// if relRds is in the same domain but is remote, this fails
9494
if (rds.namespace !== relRds.namespace) return null
9595

9696
return rds[rel.name]({ args, model, ds: relRds, relation: rel })
97-
},
97+
}
9898
}
9999

100100
/**
@@ -108,39 +108,39 @@ const updateForeignKeys = {
108108
* @param {import('./index').relations[x]} relation
109109
* @param {import('./model-factory').Datasource} ds
110110
*/
111-
async [relationType.manyToOne.name](fromModel, toModels, relation, ds) {
111+
async [relationType.manyToOne.name] (fromModel, toModels, relation, ds) {
112112
return fromModel.update(
113113
{ [relation.foreignKey]: toModels[0].getId() },
114114
false
115115
)
116116
},
117117

118-
async [relationType.oneToOne.name](fromModel, toModels, relation, ds) {
118+
async [relationType.oneToOne.name] (fromModel, toModels, relation, ds) {
119119
return this[relationType.manyToOne.name](fromModel, toModels, relation, ds)
120120
},
121121

122-
async [relationType.oneToMany.name](fromModel, toModels, relation, ds) {
122+
async [relationType.oneToMany.name] (fromModel, toModels, relation, ds) {
123123
return Promise.all(
124124
toModels.map(async m => {
125125
const model = await ds.find(m.id || m.getId())
126126
return ds.save(m.id, {
127127
...model,
128-
[relation.foreignKey]: fromModel.getId(),
128+
[relation.foreignKey]: fromModel.getId()
129129
})
130130
})
131131
)
132132
},
133133

134-
async [relationType.containsMany.name](fromModel, toModels, relation, ds) {
134+
async [relationType.containsMany.name] (fromModel, toModels, relation, ds) {
135135
toModels.map(model =>
136136
model.update({ [relation.foreignKey]: fromModel.getId() })
137137
)
138138
},
139139

140-
async [relationType.custom.name](fromModel, toModels, relation, ds) {
140+
async [relationType.custom.name] (fromModel, toModels, relation, ds) {
141141
const customFn = fromModel[`${relation}UpdateForeignKeys`]
142142
if (customFn === 'function') customFn(toModels, relation, ds)
143-
},
143+
}
144144
}
145145

146146
/**
@@ -151,9 +151,13 @@ const updateForeignKeys = {
151151
* @param {import('./datasource').default} ds
152152
* @returns
153153
*/
154-
async function createNewModels(args, fromModel, relation, ds) {
154+
async function createNewModels (args, fromModel, relation, ds) {
155155
if (args.length > 0) {
156-
const { UseCaseService, importRemoteCache } = require('.')
156+
const {
157+
UseCaseService,
158+
importRemoteCache,
159+
default: ModelFactory
160+
} = require('.')
157161
const service = UseCaseService(relation.modelName.toUpperCase())
158162
const newModels = await Promise.all(
159163
args.map(arg => service.createModel(arg))
@@ -173,7 +177,7 @@ async function createNewModels(args, fromModel, relation, ds) {
173177
* @param {import("./event-broker").EventBroker} broker
174178
* @returns {Promise<import(".").Event>} source model
175179
*/
176-
export function requireRemoteObject(model, relation, broker, ...args) {
180+
export function requireRemoteObject (model, relation, broker, ...args) {
177181
const request = internalCacheRequest(relation.modelName)
178182
const response = internalCacheResponse(relation.modelName)
179183

@@ -193,7 +197,7 @@ export function requireRemoteObject(model, relation, broker, ...args) {
193197
modelId: id,
194198
relation,
195199
model,
196-
args,
200+
args
197201
}
198202

199203
return new Promise(async function (resolve) {
@@ -209,7 +213,7 @@ export function requireRemoteObject(model, relation, broker, ...args) {
209213
* @param {import('.').relations[x]} relation
210214
* @returns {boolean}
211215
*/
212-
function isRelatedModelLocal(relation) {
216+
function isRelatedModelLocal (relation) {
213217
return require('.')
214218
.default.getModelSpecs()
215219
.filter(spec => !spec.isCached)
@@ -222,7 +226,7 @@ function isRelatedModelLocal(relation) {
222226
* @param {import("./index").relations} relations
223227
* @param {import("./datasource").default} datasource
224228
*/
225-
export default function makeRelations(relations, datasource, broker) {
229+
export default function makeRelations (relations, datasource, broker) {
226230
if (Object.getOwnPropertyNames(relations).length < 1) return
227231

228232
return Object.keys(relations)
@@ -231,9 +235,8 @@ export default function makeRelations(relations, datasource, broker) {
231235

232236
const rel = {
233237
...relations[relation],
234-
modelName: relModelName.toUpperCase(),
235-
domain: require('.').default.getModelSpec(relModelName).domain,
236-
name: relation,
238+
modelName: relModelName,
239+
name: relation
237240
}
238241

239242
try {
@@ -245,15 +248,13 @@ export default function makeRelations(relations, datasource, broker) {
245248

246249
return {
247250
// the relation function
248-
async [relation](...args) {
251+
async [relation] (...args) {
252+
const local = isRelatedModelLocal(relModelName)
253+
if (!local) await importModelCache(relModelName)
249254
// Get existing (or create temp) datasource of related object
250-
const local = isRelatedModelLocal(rel)
251255
const createNew = args?.length > 0
252256

253-
if (!local) {
254-
// the ds is for a remote object, fetch the code for it.
255-
await importRemoteCache(rel.modelName)
256-
} else if (createNew && rel.type !== 'custom') {
257+
if (createNew && rel.type !== 'custom') {
257258
// fetch the local ds and create the models
258259
const ds = datasource.factory.getDataSource(rel.modelName)
259260
return await createNewModels(args, this, rel, ds)
@@ -262,7 +263,7 @@ export default function makeRelations(relations, datasource, broker) {
262263
// if the object is remote, we now have its code
263264
const ds = datasource.factory.getDataSource(
264265
rel.modelName,
265-
rel.domain
266+
require('.').default.getModelSpec(relModelName).domain
266267
)
267268

268269
const models = await relationType[rel.type](this, ds, rel, args)
@@ -283,7 +284,7 @@ export default function makeRelations(relations, datasource, broker) {
283284
}
284285

285286
return models
286-
},
287+
}
287288
}
288289
} catch (error) {
289290
console.error({ fn: makeRelations.name, error })

src/domain/thread-pool.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class ThreadPool extends EventEmitter {
262262

263263
const errorFn = AsyncResource.bind(error => {
264264
pool.jobTime(job.stopTimer())
265-
console.error({ fn: this.run.name, error })
265+
console.error({ fn: this.run.name, msg: 'dead thread', error })
266266
unsubscribe('exit', exitFn)
267267
unsubscribe('message', messageFn)
268268
pool.incrementErrorCount()
@@ -303,6 +303,7 @@ export class ThreadPool extends EventEmitter {
303303
}
304304
console.log('aegis up', msg)
305305
pool.connectEventChannel(worker, eventChannel)
306+
pool.threads.push(thread)
306307
resolve(thread)
307308
})
308309
})
@@ -321,7 +322,7 @@ export class ThreadPool extends EventEmitter {
321322
// call thread.run
322323
this.waitingJobs.shift()(thread)
323324
// return to pool
324-
else this.freeThreads.push(thread)
325+
else this.checkin(thread)
325326
}
326327

327328
/**
@@ -335,11 +336,8 @@ export class ThreadPool extends EventEmitter {
335336
file: this.file,
336337
workerData: this.workerData
337338
})
338-
339-
if (thread) {
340-
this.threads.push(thread)
341-
return thread
342-
}
339+
340+
if (thread) return thread
343341

344342
throw new Error('error creating thread')
345343
}
@@ -519,6 +517,24 @@ export class ThreadPool extends EventEmitter {
519517
)
520518
}
521519

520+
checkout () {
521+
if (this.freeThreads.length > 0) {
522+
const thread = this.freeThreads.shift()
523+
const threadsInUse = this.threads.length - this.freeThreads.length
524+
console.debug(`thread checked-out, total in-use now ${threadsInUse}`)
525+
return thread
526+
}
527+
}
528+
529+
checkin (thread) {
530+
if (thread) {
531+
this.freeThreads.push(thread)
532+
const threadsInUse = this.threads.length - this.freeThreads.length
533+
console.debug(`thread checked-in, total in-use now ${threadsInUse}`)
534+
}
535+
return this
536+
}
537+
522538
/**
523539
* Spin up a new thread if needed and available.
524540
*/
@@ -553,7 +569,7 @@ export class ThreadPool extends EventEmitter {
553569
...options
554570
})
555571

556-
let thread = this.freeThreads.shift()
572+
let thread = this.checkout()
557573

558574
if (!thread) thread = await this.allocate()
559575

0 commit comments

Comments
 (0)