Author Topic: How to abort a PJSR script?  (Read 6613 times)

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
How to abort a PJSR script?
« on: 2012 March 19 12:41:06 »
I am having problems trying to abort a long script. I have tried to enable console.abortEnable, jsAbortable and calling processEvents as described in:
http://pixinsight.com/forum/index.php?topic=1849.msg11227#msg11227
http://pixinsight.com/forum/index.php?topic=1294.msg6536#msg6536

The "Pause/Abort" button is active and when pressed, it shows the abort dialog. However, the script doesn't stop and keeps running. In the bottom of the console there is the message "Running - Abort requested".

This is a short script that shows this behavior:
Code: [Select]
console.show();
console.abortEnabled = true;
jsAbortable=true;
for(var i=0; i<1000; i++)
{
   for(var j=0; j<10; j++)
      console.writeln(i);
   processEvents();
}

What more have I  to do?

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
Re: How to abort a PJSR script?
« Reply #1 on: 2012 March 19 15:16:14 »
I am having similar trouble getting abort to work.

More: I also see unexpected Pause/Abort button dimming behavior:

With console.abortEnable and jsAbortable both set false, sometime the Pause/Abort button switches from dimmed to active.

With console.abortEnable and jsAbortable both set true, sometimes the Pause/Abort button switches from active to dimmed.

This active/dimmed switching appears to be related to my script's ProcessInstance executeOn/executeGlobal calls.

Thanks,
Mike

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
Re: How to abort a PJSR script?
« Reply #2 on: 2012 March 19 20:32:31 »
Andres,

This works, but has unacceptable issues as a general scripting scheme IMO.

Thanks,
Mike

Code: [Select]
console.show();
console.abortEnabled = true;
jsAbortable=true;
var image = new Image;
image.statusEnabled = true;
image.initializeStatus("", 1000000000);
for(var i=0; i<20; i++)
{
   for(var j=0; j<10; j++)
      console.writeln(i);
   try {
      image.advanceStatus(1);
   }
   catch (e) {
      console.writeln("aborted");
      break;
   }
}
image.completeStatus();

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to abort a PJSR script?
« Reply #3 on: 2012 April 03 09:49:36 »
Hi,

This is now fixed in the new core version 1.7.6.793. I've just finished testing a new feature:

Boolean Console.abortRequested

This new property of the Console core object is true when the user answers 'Yes' to the confirmation dialog after clicking the Pause/Abort button (or pressing Esc). The following variation of Andrés' script allows user interruption:

Code: [Select]
console.show();
console.abortEnabled = true;
for ( var i = 0; i < 1000; ++i )
{
   for ( var j = 0; j < 10; ++j )
      console.writeln( i );
   processEvents();
   if ( console.abortRequested )
      throw "Process aborted";
}

Note that the call to processEvents() is necessary to keep the GUI responsive, so an eventual click on the Pause/Abort button or an Escape keystroke can be processed by the core application.

The new version of the core application is finishing tests right now for immediate release.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
Re: How to abort a PJSR script?
« Reply #4 on: 2012 April 03 20:18:20 »
Thanks Juan,

I am running 1.7.6.793 and testing abortEnabled.

Feedback/questions:

- I set console.abortEnabled true, but when when StarAlignment.executeOn() returns I find abortEnabled has been reset to false. It appears I may need to set it to back to true again after each process execution. Is this expected behavior?

- Is there a way to tell if a process execution itself has aborted? Maybe executeOn() will throw (I am not sure). Or maybe there is a process property (So far I can't find one).

Thanks,
Mike

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to abort a PJSR script?
« Reply #5 on: 2012 April 04 10:47:25 »
Thanks Mike,

Quote
- I set console.abortEnabled true, but when when StarAlignment.executeOn() returns I find abortEnabled has been reset to false. It appears I may need to set it to back to true again after each process execution. Is this expected behavior?

Yes. This happens because the core application disables the console pause/abort function when it runs a process instance. The process has to enable pause/abort explicitly when/if appropriate, and the core will disable it again when the process terminates execution. This is standard behavior, and has been implemented this way because not all processes can be interrupted arbitrarily and/or asynchronously. In PixInsight, a process always starts knowing that the platform is in a 'secure' state where the user cannot interrupt it.

So if you call ProcessInstance.executeGlobal() or ProcessInstance.executeOn() from a script, you have to assign true to console.abortEnabled after the call if you want to let the user pause and/or abort your script afterwards.

Quote
- Is there a way to tell if a process execution itself has aborted? Maybe executeOn() will throw (I am not sure). Or maybe there is a process property (So far I can't find one).

You cannot know if a process has been aborted by the user. However, ProcessInstance.executeOn() returns true if the process has been executed successfully, false otherwise. If the process has been aborted, the return value is false. There cannot be exceptions thrown from a module, so ProcessInstance.executeXXX() cannot throw exceptions.

I have some important improvements to the JavaScript runtime planned for PI 1.8, including more and more precise control on process execution and, among many other things, the possibility to call module-defined methods for process instances, beyond executeOn() and executeGlobal().
Juan Conejero
PixInsight Development Team
http://pixinsight.com/