Matterway
    Preparing search index...

    Function runJobs

    • A Job is a function that returns a promise. This helper can be used to run multiple jobs concurrently, returning a promise that resolves as an array of all the results.

      Type Parameters

      • T extends JobList

      Parameters

      • ctx: Context

        The context to run the jobs in.

      • jobs: T

        An array of jobs to run.

      • Optionaloptions: RunJobsOptions

        Options for running the jobs.

      Returns JobResults<T>

      An array of results, in the same order as the jobs array.

      async function step(ctx: Context) {
      const jobs = [
      {
      title: 'job 1',
      handler: async () => {
      await showProgress(ctx, 'Running jobs');
      await wait(2000);
      return Promise.resolve(1);
      },
      },
      {
      title: 'job 2',
      handler: async () => {
      await showProgress(ctx, 'Running jobs');
      await wait(3000);
      return Promise.resolve(2);
      },
      },
      {
      title: 'job 3',
      handler: async () => {
      await showProgress(ctx, 'Running jobs');
      await wait(1000);
      return Promise.resolve(3);
      },
      },
      ];
      // notice that the concurrency is set to the number of jobs
      const options = {concurrency: jobs.length, onUpdate: console.log};
      const results = await runJobs(ctx, jobs, options);
      return results;
      }
      const ctx = {};
      step(ctx).then(results => console.log(results));

      we use wait to simulate the time it takes to run a job.

       async function step(ctx: Context) {
      const jobsList = [
      { title: 'Job 1', status: ProgressibleStatus.PENDING },
      { title: 'Job 2', status: ProgressibleStatus.PENDING },
      { title: 'Job 3', status: ProgressibleStatus.PENDING },
      ];
      await showProgressList(ctx, jobsList);
      await wait(1000);
      const jobs = [
      {
      title: 'job 1',
      handler: async () => {
      await wait(2000);
      jobsList[0].status = ProgressibleStatus.RUNNING;
      await showProgressList(ctx, jobsList);
      return Promise.resolve(1);
      },
      },
      {
      title: 'job 2',
      handler: async () => {
      jobsList[0].status = ProgressibleStatus.DONE;
      await wait(2000);
      jobsList[1].status = ProgressibleStatus.RUNNING;
      await showProgressList(ctx, jobsList);
      return Promise.resolve(2);
      },
      },
      {
      title: 'job 3',
      handler: async () => {
      jobsList[1].status = ProgressibleStatus.DONE;
      await wait(2000);
      jobsList[2].status = ProgressibleStatus.RUNNING;
      await showProgressList(ctx, jobsList);
      await wait(1000);
      return Promise.resolve(3);
      },
      },
      ];
      // With concurrency set to 1, the jobs will run sequentially
      const options = { concurrency: 1, onUpdate: console.log };
      const results = await runJobs(ctx, jobs, options);

      for (const job of jobsList) {
      job.status = ProgressibleStatus.DONE;
      }
      await showProgressList(ctx, jobsList);
      await wait(2000);
      return results;
      }
      const ctx = {};
      step(ctx).then((results) => console.log(results));
      • To run all jobs without rejecting the promise if one of them fails, use try/catch inside the job handler.