Dashboards & Visualizations

Javascript and blocking search

New Member


According to dev.splunk.com/view/javascript-sdk/SP-CAAAEFA#blockingjob, setting exec_mode to "blocking" will block the javascript execution until the job returns.

// Search everything and return the first 100 results
var searchQuery = "search * | head 100";

// Set the search parameters
var searchParams = {
  exec_mode: "blocking",
  earliest_time: "2012-06-20T16:27:43.000-07:00"

// A blocking search returns the job's SID when the search is done
console.log("Wait for the search to finish...");

// Run a blocking search and get back a job
  function(err, job) {

    // Get the job from the server to display more info
      // Display properties of the job
      console.log("Search job properties\n---------------------");
      console.log("Search job ID:         " + job.sid);
      console.log("The number of events:  " + job.properties().eventCount); 
      console.log("The number of results: " + job.properties().resultCount);
      console.log("Search duration:       " + job.properties().runDuration + " seconds");
      console.log("This job expires in:   " + job.properties().ttl + " seconds");

      // Get the results and display them
      job.results({}, function(err, results) {
        var fields = results.fields;
        var rows = results.rows;
        for(var i = 0; i < rows.length; i++) {
          var values = rows[i];
          console.log("Row " + i + ": ");
          for(var j = 0; j < values.length; j++) {
            var field = fields[j];
            var value = values[j];
            console.log("  " + field + ": " + value);



I tried the code sample adding console.log("TEST") after .

The "TEST" message shows up between the:

console.log("Wait for the search to finish...");



What can i do to really block until the results are available ?

Tags (2)
0 Karma


Pretty sure "blocking" in that article doesn't mean "blocking your js code". Js is typically used asynchronously (rightfully so, in my opinion), and this is the case with the blocking search as well - the events are different though, compared to a splunk search run in "normal" execution mode. Just to clarify, the search is blocking, not your code.
If you use the same code as above and change the "blocking" exec_mode (to "normal", you'll notice errors about undefined objects. That's because the code that is defined inside the third argument to service.search, the callback function, is called immediately upon creation of the search manager, and it tries to access elements of the search manager that aren't there yet. That's the difference in case of a blocking search - you'll only get called back once the results are there, hence the "blocking".

What exactly did you want to do that has to wait until the search is done? You should never just "block" your code for no reason. If you want to execute your code after the result are in, just call it inside the callback function (where the "...done!" console log is) using a blocking search.

0 Karma


@arns is this for the purpose of learning SplunkJS or is your usecase to perform some activities when the search completes?

If it is second one what is the activity to be performed upon search completion? Does it have to be in JavaScript?

| makeresults | eval message= "Happy Splunking!!!"
0 Karma

New Member


i just want to block the js script until results.rows got the search results.

Isn't "exec_mode: "blocking" supposed to do that ?

0 Karma
.conf21 Now Fully Virtual!
Register for FREE Today!

We've made .conf21 totally virtual and totally FREE! Our completely online experience will run from 10/19 through 10/20 with some additional events, too!