Giter VIP home page Giter VIP logo

Comments (9)

MaxTwentythree avatar MaxTwentythree commented on September 25, 2024 1

Hi @dr-dimitru
Ah, super, that's exactly what I need!
Apparently I did not try the obvious one :D
Those snippets might also be nice for the documentation..
Thanks a lot for your help and the great package!

from josk.

dr-dimitru avatar dr-dimitru commented on September 25, 2024

Hello @MaxTwentythree ,

Updates would work behind the scenes. Just create a new task with same task id "uid", and it would get updated.

Firstly I wonder why it's necessary to append "setTimeout" / "setInterval" to the task uid?

No, package does it for you. The reason — be able to create Interval and Timeout tasks with same uid. To be honest — I don't see in the docs suggesting uid to be in the form of ${taskId}setTimeout

Secondly removing a task and recreating it within the same Meteor method doesn't work.

I'd like to see code snippet where yu remove the task and that doesn't work.

(I think it's what you mean with "Should be called in a separate event loop").

Here's example:

const timer = job.setTimeout(func, 34789, 'unique-taskid');
someAsyncCall(() => {
  job.clearTimeout(timer);
});

Another example:

const timer = job.setTimeout(func, 34789, 'unique-taskid');
process.nextTick(() => {
  job.clearTimeout(timer);
});

Is there a proposed solution to achieving this?

What was your original question? Regarding updating existing task:

// CREATE NEW TASK
const timer = job.setTimeout(func, 34789, 'unique-taskid');

// UPDATE EXISTING TASK
const timer = job.setTimeout(otherFunc, 512, 'unique-taskid'); // <-- same 'unique-taskid'

from josk.

dr-dimitru avatar dr-dimitru commented on September 25, 2024

@MaxTwentythree I'm glad that was solved so quickly 😅
Thank you for documentation suggestion, let's keep it open until docs is updated

from josk.

MaxTwentythree avatar MaxTwentythree commented on September 25, 2024

Hi @dr-dimitru,
I now tried the update functionality but it doesn't seem to work :( .. maybe I did something wrong
Here's my method

  function addTaskMethod(nodeId: string, endDate: Date) {
    check(nodeId, String);
    check(endDate, Date);

    const now = new Date();
    const diff = endDate.getTime() - now.getTime();

    const db = Meteor.users.rawDatabase();
    const job = new JoSk({db});
    const taskId = `task ${nodeId}`;

    const bound = Meteor.bindEnvironment((callback) => {
      callback();
    });

    const task = function finishFeedbackPhaseTask(ready) {
      bound(() => {
        Meteor.call(someMethod, nodeId, () => {
          ready();
        });
      });
    };

    job.setTimeout(task, diff, taskId);
  }

When I call this method twice with a different enddate (but the same nodeId, which means same taskId) the task should update its executeAt prop, right?

from josk.

dr-dimitru avatar dr-dimitru commented on September 25, 2024

@MaxTwentythree

  1. Yes, delay and executeAt properties should get updated
  2. You should initiate JoSk only once, move const job = new JoSk({db}); out of the addTaskMethod function to global scope. Ideally you should initiate it in its own file and export { job }, then import where used

Let me know if it helps

from josk.

MaxTwentythree avatar MaxTwentythree commented on September 25, 2024

Ah, I see, 2) makes a lot of sense ;)
I fixed this and initiate JoSk now only once.
But it didn't fix the update unfortunatelly.
I also tried the josk onError callback, but I don't get errors for the not updated tasks there..
It's not a big deal as I have a clearJob method anyways, which I now run before creating new tasks and it works like this.
Still curious why it doesn't work without it though..

from josk.

dr-dimitru avatar dr-dimitru commented on September 25, 2024

@MaxTwentythree will add it to our test suit, let's see if we can replicate it. Let's keep it open for now, thank you for reporting

from josk.

MaxTwentythree avatar MaxTwentythree commented on September 25, 2024

Thanks a lot @dr-dimitru !

from josk.

dr-dimitru avatar dr-dimitru commented on September 25, 2024

@MaxTwentythree it's been a while, no changes to the codebase were necessary. Please, see override tests. The main difference between this and your examples, — .find() called in the separate event-loop from creating and updating tsk

josk/test/npm.js

Lines 184 to 206 in 464a7ad

const uid = job.setTimeout(noop, 2048, 'timeoutOverride');
setTimeout(async () => {
const task = await job.collection.findOne({ uid });
assert.ok(typeof task === 'object', 'setTimeout override — record exists');
assert.equal(task.delay, 2048, 'setTimeout override — Have correct initial delay');
job.setTimeout(noop, 3072, 'timeoutOverride');
setTimeout(async () => {
const updatedTask = await job.collection.findOne({ uid });
assert.equal(updatedTask.delay, 3072, 'setTimeout override — Have correct updated delay');
process.nextTick(() => {
job.clearTimeout(uid);
setTimeout(async () => {
assert.equal(await job.collection.findOne({ uid }), null, 'setTimeout override — Task cleared');
done();
}, 384);
});
}, 384);
}, 384);

Feel free to reopen it in case if the issue is still persists on your end.

from josk.

Related Issues (6)

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.