Created
May 4, 2021 09:07
-
-
Save blex18/0ff46b4931ef965e1ef62a60a5704a68 to your computer and use it in GitHub Desktop.
SO_67375281
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
const moment = require('moment'); | |
const Schema = mongoose.Schema; | |
const user = process.env.SO_USER; | |
const pass = process.env.SO_PASS; | |
const uri = process.env.SO_URI; | |
const throng = require('throng') | |
mongoose.Promise = global.Promise; | |
mongoose.connect(uri, { | |
useNewUrlParser: true, | |
useCreateIndex: true, | |
useFindAndModify: false, | |
useUnifiedTopology: true, | |
auth: {user: user, password: pass} | |
}); | |
mongoose.connect(uri, {auth: {user: user, password: pass}, useNewUrlParser: true, useUnifiedTopology: true}); | |
const MessageS = new Schema( | |
{ | |
accountID: {type: Schema.Types.ObjectId, ref: "Account", required: true}, | |
listingID: {type: Schema.Types.ObjectId, ref: "Listing", required: true}, | |
messageRuleID: {type: Schema.Types.ObjectId, ref: "MessageRule", required: true}, | |
reservationID: {type: Schema.Types.ObjectId, ref: "Reservation", required: true}, | |
lockedAt: Date, | |
status: String | |
}, | |
{timestamps: true} | |
); | |
MessageS.index( | |
{ | |
listingID: 1, | |
messageRuleID: 1, | |
reservationID: 1 | |
}, | |
{unique: true, name: "Message_index_0"} | |
); | |
const Message = mongoose.model('Message', MessageS); | |
const messageQuery = { | |
accountID: "604f9355eeab332490184532", | |
listingID: "604f9358be89f997345b238d", | |
messageRuleID: "607d44e75d54c700041f38e1", | |
reservationID: "605118b7694b9765f49787e1" | |
}; | |
async function start() { | |
const time = moment().toDate(); | |
try { | |
const message = await Message.create(messageQuery); | |
console.log("CREATED A MESSAGE", message._id); | |
} catch (error) { | |
if (error.code !== 11000) { | |
// ignore duplicate key error | |
throw error; | |
} else { | |
console.log("DUPLICATE MESSAGE"); | |
} | |
} | |
const cutoff = moment().subtract( | |
1, | |
"minutes" | |
); | |
const message = await Message.findOneAndUpdate( | |
{ | |
...messageQuery, | |
status: {$nin: ["disabled", "sent"]}, | |
$or: [ | |
{lockedAt: {$exists: false}}, | |
{lockedAt: null}, | |
{lockedAt: {$lte: cutoff}} | |
] | |
}, | |
{lockedAt: moment().toDate()} | |
); | |
// If no message is defined, then it's either sent, disabled, or is locked | |
if (!message) { | |
console.log("Existing, message is either sent, disabled, or is locked"); | |
return; | |
} | |
await Message.findOneAndUpdate(messageQuery, { | |
status: "sent", | |
$unset: {lockedAt: ""} // Not sure this is needed | |
}); | |
console.log("SEND MESSAGE", time, message._id); | |
} | |
let id = 0; | |
const db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.on('close', process.exit ) | |
db.once('open', function() { | |
throng({ | |
worker: start, | |
count: 50, | |
lifetime: Infinity | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATED A MESSAGE 60910db8baf9631238883ec6 | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
SEND MESSAGE 2021-05-04T09:02:48.000Z 60910db8baf9631238883ec6 | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked | |
DUPLICATE MESSAGE | |
DUPLICATE MESSAGE | |
Existing, message is either sent, disabled, or is locked | |
Existing, message is either sent, disabled, or is locked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "SO_67375281", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"moment": "^2.29.1", | |
"mongoose": "^5.12.7", | |
"throng": "^5.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment