cancel
Showing results for 
Search instead for 
Did you mean: 

How to execute a Traffic Script within a script?

SOLVED
richard_pardue
Occasional Contributor

How to execute a Traffic Script within a script?

I understand the importing of a script inside a script as a function and that works very well.

The issue is when the other script is not a function just a sub?

import script1;

.....

# Some code

....

# Call script

script1();

--------------------

Traffic script errors on the script1 call

Have I just over looked something or will the sub have to be changed into a function?

1 ACCEPTED SOLUTION

Accepted Solutions
aclarke
Frequent Contributor

Re: How to execute a Traffic Script within a script?

Try something like this: (updated to reflect the correction by Owen Garrett below)


## TrafficScript


## Name: myResponseHandlers


sub code500(){


# if we get a 500 Service Error, retry


   if( http.getResponseCode() == 500 ){


      $node = connection.getNode();


      log.info( "Runtime Error: 500 on NODE = " .$node. " page for " .http.getPath() ." Trying Different Node" );


      request.avoidNode( connection.getNode() );


      #log.info( "Retry Different Node.... not " .$node. "" );


      request.retry();


   }


}



sub code503(){


# if we get a 503 Service unavailable, retry


   if( http.getResponseCode() == 503 ) {


      if( request.getRetries() < 3 ) {


         $node = connection.getNode();


         request.avoidNode( $node );


         log.info ( "Node " .$node. " Service unavailable more then 3 times, trying a different node... Check UNC PATH LINKS IN SERVER!!!" );


         request.retry();


      }


   }


}




Then in your main script you call them like this:


# Main Response Script


# Name: Response_Main


import myResponseHandlers as handle;



## Then run each of the scripts


handle.code500();


handle.code503();




I haven't tested the code you provided, but the import and sub structure is right...

[Note: see comment below about function names - they cannot begin with a digit - Owen]

[Note: updated this post to reflect Owen's comment on function names - Aidan ]

--
Aidan Clarke
Pulse Secure vADC Product Manager

View solution in original post

4 REPLIES 4
aclarke
Frequent Contributor

Re: How to execute a Traffic Script within a script?

Richard,

If you are importing "script1;" when you call a sub "foo" in it, you call it as script1.foo()

     Take a look at Owen Garrett's great tutorial on handling WebSockets (Managing WebSockets traffic with Stingray Traffic Manager) for some examples of this in action: The zips included in the document have some great examples on how to include other TrafficScripts as libraries and call them...

hth

--

Aidan.

--
Aidan Clarke
Pulse Secure vADC Product Manager
richard_pardue
Occasional Contributor

Re: How to execute a Traffic Script within a script?

yes, like a function script1.foo()

but what if their is not a .foo in the script?

# Main Response Sctipt

# Name: Response_Main

import Response_RuntimeError_500;

import Response_Response_Code_503;

## Then run each of the scripts

Response_RuntimeError_500();

Response_Response_Code_503();

## Note the two call error on the check script, so how can you call script that is not coded as a function?

------------------------------------------------------

## Traffic Script

## Name: Response_RuntimeError_500

if( http.getResponseCode() == 500 ){

   $node = connection.getNode();

   log.info( "Runtime Error: 500 on NODE = " .$node. " page for " .http.getPath() ." Trying Different Node" );

   request.avoidNode( connection.getNode() );

   #log.info( "Retry Different Node.... not " .$node. "" );

   request.retry();

}

------------------------------------------------------

## Traffic Script

## Response_Response_Code_503

# if we get a 503 Service unavailable, retry

if( http.getResponseCode() == 503 ) {

   if( request.getRetries() < 3 ) {

      $node = connection.getNode();

      request.avoidNode( $node );

      log.info ( "Node " .$node. " Service unavailable more then 3 times, trying a different node... Check UNC PATH LINKS IN SERVER!!!" );

      request.retry();

   }

}

aclarke
Frequent Contributor

Re: How to execute a Traffic Script within a script?

Try something like this: (updated to reflect the correction by Owen Garrett below)


## TrafficScript


## Name: myResponseHandlers


sub code500(){


# if we get a 500 Service Error, retry


   if( http.getResponseCode() == 500 ){


      $node = connection.getNode();


      log.info( "Runtime Error: 500 on NODE = " .$node. " page for " .http.getPath() ." Trying Different Node" );


      request.avoidNode( connection.getNode() );


      #log.info( "Retry Different Node.... not " .$node. "" );


      request.retry();


   }


}



sub code503(){


# if we get a 503 Service unavailable, retry


   if( http.getResponseCode() == 503 ) {


      if( request.getRetries() < 3 ) {


         $node = connection.getNode();


         request.avoidNode( $node );


         log.info ( "Node " .$node. " Service unavailable more then 3 times, trying a different node... Check UNC PATH LINKS IN SERVER!!!" );


         request.retry();


      }


   }


}




Then in your main script you call them like this:


# Main Response Script


# Name: Response_Main


import myResponseHandlers as handle;



## Then run each of the scripts


handle.code500();


handle.code503();




I haven't tested the code you provided, but the import and sub structure is right...

[Note: see comment below about function names - they cannot begin with a digit - Owen]

[Note: updated this post to reflect Owen's comment on function names - Aidan ]

--
Aidan Clarke
Pulse Secure vADC Product Manager
owen
Frequent Contributor

Re: How to execute a Traffic Script within a script?

Aidan's import and substructure is correct.  One catch - function names cannot start with numbers!

If you name your functions 'code500()', 'code503()' etc in your library, then you can invoke them in your rule as 'myResponseHandlers.code500()' etc.

You can also import and use a different name space if you wish:


# Main Response Script 


# Name: Response_Main


import myResponseHandlers as handle;



## Then run each of the scripts


handle.code500();


handle.code503();



In general, TrafficScript libraries (rules that you 'import' into other rules) can only contain functions.  Don't think of an 'import' as if it were a #include or other sort of macro include operation.