Cron Job

Prev Next

We recommend using a cron job to send the logfile requests to the Mapp tracking server every few minutes and empty your file to keep it at a manageable size. You have two possibilities:

Custom Cron Job File

This option requires you to write your own cron job file and use our MappIntelligenceCronjob class.

Integration

Maven

<dependency>
    <groupId>com.mapp.intelligence.tracking</groupId>
    <artifactId>cronjob</artifactId>
    <version>1.1.1</version>
</dependency>

Gradle

implementation group: 'com.mapp.intelligence.tracking', name: 'cronjob', version: '1.1.1'

Methods

constructor

For a complete configuration of the Mapp Intelligence Cronjob please check Configuration.

The cron job always sends via HTTP_CLIENT with batches of up to 1000 requests. It uses an unlimited queue (maxQueueSize 0), so no request of a log file is discarded because of the queue size, and it sends without requestInterval: the result of every file is decided by the flush at its end.

The track ID and the track domain are required, otherwise a MappIntelligenceException is thrown. Without a file path, the directory of the system property java.io.tmpdir is used; without a file prefix, MappIntelligenceRequests. requestQueues, connectionTimeout, readTimeout and logger have no config file key: they can only be set with new MappIntelligenceCronjob(config), not with the predefined cron job script and -c (there, --debug sets the logger).

/**
 * @param config Mapp Intelligence config
 * @throws MappIntelligenceException Mapp Intelligence configuration exception
 */
MappIntelligenceCronjob cronjob = new MappIntelligenceCronjob(MappIntelligenceConfig config);

Alternatively, you can pass the command-line arguments of the predefined cron job script:

/**
 * @param args Command line arguments
 * @throws MappIntelligenceException Mapp Intelligence configuration exception
 */
MappIntelligenceCronjob cronjob = new MappIntelligenceCronjob(String[] args);

run

Send the logfile requests to the Mapp tracking server and delete your logfiles to keep it at a mangeable size. Returns the following results:

  • 0: All data was sent successfully, there were no log files to send, or another run is still sending the files of the same file path and prefix (this run sends nothing).

  • 1: An error has occurred, for example requests could not be delivered or had to be discarded, or the file path does not exist or is not writable.

The cron job processes all files <filePrefix>-<timestamp>….log in the file path, in the order of their names. Only files named after the prefix, a - and then the digits of the timestamp belong to a prefix, so the prefix Shop takes neither the files of ShopDE nor those of Shop-DE. Temporary files (.tmp) that are older than the configured maxFileDuration (default 30 minutes) are renamed to .log first, so that they are sent as well. The outcome per file:

Outcome

File

Run

All requests were delivered

The file is deleted. If it cannot be deleted, it is renamed to *.log.sent and not sent again.

Continues with the next file.

Requests could not be delivered, but may still succeed (for example network or server errors after all attempts)

The file is kept as .log with only the requests that were not delivered and is sent by the next run.

Stops, exit status 1. The remaining files are kept for the next run, in order.

At least one batch can never be sent (permanent client error, oversized batch)

Its requests (only these, not the delivered ones) are appended to *.log.discarded: kept for inspection, but not picked up again. If nothing is left to retry, the log file is deleted; otherwise it is kept as .log with only the requests left to retry.

Nothing left to retry: continues with the next file, exit status 1. Otherwise: stops, exit status 1.

The file cannot be read

The file is kept and the error is logged.

Stops, exit status 1.

A log file that was sent partially is rewritten with only the requests that were not delivered, so the next run does not send the delivered ones again. The cron job writes them into a helper file <file>.log.<random>.part first, which then replaces the log file. A .part file is only left behind if the cron job is killed at that moment; it is not processed and can be deleted.

Only one run at a time sends the files of a file path and prefix: a run holds an exclusive lock on <filePath>/<filePrefix>.cronjob.lock. A run that starts while another one is still sending ends right away with exit status 0, without sending anything. The operating system releases the lock when the process ends, also if it is killed. The lock file stays in the directory; do not delete it while a run is active. The cron job therefore needs write access to the file path.

