User Tools

Site Tools


sib:listeners:filelistener

FileListener

Overview

The FileListener SIB Listener periodically checks for files in a configured path that matches a given RegEx filter. When files matching the filter are found the file will be read and posted on the configured MQTT-topic. The default payload is BASE64 encoded but that can be overridden in the inputFormat method.

If the delete or moveto .custom configurations are set the handled files are either moved to another path or deleted.

Details

Modulesib_filelistener
Version1.3.3

Workflow

  • CRONSchedule: [.custom.schedule]
    • on trigger:
      • Check for files (.custom.path)
        • on file found:
          • Check .custom.filter for filename match
            • on match:
              • Publish: .custom.topic

Custom configuration

filelistener/custom.js
var FileListenerCustom = {
	_id: "fileSample:1",
	_name: "SIB FileListener Sample",
	delete: true,
	filter: "\.bat$|\.csv$",
	init: ( thisListener )=>{},
	inputFormat: (topic, msg, cb)=>{
		console.log( "Running inputFormat" );
		msg.contents = new Buffer(msg.contents, "base64").toString( "UTF-8" );
		cb( msg );
	},
	loggerCfg: {
		dest: {
			file: false,
			console: true,
			mqtt: true
		}
	},
	moveto: "/tmp/filelistener1/archive",
        renameOnMove: ( filename )=>{
            return filename;
        },
	path: "/tmp/filelistener1",
	schedule: "*/10 * * * * *",
	mqtt: "mqtt://localhost",
	topic: "SIB/Samples/FileListener",
        useFileLock: false,
        lockOptions: {
            stale: 10000,
            realpath: true,
            lockfilePath: "./lockfile"
        }
};
 
module.exports = FileListenerCustom;
  • _id: The unique ID of this service
  • _name: A plaintext description of this service
  • delete: true/false, if true the file is deleted after pickup
  • filter: RegEx filter to test the filename for, if match the file is picked up
  • init(thisListener): If defined this method is executed when the service is starting up as the last step of initialization. thisListener is a reference to the instance of the listener that does the method call.
  • inputFormat(topic, msg, callback): The method that is run on service activation. The original message is passed as the argument and the return value from this method must be a JSON formatted message conforming to the SIB message standard.
  • loggerCfg: A JSON Object defining what logger actions should be active for this service (see Logger Configuration
  • moveto: If set, moves the file to this path after pickup
  • renameOnMove: Method that is run when files are moved. Returns the name the file should have in the new location.
  • path: The file path to check for files
  • publishInput: If defined overrides the standard publishing to the internal MQTT transfer
  • schedule: The cron-schedule when to run the check
  • mqtt: The MQTT connection string for the internal MQTT transport
  • topic: The MQTT topic to subscribe to on the internal MQTT transport
  • useFileLock: Set to true if the listener should use the file locking functionality
  • lockOptions: Options for the lock functionality.
    • stale: Duration in milliseconds in which the lock is considered stale, defaults to 10000 (minimum value is 5000)
    • realpath: Resolve symlinks using realpath, defaults to true (note that if true, the file must exist previously)
    • lockfilePath: Custom lockfile path. e.g.: If you want to lock a directory and create the lock file inside it, you can pass file as <dir path> and options.lockfilePath as <dir path>/dir.lock
sib/listeners/filelistener.txt · Last modified: 2019/09/20 07:29 by hubbe