Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task Scheduling In App #1210

Open
jonathanroze opened this issue Apr 9, 2023 · 2 comments
Open

Task Scheduling In App #1210

jonathanroze opened this issue Apr 9, 2023 · 2 comments

Comments

@jonathanroze
Copy link

Hi,

I wanted to know if it was planned to provide a recurring task management system directly in the app instance instead of being executed via the CLI?

In order to be able to trigger different elements in the code directly (Typically, sending a message to websockets clients?).

This would also allow to set up a "multi servers" system for the crons so that the load is distributed.

Thanks and thank for your work on FoalTS, my opinion is that it is probably the best framework ts (not chauvin at all..)

@LoicPoullain
Copy link
Member

Hi 👋

This would indeed be a nice feature to have. I cannot spend much time on this at the moment but if someone is willing to implement this, I could help 🙂

I guess there are a few questions that should be answered first:

  • What would be the interface of the service? Do we have examples on other projects / frameworks to get inspired?
  • Do we want to use another library to handle scalability? Or can it be implemented easily natively?

@lcnvdl
Copy link
Contributor

lcnvdl commented Apr 25, 2024

Maybe this can be useful:

I use Schedule inside my FoalTS Apps.

I just make the jobs like is explained here: https://foalts.org/docs/2.x/cookbook/scheduling-jobs

And then I call the "main" function in the app.controller

// (...)
import { main as schedule } from '../scripts/schedule-jobs';
import { DataSource } from 'typeorm';
import { createDataSource } from '../db';

export class AppController implements IAppController {
  subControllers = [
// ...
  ];

  async init() {
      const ds = createDataSource();
      const connection: DataSource = await ds.initialize();
      await schedule({ connection, runAtStart: true });
  }
}

Schedule jobs.ts

// https://foalts.org/docs/cookbook/scheduling-jobs

import { scheduleJob } from 'node-schedule';
import { main as closeElderSessions } from './close-elder-sessions';
import { main as deleteOrphans } from './delete-empty-finished-sessions';
import { main as deleteOlderFinishedSessions } from './delete-older-finished-sessions';
import { DataSource } from 'typeorm';

export async function main(args?: { runAtStart?: boolean; useExistingConnection?: boolean; unit?: string; value?: number; connection?: DataSource }) {
  console.log('Scheduling the job...');

  if (args?.runAtStart) {
    await closeElderSessions(Object.assign({ unit: 'day', value: 3 }, args || {}));
    await deleteOlderFinishedSessions(args || {});
    await deleteOrphans(args || {});
  }

  //  At 3.00 am

  scheduleJob('0 3 * * *',
    async () => {
      try {
        await closeElderSessions(Object.assign({ unit: 'day', value: 3 }, args || {}));
        await deleteOlderFinishedSessions(args || {});
        await deleteOrphans(args || {});
      }
      catch (err) {
        console.error('Error running schedule');
        console.error(err);
      }
    }
  );

  console.log('Job scheduled!');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants