How to call external function in background tasks in arangodb

I have the functionality fully working and I want to call it every 30 minutes from a background task. But it doesn't raise or throw an error as "undefined".

app.js

function hourly() { require("console"); console.log('I am running');}

controller.get('/testOnce', function(req, res) {
    var tasks = require("org/arangodb/tasks");

    tasks.register({
        id : "Test",
        name : "Testing background task",
        period : 5,
        command : "hourly()"
    });
});

      

I tried to define hourly in a separate js and then call it with "require". But these throws cannot find the "myjob" module

myjob.js

hourly () function {require ("console"); console.log ("I'm running");

app.js

controller.get('/testOnce', function(req, res) {
var tasks = require("org/arangodb/tasks");

tasks.register({
    id : "Test",
    name : "Testing background task",
    period : 5,
    command : "var job = require('myjob');"
});

      

});

+3


source to share


2 answers


Attribute content command

cannot refer to variables defined in other scopes. For example, in the app.js variant, you are using a named variable hourly

that can no longer be present when the command is executed.

In a simple case, just write something, the app.js variant can be executed if its parameter is command

changed to the following (which does not require any variables):

var tasks = require("org/arangodb/tasks");
tasks.register({
    period : 5,
    command : "require('console').log('I am running from inline command');"
});

      

The variant defining the task function in a separate file (with a name myjob.js

) can be executed using the function available through the export of this module:

function hourly() { 
  require("console").log('I am running from myjob.js');
}

exports.hourly = hourly;

      



This is because it require()

will only show what the module exported. In the above case, the module will expose a named function hourly

that can now be called from a background task like this:

var tasks = require("org/arangodb/tasks");
tasks.register({
    period : 5,
    command : "require('myjob').hourly();"
});

      

Note that for this to work, the file myjob.js

must be located in the module's search path. IIRC, by default js/node

. Also note that this directory already includes the modules included in the kit and is subject to change on ArangoDB updates.

If a regular command is to be executed from the Foxx route, then using Foxx queues may also be an option, as they should allow a script with a job function to be placed inside the application directory. However, I have not tried it yet.

+5


source


The "Foxx solution" will use a queued job and a script (introduced in 2.6) to resolve this issue.

I covered this in the latest Foxx webinar and am working on a blog post and cookbook recipe.



The problem with this method is that Foxx jobs cannot be periodic in 2.6. The feature is slated for 2.7, but with 2.6 just released, you probably won't be able to use it anytime soon.

Personally, I would recommend using an external scheduler and call the Foxx script from there (via the foxx manager CLI or HTTP API).

+2


source







All Articles