Giter Club home page Giter Club logo

Comments (7)

darkterra avatar darkterra commented on June 28, 2024 1

You can if you wish add a different id to each event to force the module to have several events with the same 'name' and the same 'after'.

However, if you think that you will generate a lot of events, I do not think that it is a good use of the module. The goal is to trigger an event in the future (even if the server reboot, you do not lose events because they are stored in database).

I advise you to consider the name of an event schedule as a category.
Ex:

{ name: 'creditCardCheck', collection: 'users', query: {}, cron: '0 0 23 * * *' }

So in your listener you will do all the desired processing (for example a search in database given all the elements to be processed (here, check the validity of all credit cards)).

Otherwise you can also delegate to the module the task of returning to you the good documents stored in the database (look how to use 'collection' and 'query' when you record an event)
On the picture below is the last two lines of the table:

image

Finally, a little trick, if during the recording of an event, you give a 'collection' and an 'id', the module will automatically return the event when 'after' will be gone + the corresponding document of the collection at the id given when recording the event.


If the above explanation does not solve your problem, do not hesitate to explain in detail your need, that I can find you the best way to use the module to do what you want ;)

from mongo-scheduler.

darkterra avatar darkterra commented on June 28, 2024

Hey @AlexanderProd !

Can you drop some peace of code here to reproduce this bug ?

Don't forget to tell me the version used of mongo-scheduler, but also the used version of node.js

from mongo-scheduler.

AlexanderProd avatar AlexanderProd commented on June 28, 2024

Hi @darkterra
I'm not sure if this is actually a bug or just 'normal' mongodb behavior with the current implementation of adding new events, this is just an assumption though and needs further research.

This has been happening in every version I've been using since before 1.1.5 it just hasn't been a major deal breaker for me. Still isn't but I think it would be good to fix.

This is a part of the code how I'm scheduling new events currently. I'm not really doing anything out of the ordinary.

form.on('end', () => {
    const event = {
      name: 'post',
      after: new Date(Number(data.uploadDate)),
      data: data,
    };

    scheduler.schedule(event);
    res.sendStatus(200);
  });

form refers to form data handled with formidable in an express server.

I'm using node v11.10.0 on macOS in development and Ubuntu 18.04 in production.

from mongo-scheduler.

darkterra avatar darkterra commented on June 28, 2024

Ok, can you show me the value of the object 'event' when you try to save a new scheduled event ?
I would like the value of both attempts.

But between the title of this issue and the code you show me, I already have my idea.

If you try to register a new event with the same name and release date as an event already registered in the database, then the mongo-scheduler-more replaces the old event with the new one.

Look at the code I use to do a unit test:

describe('bulk', () => {
  const bulkSchedule = [
    {
      name: 'event-to-bulk',
      after: moment().add(15, 'm').toDate()
    },
    {
      name: 'event-to-bulk',
      after: moment().add(25, 'm').toDate()
    },
    {
      name: 'event-to-bulk',
      after: moment().add(8, 'm').toDate()
    },
    {
      name: 'event-to-bulk',
      after: moment().add(66, 'm').toDate()
    },
    {
      name: 'event-to-bulk',
      after: moment().add(5000, 'm').toDate()
    },
    {
      name: 'event-to-bulk', //  <------------------------  This event should override the event just above (same name and same after)
      data: 'this is hacked scheduler !!!',
      after: moment().add(5000, 'm').toDate()
    }
  ];
  
  it('should schedule all events at once request to mongo', done => {
    const expectation = (olderr, oldResult) => {
      if (olderr) {
        console.error('olderr: ', olderr);
      }
      
      events.find().toArray((err, docs) => {
        if (err) {
          console.error(err);
        }
        
        expect(docs.length).to.be.equal(5);
        expect(oldResult.result.nUpserted).to.be.equal(5);
        expect(oldResult.result.nMatched).to.be.equal(1);
        expect(oldResult.result.nModified).to.be.equal(1);
        done();
      });
    };
    
    scheduler.scheduleBulk(bulkSchedule, expectation);
  });
});

I'm trying to record 6 events that all have the same name. Of the 6 alone 2 have the same 'after' and the same 'name'. So I have to have 5 event registered in database.

This behavior is quite normal and sought by this module, look at the documentation (last two lines):

image

EDIT:
This behavior is my first modification when I cloned the original module (more than a year ago), it is this line of code that allows to have several same event stored with the same 'name' and different 'after', before my edit, every attempt to record an event with the same 'name' but different 'after' was overwritten.

9b30402#diff-edd552e5b38c38c77f6df11eb7b22ebeR37

from mongo-scheduler.

AlexanderProd avatar AlexanderProd commented on June 28, 2024

If you try to register a new event with the same name and release date as an event already registered in the database, then the mongo-scheduler-more replaces the old event with the new one.

Got it, since all my events have the same name and when the time is the same it overrides it.

I guess it might be worth to think about enabling multiple events with the same name and time since the id would still be enough to differentiate them, what do you think?

from mongo-scheduler.

darkterra avatar darkterra commented on June 28, 2024

Hello @AlexanderProd ,

Does my answer allow you to do what you want to do?
If so, you can close this issue.
In the opposite case do not hesitate to keep me informed, that I can bring you the necessary information.

from mongo-scheduler.

AlexanderProd avatar AlexanderProd commented on June 28, 2024

Hi @darkterra

Yeah I've got it working by adding Date.now()as id to each each new Event this enables me to get multiple events with the same after and name field.

It's not the most elegant solution but it gets the job done.

from mongo-scheduler.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.