Skip to content
Dominick Peluso edited this page Nov 1, 2016 · 9 revisions

Overview

Jobs are objects which are produced from a given nest which trigger behavior. In most cases, a job is a file -- but that is not always the case.

Usage

Within a tunnel runner, job objects are provided.

tunnel.run(function(job, nest, done){
    console.log(job.name); // "myFile.pdf"
});

Basic methods

Depending on what nest your job came from, you may get a different type of job. Each type of job has a different set of methods, although many are common.

if(job.extension === "pdf"){
    // Do stuff to PDFs
} else {
    // Do stuff to all others
}

These are the current types:

Setting property values

Even though jobs are JS objects and properties can be assigned to them anywhere, there is a special getter and setter for saving job data.

job.setPropertyValue("My Job Number", 123456);
console.log("Got ", job.getPropertyValue("My Job Number")); // Got 123456

Property values can be any type, even deep objects. The advantage of using property values is if you pack a job, the property values will be restored upon unpacking.

Transferring to other tunnels

Jobs can be easily transferred to other tunnels with the transfer method.

tunnel1.run(function(job, nest){
    job.transfer(tunnel2);
});
tunnel2.run(function(job){
    console.log("Got " + job.name + " in tunnel " + tunnel2.name);
});

Moving to other nests

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, function(){
        // Moved
    });
});

Packing

Packing allows you to store jobs with their properties on the filesystem, removing them from antfarm's memory. Packing produces a PackedJob, which can be unpacked in another tunnel at a later time.

/* -------- Write pack ----------- */
var tunnel = af.createTunnel("Write properties and pack");

tunnel.watch(in_folder);

tunnel.run(function(job, nest){

    console.log("Packing...", job.getName());

    job.setPropertyValue("My Job Number", 123456);

    // Pack method, provides a PackedJob in a callback
    job.pack(function(packJob){
        packJob.move(packed_folder);
    });

});

/* -------- Read pack ----------- */
var tunnel2 = af.createTunnel("Unpack and read properties");
tunnel2.watch(packed_folder);

tunnel2.run(function(packedJob, nest){

    console.log("Unpacking...", packedJob.name);

    // Unpack method, provides the original (unpacked) job in the callback
    packedJob.unpack(function(unpackedJob){
        console.log("Unpacked job number", unpackedJob.getPropertyValue("My Job Number"));
        // Unpacked job number 123456
        unpackedJob.move(unpacked_folder);
        packedJob.remove();
    });
    
});