Coder Social home page Coder Social logo

Updating jobs about stevejobs HOT 14 CLOSED

msavin avatar msavin commented on May 27, 2024
Updating jobs

from stevejobs.

Comments (14)

msavin avatar msavin commented on May 27, 2024 1

You could do something like:

if (!JobsInternal.Utilities.collection.findOne({name: name})) {
     Jobs.run(JOB_ID, null, RUN_CONFIG);
} 

FYI - as @gary-menzel mentioned - there is a v3 on the way which will feature this.replicate so that each instance of the job has its own document. More info in CHANGES.md

https://github.com/msavin/SteveJobs...meteor.schedule.background.tasks.jobs.queue/tree/v3

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024 1

That seems reasonable. Is V3 close to stable?

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

Oh I think another confounding factor is that this is a repeating job, so I do not end with this.success(), I end with this.reschedule(...).

export function registerJob({
  name,
  run,
}: {
  name: string;
  run: (args: RunArguments) => Promise<void>;
}) {
  console.info(`Registering ${name} job.`);

  Jobs.register({
    [name](args: RunArguments) {
      /* Forward context/params to 'run' method - execute in a fiber-aware await to
       allow regular async syntax in provided function */
      MeteorPromise.await(run.apply(this, args));
    },
  });
}

const RUN_CONFIG: RunConfig = {
  in: {
    minutes: 5,
  },
};

export const JOB_ID = 'job1';

async function runFunction(_args?: RunArguments) {
  const self: JobInstance = this;

  // ... do a bunch of stuff

  // Run the job again, so it keeps looping.
  self.reschedule(RUN_CONFIG);
}

registerJob({ name: JOB_ID, run: runFunction });
console.info(`Running ${JOB_ID} job.`);
Jobs.run(JOB_ID, null, RUN_CONFIG);

And the result is many entries in the job_data collection, all with the pending state.

from stevejobs.

gary-menzel avatar gary-menzel commented on May 27, 2024

#29 may also be related - I believe there is a V3 being planned that will readdress how rescheduling works.

from stevejobs.

msavin avatar msavin commented on May 27, 2024

This is working as it should. The problem is, the server is executing Jobs.run(JOB_ID, null, RUN_CONFIG) every time it starts up. You only need to run it once to "jumpstart" it.

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

Oh that's interesting. Is there a way to check if that "jumpstart" has already been done? Allows me to ensure that the job is started as the code slides through different environments (and eventually to production).

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

And what's the easiest way to install V3?

from stevejobs.

msavin avatar msavin commented on May 27, 2024

I just pushed it - so run meteor update and you'll have it. Let me know how it goes. :)

from stevejobs.

mazak avatar mazak commented on May 27, 2024

Perfect!

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

Awesome thats great! Thanks for the speedy response and the support.

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

If you are curious, this is what I came up with to wrap it into a cron job.

My business logic lives in an async function the returns SUCCESS or RETRY and the wrappers in ^ do the work of rescheduling or replicating depending on the result. Also cancels the pending version on creation so the job can be updated with new code changes safely.

And I just call runRepeatingJob({ name: "jobName", job: myCoolFunction, args, repeatMinutes: 5 }) and the wrapper handles the rest. myCoolFunction does not need to know anything about the implementation.

from stevejobs.

msavin avatar msavin commented on May 27, 2024

@jmoseley thanks for sharing and glad you got it working. Unfortunately, I'm not up on the latest es6 practicers, but once I am I will be sure to take a look :)

from stevejobs.

jmoseley avatar jmoseley commented on May 27, 2024

Ah ya it's typescript, so might be hard to grok up front, but hopefully it helps someone else get started.

from stevejobs.

gary-menzel avatar gary-menzel commented on May 27, 2024

@jmoseley Although I don't use TS, I found it useful. I have been using Promise a fair bit but somehow missed that there is a Meteor version available as a package. It looks like it makes working withMeteor.bindEnvironment much more seamless. Thanks for the link.

from stevejobs.

Related Issues (20)

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.