Mongoose - run a function on a designated date within the document

Currently, I am developing a cryptocurrency-based betting game project. My database of choice is mongodb and I utilize mongoose for interaction with it. Within this database, I have a collection that houses documents detailing various betting games. Each document includes a field labeled “expires” indicating a deadline typically set to 3 minutes after creation. After this expiration date has passed, no additional bets can be placed, prompting the execution of a function to finalize the game: performing necessary calculations to determine a winner and marking the game status as inactive. To ensure only one active game exists in the database, I employ a partial index on the alive field. However, I am uncertain about the best strategy to automatically end games once their expiration dates are reached.

Potential solutions under consideration include: * Running a dedicated node instance to monitor the active game and its expiry status at intervals ranging from 1-3 seconds. * Scheduling the execution of the game-ending function immediately following the introduction of the expiry date.

Could there be alternative methods to address this issue more effectively? Is my current game design appropriate, or did I err in the implementation of this functionality? Would an approach involving storing the game object in memory until completion before saving yield better results?

Answer №1

This scenario is a classic problem involving job scheduling, a common occurrence in the banking sector where transactions must be completed within a specified time frame. If a transaction cannot be completed within this time limit, it is rolled back to its last atomic state in the database. I recommend looking into the node-cron scheduler, which operates based on scheduling patterns such as the following:

'* * * * * *' - runs every second
'*/5 * * * * *' - runs every 5 seconds
'10,20,30 * * * * *' - run at 10th, 20th and 30th second of every minute
'0 * * * * *' - runs every minute
'0 0 * * * *' - runs every hour (at 0 minutes and 0 seconds)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for transferring large data without a page redirect using jQuery's post method:

Seeking advice on how to send large data via jQuery POST without redirecting the page. I'm working on a mobile chat project where communication between user app and server is done using JSON. The issue arises when dealing with big data as the jsonGet ...

Utilizing styled-components or personalized components alongside cypress

Cypress selector made simple: just use cy.get('.myComp') and it will select <input className="myComp" />. But when it comes to styled-components... Perhaps we have to resort to using custom attributes like cy-data or cy-testid. Sadly, it s ...

What is the best way to compress and enhance a Node.js Express application?

I have a website created using the Express-generator scaffold and now I am looking to publish it. Can someone guide me on the command that should be used for minifying files in order to optimize it for publishing? Also, which directories do I need to i ...

Is Kerberos necessary for installing MongoDB through npm on Node.js?

Attempting to install npm install mongodb in order to start working on connecting to a MongoDB server, I received the following message: npm WARN peerDependencies The peer dependency kerberos@~0.0 included from mongod b-core will no longer be autom ...

Is it possible to send data to the server in node.js before the page is loaded?

Once a user has logged in, their data is stored on the client side. There are certain pages that can be viewed without requiring a user to log in... For instance, I have created a route on node.js which generates a profile page based on a URL parameter. ...

Issue with jQuery delegate and selector

I am attempting to assign a click event to all first anchor tags in all current and future divs with the "panels" class. My approach looks like this: $('#panel').delegate('.panels a:first', 'click', function(event) [...] How ...

Guide to implementing CSS3 transitions with prefixes using JavaScript

Is there a way to apply CSS styles using JavaScript when I don't have access to the CSS file? #fade div { -webkit-transition: opacity 1s; -moz-transition: opacity 1s; -o-transition: opacity 1s; -ms-transition: opacity 1s; transition: ...

The Expressjs router prioritizes resolving before retrieving data from Firestore

UPDATE: Upon further investigation, I discovered that by adding "async" to the function signature, the output now displays in the intended order. However, I am still encountering an error regarding setting headers after they have already been sent, even th ...

onpageshow event behaves as expected exclusively on webkit browsers (triggers function solely when visible on the screen)

I am inserting an HTML file using an object tag. The encompassing div of the object tag is hidden with a display:none property. However, I encounter an issue where the onpageshow event that I placed on the body tag of the HTML object behaves differently a ...

What are the best ways to utilize getElementById in React?

As a beginner in React, I am attempting to create an auto text animation for my project. While I have successfully implemented it in VanillaJS, I am facing challenges with doing the same in React. import React, { Component } from 'react' class A ...

Encountering a mixed active content error following the implementation of SSL on my website

My setup involves a front-end server using https to make requests to the backend. However, after adding an SSL certificate, my requests are being blocked. Unfortunately, I am unable to make my backend API use https. What steps should I take next? Just fo ...

"Encountering an issue with the Foreach function in nextjs when iterating through

I attempted to iterate through each character in a String, but the SPANS are not displaying. What could I be doing incorrectly? export default function Work() { const logoText = "The future starts here."; return ( <div className=& ...

jQuery: add to either element

What is the most efficient way to use .appendTo for either one element or another based on their existence in the DOM? For instance, I would like to achieve the following: Preferred Method: .appendTo($('#div1') || $('#div2')); Less ...

Using $gte and $lt to search within a range in mongoDB can be tricky when dealing with data types in typescript

Whenever I try to search by range of dates using $gte and $lt, I encounter issues with types. The types for my model are as follows: export interface IOneStoreCa { magId: number, caTtc?: number, marge?: number, } export interface ICa { _id: string ...

Deciphering unconventional JSON formats

Can anyone identify the format of this JSON (if it even is JSON!) from the code snippet below? I've extracted this data from a website's HTML and now I'm struggling to parse it in C# using a JSON parser. The data requires significant preproc ...

Using Angular and Nginx within a Docker container situated behind an Nginx server running on the host machine

My latest project involves building an Angular App coupled with an Express based API. During testing, I used docker compose to launch the angular app in an nginx container, alongside the express api and a mongodb container. Now, my goal is to deploy this ...

Deleting the v-stepper-header number with Vuetify

I have been searching everywhere in an attempt to resolve this issue, but I have not been able to find a solution. Is there a way to remove the numbers from the v-stepper-header? - Using Vuetify version: 1.5.6 Current: https://i.stack.imgur.com/gLxFX.png ...

Tips on enlarging the header size in ion-action-sheet within the VueJS framework of Ionic

Recently I started using Vue along with the ionic framework. This is a snippet of code from my application: <ion-action-sheet :is-open="isActionSheetOpen" header="Choose Payment" mode="ios" :buttons="buttons&qu ...

When using MathJax on an iPhone with a device width setting, you will experience the

Utilizing MathJax to display mathematical symbols on a mobile device- such as an iPhone, I encountered an issue with the meta tag: <meta name="viewport" content="user-scalable=no, width=device-width" /> This seemed to be causing problems as the Mat ...

How can I iterate through JSON data and showcase it on an HTML page?

I am in the process of developing a weather application using The Weather API. So far, I have successfully retrieved the necessary data from the JSON and presented it in HTML format. My next goal is to extract hourly weather information from the JSON and ...