Oromis
Oromis

Reputation: 11

Check if function is running with Google Apps Script and Google Web Apps

I've written a simple Google Apps Script that connects to IFTTT on the smartphone and sends SMS. Now I'm trying to develop a user-friendly interface by deploying the script as a Google Web App: the idea is that you should click a button, then it should read "Sending SMS" and keep you waiting until done.

I think this needs the button code to check whether the script is running or not (thus it's terminated), but I can't get it to work.
Here's my setup.

In code.gs I have the main function that sends SMS:

var isRunning;
function execute() {
 isRunning = true;
 if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
 OTHER STUFF;
 isRunning = false;
 if (checkRunning() == true) {Logger.log('isRunning: TRUE')} else {Logger.log('isRunning: FALSE')}
}

and the function that checks whether the variable isRunning is true or false:

function checkRunning() {
  if (isRunning == true) {
    return true}
    else {return false}
}

In my index.html file there's a button to execute, and a button to check:

<input class="button" type="button" value="Send SMS" onclick="google.script.run.execute()"/>
<input class="btncheck" type="button" value="Check running"/>

with this code between <script></script> (using Jquery):

var c;
setInterval(function() {google.script.run.withSuccessHandler(varSet).checkRunning()},1000);
function varSet(value) {c = value}

$('.btncheck').on('click', function() {
   alert();
 }); 

function alert() {
   alert(c)
}

I know for sure that the execute() function runs for about 15 seconds. Therefore if I click the "execute" button, in the following seconds I should be able to click the "check running" button and output true. But it always outputs false because the checkRunning() function returns false.
In the script Log though, the isRunning variable is correctly set to true at start and false at the end of execute().
Any idea?

Upvotes: 1

Views: 2812

Answers (1)

Anton Dementiev
Anton Dementiev

Reputation: 5716

My guess is that each client-to-server call always creates new execution context. In other words, it's not possible to persist variables between function calls, unless you are saving them to a 'stable' object, such as a spreadsheet or a file. Because google.script.run calls run asynchronously, they get different instances of the global object from .gs file, so the "checkRunning()" function is locked inside its own execution context and has no way to get access to or communicate with parallel threads.

You can work around this limitation by using CacheService to persist data and get rid of global variables altogether. I got this to work by making the following changes

function setIsRunning(value){

  CacheService.getScriptCache().put("isRunning", value.toString());

}

function execute() {

 setIsRunning(true);

  Utilities.sleep(15000); // simulate execution for 15 secs

 setIsRunning(false);

}


function checkRunning() {

var currentState = CacheService.getScriptCache().get("isRunning");

 return (currentState == 'true');
}

Note that I'm returning (currentState == 'true') to convert String to Boolean. Boolean('false') will produce true;

Upvotes: 2

Related Questions