Batch Processor

Headless Client Plugin Guide / Batch Processor

Overview

The Batch Processor is a headless client that starts automatically when the server starts and runs without any direct user interaction. It is typically used for backend automation and maintenance tasks. Since this client is initiated as part of the server’s lifecycle, it is ideal for tasks that need to execute consistently on startup or at regular intervals.

The Batch Processor can be configured with specific credentials or startup arguments, depending on your needs, and it can either run a one-time task (like server maintenance) or be scheduled to run recurrently using the Scheduler Plugin.

You can find more detailed information about Batch Processors here.

Features

  • No User Interaction*: The Batch Processor runs automatically with no need for a logged-in user interface.

  • Login Credentials: You can configure a user credential for authentication, managed through the Servoy Admin Page.

  • Startup Arguments: Startup arguments can also be set, making it easier to customize the batch session’s behavior during initialization.

  • Scheduling: You can bind the processor to a startup method for execution upon server start, also schedule recurring tasks via the Scheduler Plugin.

Examples

Bind to a startup method

A simple method that runs when the server starts. It can be configured to perform any server-side tasks, like cleaning up logs or running diagnostics In this example, the Batch Processor will clean up old logs and session data in the performMaintenance() function.

/**
 * Callback method for when solution is opened.
 *
 * @param {String} arg startup argument part of the deeplink url with which the Client was started
 * @param {Object<Array<String>|String>} queryParams all query parameters of the deeplink url with which the Client was started, key>string if there was one value else key>Array<String>
 *
 * @properties={typeid:24,uuid:"E4E0B0D5-B12B-40B1-8E4B-CC5670C9C5D2"}
 */
function onSolutionOpen(arg, queryParams) {
    // Example: Initializing system-wide settings
    application.output('Batch Processor: Server has started. Initializing system...');
    
    // Perform any server-side maintenance or setup
    performMaintenance();
}

function performMaintenance() {
    // Maintenance logic
    // You could perform database cleanups, data synchronization, etc.
        application.output('Batch Processor: Starting maintenance tasks...');

    // 1. Clean up old server logs (keeping only the last 3 months)
    var cutoffDate = new Date();
    cutoffDate.setMonth(cutoffDate.getMonth() - 3);  // Logs older than 3 months

    var logQuery = databaseManager.createSelect('db:/example_db/log_table');
    logQuery.where.add(logQuery.columns.log_date.lt(cutoffDate));
    
    // Delete old log entries
    var deletedLogs = databaseManager.getFoundSet(logQuery).deleteAllRecords();
    application.output('Batch Processor: ' + deletedLogs + ' old log records deleted.');

    // 2. Remove outdated session data (keeping only the last 7 days)
    var sessionCutoffDate = new Date();
    sessionCutoffDate.setDate(sessionCutoffDate.getDate() - 7);  // Sessions older than 7 days

    var sessionQuery = databaseManager.createSelect('db:/example_db/session_table');
    sessionQuery.where.add(sessionQuery.columns.session_date.lt(sessionCutoffDate));

    // Delete old session entries
    var deletedSessions = databaseManager.getFoundSet(sessionQuery).deleteAllRecords();
    application.output('Batch Processor: ' + deletedSessions + ' old session records deleted.');

    application.output('Batch Processor: Maintenance tasks completed.');
}

Run once and shutdown

Sometimes, the processor only needs to perform a task once, such as server initialization or cleanup, and then stop.

In this scenario:

  • The Batch Processor runs the runOneTimeTask() method.

  • Once the task completes, the application.exit() method is called, which shuts down the batch processor.

This is ideal for one-time tasks that don’t need to keep running after startup, such as initializing services or processing data at the server boot.

Schedule a recurring task

To run a task at recurring intervals, the Scheduler Plugin can be used. Here’s how to schedule a task that runs every 10 minutes:

Key Points:

  • Scheduler Plugin is used in onSolutionOpen to add a CRON job that runs every 10 minutes.

  • The CRON expression '0 0/10 * * * ?' schedules the task to execute at the start of every 10-minute period.

  • The recurringTask() method contains the logic that will be executed every time the job runs.

This setup is useful for tasks like recurring data synchronization, sending automated emails, or running periodic checks.

Last updated

Was this helpful?