Antfarm is a simple automation framework that aims to make file automation robust and scalable.
🐜🐜🐜🐜🐜
$ npm install antfarm
var Antfarm = require('antfarm');
var options = {
log_dir: "/Users/dominickpeluso/Desktop",
log_max_size: null,
log_max_files: null,
log_file_level: null,
log_out_level: null
};
var af = new Antfarm(options);
Tunnels are defined workflows which modularize and run your program code.
// Build a Tunnel
var tunnel = af.createTunnel("Hotfolder sorting workflow");
You can attach nests to tunnels, which triggers the nest's watching behavior.
// Attach the Nest to our Tunnel to watch for new files
tunnel.watch(hotfolder);
Tunnels handle your application logic via the run method. You can have multiple run instances within a single tunnel, allowing you to run several actions concurrently.
// When a new file is found, execute the following
tunnel.run(function(job, nest){
// your application code
});
If you need to run logic synchronously, you can do so with runSync:
ftp_tunnel.runSync(function(job, nest, done){
console.log("Do this first");
done();
});
ftp_tunnel.runSync(function(job, nest, done){
console.log("Do this second");
done();
});
`
Similar to the run method, the fail method will allow you to handle job errors.
// When a job fails, execute the following
tunnel.fail(function(job, nest){
console.log("Job " + job.getName() + " failed!");
});
Nests are a simple abstraction for storage locations (filesystem folders, S3 buckets, FTP, etc...).
var desktop_nest = af.createFolderNest("/Users/dominickpeluso/Desktop");
var ftp_nest = af.createFtpNest("ftp.ants.com", 21, 'username', 'password', 5);
You can easily move jobs from one nest to another without having to worry about its type.
tunnel.watch(desktop_nest);
tunnel.run(function(job, nest){
// Uploads files from desktop to the FTP
job.move(ftp_nest);
});
Jobs are provided within tunnel events and have lots of helpful methods.
if(job.getExtension() == "pdf"){
// Do stuff to PDFs
}
This workflow watches an FTP folder for new files every 5 minutes. Then, when found, simply transfers them to the desktop.
var Antfarm = require('../lib/antfarm'),
af = new Antfarm({
log_dir: "/Users/dominickpeluso/Logs"
});
var my_ftp = af.createFtpNest("ftp.ants.com", 21, 'username', 'password', 5);
var out_folder = af.createFolderNest("/Users/dominickpeluso/Desktop");
var ftp_tunnel = af.createTunnel("FTP test");
ftp_tunnel.watch(out_folder);
ftp_tunnel.run(function(job, nest){
job.move(my_ftp);
});
ftp_tunnel.fail(function(job, nest){
console.log("do fail");
});
This workflow routes PDFs which arrive in the hotfolder to a particular folder while routing all other filetypes to another folder.
// Import Antfarm and set some options
var Antfarm = require('../lib/antfarm'),
af = new Antfarm({
log_dir: "/Users/dominickpeluso/Logs"
});
// Set some paths for convenience
const INPUT_PATH = '/Users/dominickpeluso/Desktop/Antfarm Example/Hotfolder In';
const OUTPUT_PDF_PATH = '/Users/dominickpeluso/Desktop/Antfarm Example/Out/PDF';
const OUTPUT_OTHER_PATH = '/Users/dominickpeluso/Desktop/Antfarm Example/Out/Others';
// Build a Tunnel
var tunnel = af.createTunnel("Hotfolder sorting workflow");
// Create a Nest for our hot folder
var hotfolder = af.createFolderNest(INPUT_PATH);
// Attach the Nest to our Tunnel to watch for new files
tunnel.watch(hotfolder);
// When a new file is found, execute the following
tunnel.run(function(job, nest){
// Move PDFs to one folder, all others to another
if(job.getExtension() == "pdf"){
job.move(af.createFolderNest(OUTPUT_PDF_PATH));
} else {
job.move(af.createFolderNest(OUTPUT_OTHER_PATH));
}
});
// When a job fails, execute the following
tunnel.fail(function(job, nest){
console.log("Job " + job.getName() + " failed!");
});
Generated using TypeDoc