JianghuJS-Scheduled Tasks

12002

1. Concept

  • What is a Scheduled Task

A scheduled task refers to a task that is set to execute at a predetermined time, performing specific operations or programs at specific times or intervals. These tasks can be triggered periodically, regularly, or at specific points in time. In software development, scheduled tasks are commonly used for handling repetitive operations, automating tasks, and scheduling jobs.

  • Background of Scheduled Task Requirements

Although the HTTP Server developed through the framework follows a request-response model, there are still many scenarios that require the execution of scheduled tasks, such as:

    1. Periodically reporting application status.
    1. Regularly updating local cache from remote interfaces.
    1. Periodically performing file splitting and deleting temporary files.

The framework provides a mechanism to make the writing and maintenance of scheduled tasks more elegant.

2. Definition of Scheduled Tasks in JianghuJS

  • Directory Location

All scheduled tasks are uniformly stored in the app/schedule directory, where each file represents an independent scheduled task that can configure the properties of the scheduled task and the methods to be executed.

  • Definition Method

As a simple example, to define a scheduled task that updates remote data to the memory cache, you can create a file named update_cache.js in the app/schedule directory.

module.exports = {  
  schedule: {  
    interval: '1m', // 1 minute interval  
    type: 'all', // Specify that all workers need to execute  
  },  
  async task(ctx) {  
    const res = await ctx.curl('http://www.api.com/cache', {  
      dataType: 'json',  
    });  
    ctx.app.cache = res.data;  
  },  
};  

This scheduled task will execute once every minute on each Worker process, fetching remote data and mounting it to app.cache.

3. Writing Scheduled Tasks

todo: Example of a scheduled task that updates remote data to memory cache

During the startup process of the Jianghu application, here is a detailed list of the application's startup lifecycle methods (refer to the project root directory app.js):

  1. configWillLoad: This method is called just before the configuration file is loaded, allowing you to load some custom configuration files in this method.
  2. configDidLoad: This method is called after the configuration file has been loaded, allowing you to perform some operations on the loaded configuration files in this method.
  3. didLoad: This method is called when the application starts, allowing you to perform some initialization operations in this method.
  4. willReady: This method is called when the application starts, allowing you to load some plugins or other dependencies in this method.
  5. didReady: This method is called when the application starts, allowing you to perform some operations after initialization is complete in this method.
  6. serverDidReady: This method is called when the application starts, allowing you to perform some operations after the server has started in this method.
  7. beforeClose: This method is called when the application is closing, allowing you to perform some cleanup operations in this method.