Dashboards & Visualizations

Javascript and blocking search

arns
New Member

Hello,

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
service.search(
  searchQuery,
  searchParams,
  function(err, job) {
    console.log("...done!\n");

    // Get the job from the server to display more info
    job.fetch(function(err){
      // 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...");

and

console.log("...done!\n");

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

Tags (2)
0 Karma

jeffland
SplunkTrust
SplunkTrust

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

niketn
Legend

@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

arns
New Member

@niketnilay,

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
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...