Skip to content

Commit a6fffad

Browse files
authored
Merge branch 'main' into sync-bli
2 parents bca08ce + e9fb107 commit a6fffad

File tree

12 files changed

+337
-320
lines changed

12 files changed

+337
-320
lines changed

forkrun.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
#include <unistd.h>
43
#include <sys/types.h>
4+
#include <unistd.h>
55

66
#define BUFSIZE 256
77

8-
char* envvar_cmd = "FORKRUN_CMD";
9-
char* envvar_arg = "FORKRUN_ARG";
8+
char *envvar_cmd = "FORKRUN_CMD";
9+
char *envvar_arg = "FORKRUN_ARG";
1010
char cmd[BUFSIZE];
1111
char arg[BUFSIZE];
1212

13-
int main (void) {
14-
pid_t pid;
15-
16-
if ((pid = fork()) == 0) {
17-
snprintf(cmd, BUFSIZE, "%s", getenv(envvar_cmd));
18-
snprintf(arg, BUFSIZE, "%s", getenv(envvar_arg));
19-
char* const argv[] = { cmd, arg, NULL };
13+
int main(void) {
14+
pid_t pid;
15+
if ((pid = fork()) == 0) {
16+
snprintf(cmd, BUFSIZE, "%s", getenv(envvar_cmd));
17+
snprintf(arg, BUFSIZE, "%s", getenv(envvar_arg));
18+
char *const argv[] = {cmd, arg, NULL};
2019

21-
if (execv(cmd, argv) < 0) {
22-
perror("execv error");
23-
}
24-
} else if (pid < 0) {
25-
perror("fork error");
26-
} else {
27-
return EXIT_SUCCESS;
20+
if (execv(cmd, argv) < 0) {
21+
perror("execv error");
2822
}
23+
} else if (pid < 0) {
24+
perror("fork error");
25+
} else {
26+
return EXIT_SUCCESS;
27+
}
2928

30-
return EXIT_FAILURE;
29+
return EXIT_FAILURE;
3130
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"core-js": "^3"
2424
},
2525
"dependencies": {
26-
"@assemblyscript/loader": "0.21.1",
26+
"@assemblyscript/loader": "0.25.2",
2727
"@octokit/core": "4.0.5",
28-
"@octokit/rest": "19.0.4",
28+
"@octokit/rest": "19.0.5",
2929
"acme-client": "^5.0.0",
3030
"as-bind": "0.8.2",
31-
"assemblyscript": "0.21.1",
31+
"assemblyscript": "0.25.2",
3232
"cors": "^2.8.5",
3333
"dotenv": "16.0.1",
34-
"express": "4.18.1",
34+
"express": "4.18.2",
3535
"express-fileupload": "^1.4.0",
3636
"express-jwt": "7.7.5",
3737
"import-fresh": "3.3.0",
@@ -59,7 +59,7 @@
5959
"@babel/core": "7.18.10",
6060
"@babel/node": "7.18.10",
6161
"@babel/preset-env": "7.18.10",
62-
"@types/node": "16.11.52",
62+
"@types/node": "16.18.11",
6363
"babel-loader": "8.2.5",
6464
"chalk": "5.0.1",
6565
"cross-var": "1.1.0",

src/adapters/datasources/datasource-mongodb.js

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ const processQuery = qpm({
2121
const url = process.env.MONGODB_URL || 'mongodb://localhost:27017'
2222

2323
const dsOptions = configRoot.adapters.datasources.DataSourceMongoDb.options || {
24-
runOffline: false,
25-
numConns: 2,
24+
runOffline: true,
25+
numConns: 2
2626
}
2727

2828
const cacheSize = configRoot.adapters.cacheSize || 3000
29+
let connPool
2930

3031
/**
3132
* @type {Map<string,MongoClient>}
@@ -43,7 +44,7 @@ const mongoOpts = {
4344
* even when the database is offline.
4445
*/
4546
export class DataSourceMongoDb extends DataSource {
46-
constructor(map, name, namespace, options = {}) {
47+
constructor (map, name, namespace, options = {}) {
4748
super(map, name, namespace, options)
4849
this.cacheSize = cacheSize
4950
this.mongoOpts = mongoOpts
@@ -52,47 +53,56 @@ export class DataSourceMongoDb extends DataSource {
5253
this.url = url
5354
}
5455

55-
connect(client) {
56-
return async function () {
57-
let timeout = false
58-
const timerId = setTimeout(() => {
59-
timeout = true
60-
}, 500)
61-
await client.connect()
62-
clearTimeout(timerId)
63-
if (timeout) throw new Error('mongo conn timeout')
64-
}
65-
}
66-
67-
async connectionPool() {
56+
/**
57+
*
58+
* @returns {Promise<import('mongodb').Db>}
59+
*/
60+
async connectionPool () {
6861
return new Promise((resolve, reject) => {
6962
if (this.db) return resolve(this.db)
7063
MongoClient.connect(
7164
this.url,
7265
{
7366
...this.mongoOpts,
7467
poolSize: dsOptions.numConns || 2,
68+
connectTimeoutMS: 500
7569
},
76-
(err, database) => {
70+
(err, db) => {
7771
if (err) return reject(err)
78-
resolve((this.db = database.db(this.namespace)))
72+
this.db = db(this.namespace)
73+
resolve(this.db)
7974
}
8075
)
8176
})
8277
}
8378

84-
async connection() {
79+
connect (client) {
80+
return async function () {
81+
let timeout = false
82+
const timerId = setTimeout(() => {
83+
timeout = true
84+
}, 500)
85+
await client.connect()
86+
clearTimeout(timerId)
87+
if (timeout) throw new Error('mongo conn timeout')
88+
}
89+
}
90+
91+
async connection () {
8592
try {
8693
while (connections.length < (dsOptions.numConns || 1)) {
87-
const client = new MongoClient(this.url, this.mongoOpts)
94+
const client = new MongoClient(this.url, {
95+
...this.mongoOpts,
96+
connectTimeoutMS: 500
97+
})
8898
const thresholds = {
8999
default: {
90100
errorRate: 1,
91101
callVolume: 1,
92102
intervalMs: 10000,
93103
testDelay: 300000,
94-
//fallbackFn: () => client.emit('connectionClosed')
95-
},
104+
fallbackFn: () => console.log('circuit open')
105+
}
96106
}
97107
const breaker = CircuitBreaker(
98108
'mongodb.connect',
@@ -145,7 +155,7 @@ export class DataSourceMongoDb extends DataSource {
145155
.createIndexes(indexOperations)
146156
}
147157

148-
async find(id) {
158+
async find (id) {
149159
try {
150160
return (await this.collection()).findOne({ _id: id })
151161
} catch (error) {
@@ -163,7 +173,7 @@ export class DataSourceMongoDb extends DataSource {
163173
* @param {*} id
164174
* @param {*} data
165175
*/
166-
async save(id, data) {
176+
async save (id, data) {
167177
try {
168178
await (
169179
await this.collection()
@@ -186,19 +196,19 @@ export class DataSourceMongoDb extends DataSource {
186196
* @param {number} highWaterMark num of docs per batch write
187197
* @returns
188198
*/
189-
createWriteStream(filter = {}, highWaterMark = HIGHWATERMARK) {
199+
createWriteStream (filter = {}, highWaterMark = HIGHWATERMARK) {
190200
try {
191201
let objects = []
192202
const ctx = this
193203

194-
async function upsert() {
204+
async function upsert () {
195205
const operations = objects.map(obj => {
196206
return {
197207
replaceOne: {
198208
filter: { ...filter, _id: obj.id },
199209
replacement: { ...obj, _id: obj.id },
200-
upsert: true,
201-
},
210+
upsert: true
211+
}
202212
}
203213
})
204214

@@ -217,17 +227,17 @@ export class DataSourceMongoDb extends DataSource {
217227
const writable = new Writable({
218228
objectMode: true,
219229

220-
async write(chunk, _encoding, next) {
230+
async write (chunk, _encoding, next) {
221231
objects.push(chunk)
222232
// if true time to flush buffer and write to db
223233
if (objects.length >= highWaterMark) await upsert()
224234
next()
225235
},
226236

227-
end(chunk, _, done) {
237+
end (chunk, _, done) {
228238
objects.push(chunk)
229239
done()
230-
},
240+
}
231241
})
232242

233243
writable.on('finish', async () => await upsert())
@@ -247,7 +257,7 @@ export class DataSourceMongoDb extends DataSource {
247257
*
248258
* @returns {Promise<import('mongodb').AbstractCursor>}
249259
*/
250-
async mongoFind({ filter, sort, limit, aggregate, skip } = {}) {
260+
async mongoFind ({ filter, sort, limit, aggregate, skip } = {}) {
251261
console.log({ fn: this.mongoFind.name, filter })
252262
let cursor = aggregate
253263
? (await this.collection()).aggregate(aggregate)
@@ -287,7 +297,6 @@ export class DataSourceMongoDb extends DataSource {
287297
*/
288298
streamList({ writable, serialize, transform, options }) {
289299
try {
290-
let pipeArgs = []
291300

292301
const serializer = new Transform({
293302
writableObjectMode: true,
@@ -340,15 +349,12 @@ export class DataSourceMongoDb extends DataSource {
340349
})
341350

342351
return new Promise(async (resolve, reject) => {
352+
const pipeArgs = []
343353
const readable = (await this.mongoFind(options)).stream()
344354

345-
readable.on('error', reject)
346-
readable.on('end', resolve)
347-
348355
// optionally transform db stream then pipe to output
349356
pipeArgs.push(readable)
350-
if (transform) pipeArgs.push(transform)
351-
if (serialize) pipeArgs.push(serializer)
357+
352358
if (options.page) {
353359
const count = ~~(await this.mongoCount(options.filter))
354360
pipeArgs.push(paginate)
@@ -360,10 +366,9 @@ export class DataSourceMongoDb extends DataSource {
360366
() => console.log('paginated query', options)
361367
)
362368
}
363-
pipeArgs.push(writable)
364-
365-
// perform pipe operations
366-
pipeArgs.reduce((prevVal, currVal) => prevVal.pipe(currVal))
369+
370+
return pipeArgs
371+
367372
})
368373
} catch (error) {
369374
console.error({
@@ -382,7 +387,7 @@ export class DataSourceMongoDb extends DataSource {
382387
* @override
383388
* @param {import('../../domain/datasource').listOptions} param
384389
*/
385-
async list(param) {
390+
async list (param) {
386391
try {
387392
const options = this.processOptions(param)
388393
if (0 < ~~query.__page) {
@@ -401,7 +406,7 @@ export class DataSourceMongoDb extends DataSource {
401406
}
402407
console.log({ options })
403408

404-
if (writable && !query.__json)
409+
if (streamRequested && !query.__json)
405410
this.streamList({ writable, serialize, transform, options })
406411
else {
407412
const data = (await this.mongoFind(options)).toArray()
@@ -424,19 +429,19 @@ export class DataSourceMongoDb extends DataSource {
424429
*
425430
* @override
426431
*/
427-
async count() {
432+
async count () {
428433
return {
429434
total: await this.countDb(),
430435
cached: this.getCacheSize(),
431-
bytes: this.getCacheSizeBytes(),
436+
bytes: this.getCacheSizeBytes()
432437
}
433438
}
434439

435440
/**
436441
* @override
437442
* @returns
438443
*/
439-
async countDb() {
444+
async countDb () {
440445
return (await this.collection()).countDocuments()
441446
}
442447

@@ -447,7 +452,7 @@ export class DataSourceMongoDb extends DataSource {
447452
* @override
448453
* @param {*} id
449454
*/
450-
async delete(id) {
455+
async delete (id) {
451456
try {
452457
await (await this.collection()).deleteOne({ _id: id })
453458
} catch (error) {

0 commit comments

Comments
 (0)