/**
 * @return Status
 */
cronjob.run();
Example
package demo.cronjob;

import com.mapp.intelligence.tracking.MappIntelligenceCronjob;
import com.mapp.intelligence.tracking.config.MappIntelligenceConfig;

public class MappIntelligenceCronjobCommandLine {
	public static void main(String[] args) {
        int status = 1;
        try {
			MappIntelligenceConfig mappIntelligenceConfig = new MappIntelligenceConfig();
			mappIntelligenceConfig.setTrackId("111111111111111");
			mappIntelligenceConfig.setTrackDomain("analytics01.wt-eu02.net");
			mappIntelligenceConfig.setFilePath("/path/to/your/log/file/");
			mappIntelligenceConfig.setFilePrefix("MappIntelligenceData");
			
            MappIntelligenceCronjob c = new MappIntelligenceCronjob(mappIntelligenceConfig);
            status = c.run();
        } catch (Exception e) {
            // do nothing
        }

        System.exit(status);
    }
}

Predefined Cron Job Script

This option allows you to use our predefined cron job script, an executable jar file that contains all dependencies. It is published as the Maven artifact com.mapp.intelligence.tracking:cronjob (see Integration above), for example as cronjob-1.1.1.jar on Maven Central. The examples below assume that you saved it as mapp-intelligence-java-cronjob.jar.

Short notation

Long notation

Description

-i

--trackId

Enter your Mapp Intelligence track ID provided by Mapp.

-d

--trackDomain

Enter your Mapp Intelligence tracking URL.

-f

--filePath

Enter the path to your request logging file.

-p

--filePrefix

Enter the prefix for your request logging files.


-c

--config

Enter the path to your configuration file (*.properties or *.xml). The other options override the values of the file.



--debug

Activates the debug mode. The debug mode sends messages to the command line.

-ll

--logLevel

Shows all messages of this level and of higher levels of importance: DEBUG, INFO, WARN, ERROR, FATAL, NONE (case-insensitive, default: ERROR). An invalid value is ignored.


--help

Display the help (this text) and exit with status 0. Nothing is sent.


--version

Display version and exit with status 0. Nothing is sent.

The exit status of the script is the result of run (see above). It also exits with status 1 if the configuration is invalid (for example a missing track ID or track domain) or if an unknown option is passed.

Note

--debug only defines where the messages go: they are written to stderr. Without --debug, the script prints no messages at all, whatever the log level. Which messages are shown is controlled by --logLevel (or logLevel in the configuration file), which defaults to ERROR. Use --debug --logLevel DEBUG to see all messages.

Example
$ java -jar ./mapp-intelligence-java-cronjob.jar -i 111111111111111 -d analytics01.wt-eu02.net -f /tmp/ -p MappIntelligenceData
 
$ java -jar ./mapp-intelligence-java-cronjob.jar -i 111111111111111 -d analytics01.wt-eu02.net -f /tmp/ -p MappIntelligenceData --debug
 
$ java -jar ./mapp-intelligence-java-cronjob.jar -i 111111111111111 -d analytics01.wt-eu02.net -f /tmp/ -p MappIntelligenceData --debug --logLevel DEBUG
 
$ java -jar ./mapp-intelligence-java-cronjob.jar -c /path/to/your/configuration/file/config.properties
 
$ java -jar ./mapp-intelligence-java-cronjob.jar -c /path/to/your/configuration/file/config.properties --debug

Define a Cron Job

To create your crontab file, type the following command at the shell prompt crontab -e and add the following lines:

# sends every five minutes the tracking data to the Mapp tracking serve
*/5 * * * * java -jar ./mapp-intelligence-java-cronjob.jar -c /path/to/your/configuration/file/config.properties