Skip to content

Commit 69e71eb

Browse files
Merge pull request #1207 from jembi/ft-pending-async
added Pending Async as status for transaction
2 parents 6f53407 + d13259b commit 69e71eb

20 files changed

+262
-64
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openhim-core",
33
"description": "The OpenHIM core application that provides logging and routing of http requests",
4-
"version": "8.2.0",
4+
"version": "8.3.0",
55
"main": "./lib/server.js",
66
"bin": {
77
"openhim-core": "./bin/openhim-core.js"

src/api/apps.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ const createErrorResponse = (ctx, operation, error) => {
4545
const validateId = (ctx, id) => {
4646
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
4747
ctx.statusCode = 400
48-
throw Error(`App id "${id}" is invalid. ObjectId should contain 24 characters`)
48+
throw Error(
49+
`App id "${id}" is invalid. ObjectId should contain 24 characters`
50+
)
4951
}
5052
}
5153

src/api/clients.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,22 @@ export async function getClient(ctx, clientId, property) {
102102
return
103103
}
104104

105-
clientId = unescape(clientId)
106-
107105
try {
108-
const result = await ClientModelAPI.findById(
109-
clientId,
110-
projectionRestriction
111-
)
112-
.lean()
113-
.exec()
106+
let result
107+
if (ctx?.query?.byNamedClientID === 'true') {
108+
result = await ClientModelAPI.findOne(
109+
{clientID: clientId},
110+
projectionRestriction
111+
)
112+
.lean()
113+
.exec()
114+
} else {
115+
clientId = unescape(clientId)
116+
result = await ClientModelAPI.findById(clientId, projectionRestriction)
117+
.lean()
118+
.exec()
119+
}
120+
114121
if (result === null) {
115122
utils.logAndSetResponse(
116123
ctx,

src/api/tasks.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ export async function addTask(ctx) {
216216
)
217217

218218
// Clear the transactions out of the auto retry queue, in case they're in there
219-
return AutoRetryModelAPI
220-
.deleteMany({transactionID: {$in: transactions.tids}})
221-
.exec(err => {
222-
if (err) {
223-
return logger.error(err)
224-
}
225-
})
219+
return AutoRetryModelAPI.deleteMany({
220+
transactionID: {$in: transactions.tids}
221+
}).exec(err => {
222+
if (err) {
223+
return logger.error(err)
224+
}
225+
})
226226
} else {
227227
// rerun task creation not allowed
228228
utils.logAndSetResponse(

src/middleware/messageStore.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as metrics from '../metrics'
88
import * as transactions from '../model/transactions'
99
import * as utils from '../utils'
1010

11-
const { transactionStatus } = transactions
11+
const {transactionStatus} = transactions
1212

1313
function copyMapWithEscapedReservedCharacters(map) {
1414
const escapedMap = {}
@@ -30,7 +30,9 @@ export function storeTransaction(ctx, done) {
3030
const headers = copyMapWithEscapedReservedCharacters(ctx.header)
3131

3232
const tx = new transactions.TransactionModel({
33-
status: transactionStatus.PROCESSING,
33+
status: ctx?.matchingChannel?.isAsynchronousProcess
34+
? transactionStatus.PENDING_ASYNC
35+
: transactionStatus.PROCESSING,
3436
clientID: ctx.authenticated != null ? ctx.authenticated._id : undefined,
3537
channelID: ctx.authorisedChannel._id,
3638
clientIP: ctx.ip,
@@ -87,9 +89,18 @@ export function storeTransaction(ctx, done) {
8789

8890
export function storeResponse(ctx, done) {
8991
const headers = copyMapWithEscapedReservedCharacters(ctx.response.header)
92+
const isAsynchronousProcess =
93+
Boolean(ctx?.matchingChannel?.isAsynchronousProcess) ||
94+
Boolean(ctx?.authorisedChannel?.isAsynchronousProcess)
95+
const status =
96+
isAsynchronousProcess && ctx.response.status < 400
97+
? 202
98+
: ctx.response.status
99+
// if the channel is asynchronous and the response is successful, change the status to 202 otherwise use the original status
100+
ctx.response.status = status
90101

91102
const res = {
92-
status: ctx.response.status,
103+
status,
93104
headers,
94105
body: !ctx.response.body ? '' : ctx.response.body.toString(),
95106
timestamp: ctx.response.timestamp
@@ -202,6 +213,10 @@ export function storeNonPrimaryResponse(ctx, route, done) {
202213
* This should only be called once all routes have responded.
203214
*/
204215
export function setFinalStatus(ctx, callback) {
216+
const isAsynchronousProcess =
217+
Boolean(ctx?.matchingChannel?.isAsynchronousProcess) ||
218+
Boolean(ctx?.authorisedChannel?.isAsynchronousProcess)
219+
205220
let transactionId = ''
206221
if (
207222
ctx.request != null &&
@@ -253,7 +268,11 @@ export function setFinalStatus(ctx, callback) {
253268
ctx.response.status <= 299 &&
254269
routeSuccess
255270
) {
256-
tx.status = transactionStatus.SUCCESSFUL
271+
if (isAsynchronousProcess) {
272+
tx.status = transactionStatus.PENDING_ASYNC
273+
} else {
274+
tx.status = transactionStatus.SUCCESSFUL
275+
}
257276
}
258277
if (
259278
ctx.response.status >= 400 &&

src/middleware/router.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ function sendKafkaRequest(ctx, route) {
656656
const message = {
657657
method: ctx.request.method,
658658
path: ctx.request.url,
659+
pattern: channel.urlPattern,
659660
headers: ctx.request.headers,
660661
body: ctx.body && ctx.body.toString()
661662
}
@@ -669,7 +670,8 @@ function sendKafkaRequest(ctx, route) {
669670
resolve({
670671
status: 200,
671672
body: JSON.stringify(res),
672-
timestamp: +new Date()
673+
timestamp: +new Date(),
674+
headers: {}
673675
})
674676
})
675677
})

src/middleware/sessionStore.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class MongooseStore {
4141
async set(id, data) {
4242
const {session} = this
4343
const record = {_id: id, data, updatedAt: new Date()}
44-
await session.findByIdAndUpdate(id, record, {upsert: true, writeConcern: {w: "majority", wtimeout: 10000}})
44+
await session.findByIdAndUpdate(id, record, {
45+
upsert: true,
46+
writeConcern: {w: 'majority', wtimeout: 10000}
47+
})
4548
return data
4649
}
4750

src/model/channels.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ const ChannelDef = {
112112
type: String,
113113
required: true
114114
},
115+
isAsynchronousProcess: {
116+
type: Boolean,
117+
default: false
118+
},
115119
maxBodyAgeDays: {
116120
type: Number,
117121
min: 1,

src/model/tasks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const TaskSchema = new Schema({
88
status: {
99
type: String,
1010
required: true,
11-
enum: ['Queued', 'Processing', 'Paused', 'Cancelled', 'Completed'],
11+
enum: ['Queued', 'Processing', 'Paused', 'Cancelled', 'Completed', "Pending Async"],
1212
default: 'Queued',
1313
index: true
1414
},

0 commit comments

Comments
 (0)