Next.js Discord

Discord Forum

How to use a server side variable for all users

Unanswered
Maine Coon posted this in #help-forum
Open in Discord
Maine CoonOP
I have an import process that runs automatically every 10 minutes. The time of the last import should be displayed in the frontend, and this time applies to every user.
In addition, every user should have the option to start the import process manually. This should reset the timer to 10 minutes.
My current problem is that the existing timer is not reset, but a new timer is started each time.

So how can I create a server-side variable and use and update it for all users?

The import process is called via an api route and then the startAutomaticImport() function gets called:

export default class ImportDataUseCase {
    private importer: ImporterService;
    public static timer: NodeJS.Timeout;

    constructor(importer: ImporterService) {
        this.importer = importer;
    }

async executeImport() {
   try {
      await this.importer.importData();
      return { message: "Import successful.", status: 200 }; } 
  catch (error) {
      return { message: "Import failed: " + error.message, status: 500 };
  }
}

async startAutomaticImport() {       clearInterval(this.timer);
  this.timer = setInterval(async () => {
      await this.executeImport();
  }, 600000);
  return await this.executeImport();
}

2 Replies

Maine CoonOP
bump
Maine CoonOP
bump