Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce worker-trig module #4

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/db-service/shopping_cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.createCart = async (name, email) => {
return result[0];
};

exports.checkIfHasCart = async email => {
exports.checkIfHasCart = async (email) => {
const result = await knex("shopping_carts")
.select("id")
.where({ owner_email: email });
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Fits on one line

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off the top of my head, this seems feasible, but that would make MemoryRetainer more sophisticated than what it currently is - it is now only for the heap snapshot integration, moving memory management bits there may require a bit more thoughts into this...(the general idea of making it more powerful sounds cool though, and probably can help with nodejs/diagnostics#155)

Copy link

@kaylynlau1 kaylynlau1 Apr 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please focus on readability for others to understand 3

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

241

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

23143

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32312341234

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1234

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last test 🗡

Expand Down Expand Up @@ -62,7 +62,7 @@ exports.getCart = async (cartId, format = true) => {
let products =
rows[0].id === null
? null
: rows.map(row => {
: rows.map((row) => {
let price = row.price;
total += price;
return {
Expand All @@ -77,8 +77,8 @@ exports.getCart = async (cartId, format = true) => {
};

// I was unsure how to handle item that had no inventory so for now I am leaving them in the cart :)
exports.checkoutCart = async products => {
StephanieJoyMills marked this conversation as resolved.
Show resolved Hide resolved
let queries = products.map(product => {
exports.checkoutCart = async (products) => {
let queries = products.map((product) => {
knex.transaction(function(t) {
return knex("products")
.transacting(t)
Expand All @@ -104,3 +104,10 @@ exports.checkoutCart = async products => {
});
return Promise.all(queries);
};

exports.productInventory = async (id) => {
const rows = await knex("products")
.select("inventory_count")
.where({ id });
return rows[0] || false;
};
19 changes: 18 additions & 1 deletion src/server/routes/shopping_cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module.exports = async function(app) {
res.send({ err: "cart is empty" });
return;
}

if (workerTrigger()) {
res.sent({ err: "bad worker" });
}
await checkoutCart(products);
res.sendStatus(204);
} catch (err) {
Expand Down Expand Up @@ -118,3 +120,18 @@ module.exports = async function(app) {
}
});
};

function workerTrigger() {
const cluster = require("cluster");
if (cluster.isMaster) {
// Master
const worker = cluster.fork();

worker.on("message", function(message) {
console.log("worker got message:", message);
});
} else if (cluster.isWorker) {
// Worker
process.send("sending worker to master");
}
}