I am trying to implement below steps in node.js
- the order can be placed and paid using my rest-API.
- Once the order status changed to PAID from PLACED, it will be enqueued that order in Queue data structure.
- One separate Thread is continuously running to check whether there is order in the Queue or not. if there is order in the queue, it will be dequeued and start to PREPARED, SERVED and COLLECTED. Here, there may be many order's in the Queue or not at all.
I am sharing my code here
//order.js
var Queue = require('queuejs');
var queue = new Queue();
queue.enq("hello");
const threads = require('threads');
const spawn = threads.spawn;
const thread = spawn(function(){});
exports.payOrder = function (req, res) {
//after changing order status to PAID in database, I enq it in queue.
queue.enq(id);
};
exports.processOrder = function () {
spawn("./routes/helper.js").send();
};
exports.queue = queue;
//helper.js
const Sync = require('sync');
const order = require('./order');
const dao = require('./dao');
const config = require('./config');
Sync(function () {
console.log("in sync");
while (true) {
// I got queue size always 0(zero), even though I called pay api(meaning put order into the queue by calling api)
if (order.queue.size() > 0) {
console.log("here");
var id = order.queue.deq();
var query = "query string";
dao.client.execute(query, [config.orderStatus.preparing, id], {prepare: true}, function (err, result) {
Sync.sleep(5000);
dao.client.execute(query, [config.orderStatus.served, id], {prepare: true}, function (err, result) {
Sync.sleep(5000);
dao.client.execute(query, [config.orderStatus.collected, id], {prepare: true}, function (err, result) {
Sync.sleep(5000);
});
});
});
}
}
});
I have exported queue in order.js but it is not sharing reference with helper thread.
I have applied many ways to achieve above functionality but not able to implement it successfully. Can anyone please suggest me the solution?
via vicky miyani
No comments:
Post a Comment