Skip to content

Instantly share code, notes, and snippets.

@wootwoot1234
Forked from blex18/index.js
Last active May 5, 2021 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wootwoot1234/49cb7d082850d93f8cd03da164644cfb to your computer and use it in GitHub Desktop.
Save wootwoot1234/49cb7d082850d93f8cd03da164644cfb to your computer and use it in GitHub Desktop.
SO_67375281
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: "604f9355eeab332490184533",
listingID: "604f9358be89f997345b2380",
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', async function() {
await Message.deleteOne(messageQuery);
throng({
worker: start,
count: 50,
lifetime: Infinity
});
});
CREATED A MESSAGE 60920fb1900221c1b209c749
CREATED A MESSAGE 60920fb141ea1fc1af42b842
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb18bb100c1b0dcc17c
CREATED A MESSAGE 60920fb11574c3c1b329eb32
Existing, message is either sent, disabled, or is locked
CREATED A MESSAGE 60920fb15ffa3dc1b477d3fb
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
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
SEND MESSAGE 2021-05-05T03:23:29.622Z 60920fb15ffa3dc1b477d3fb
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
CREATED A MESSAGE 60920fb16405f7c1bdacf384
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb178a11ac1be67bab3
CREATED A MESSAGE 60920fb120576ec1bb79ec0a
Existing, message is either sent, disabled, or is locked
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb189993bc1c497104b
DUPLICATE MESSAGE
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb173c401c1c71a0878
Existing, message is either sent, disabled, or is locked
SEND MESSAGE 2021-05-05T03:23:29.791Z 60920fb120576ec1bb79ec0a
DUPLICATE MESSAGE
DUPLICATE MESSAGE
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb14b22bac1cc805be6
Existing, message is either sent, disabled, or is locked
DUPLICATE MESSAGE
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb162faf0c1cda0a00a
SEND MESSAGE 2021-05-05T03:23:29.828Z 60920fb189993bc1c497104b
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
Existing, message is either sent, disabled, or is locked
Existing, message is either sent, disabled, or is locked
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb110591ac1cb0451b8
CREATED A MESSAGE 60920fb1a0629dc1ca3f4364
Existing, message is either sent, disabled, or is locked
Existing, message is either sent, disabled, or is locked
CREATED A MESSAGE 60920fb1ccd543c1d593e12b
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
CREATED A MESSAGE 60920fb2d315ccc1d7122da4
DUPLICATE MESSAGE
SEND MESSAGE 2021-05-05T03:23:29.934Z 60920fb1ccd543c1d593e12b
Existing, message is either sent, disabled, or is locked
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb2a57a81c1c8e47b8b
CREATED A MESSAGE 60920fb272b17fc1d2ea65b6
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb22dc775c1ce0cf984
DUPLICATE MESSAGE
DUPLICATE MESSAGE
CREATED A MESSAGE 60920fb20b5e50c1e0a0e3ed
SEND MESSAGE 2021-05-05T03:23:29.975Z 60920fb272b17fc1d2ea65b6
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
CREATED A MESSAGE 60920fb2db0890c1d1569ee6
Existing, message is either sent, disabled, or is locked
Existing, message is either sent, disabled, or is locked
DUPLICATE MESSAGE
DUPLICATE MESSAGE
DUPLICATE MESSAGE
SEND MESSAGE 2021-05-05T03:23:30.034Z 60920fb20b5e50c1e0a0e3ed
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
SEND MESSAGE 2021-05-05T03:23:30.070Z 60920fb2db0890c1d1569ee6
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
{
"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