Skip to content

Commit 8aa1630

Browse files
committed
no SIGTERM handling
1 parent b1682ce commit 8aa1630

File tree

9 files changed

+42
-42
lines changed

9 files changed

+42
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ in the documentation of this project are to be interpreted as described in [RFC
8282

8383
## Features
8484

85-
* [PEB can be started from any folder without installation procedure.](./doc/CONSTANTS.md)
85+
* [PEB can be started from any folder without installation procedure.](./doc/CONSTANTS.md#files-and-folders)
8686
* [Perl script output is seamlessly inserted in a nice HTML user interface.](./doc/SETTINGS.md#perl-scripts-api)
8787
* [Event-driven interactive Perl scripts can be repeatedly fed with data.](./doc/SETTINGS.md#interactive-perl-scripts)
8888
* [Any version of Perl 5 can be used.](./doc/REQUIREMENTS.md#runtime-requirements)

doc/SETTINGS.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,10 @@ A PEB interactive Perl script should have the following features:
157157
* **No buffering**
158158
PEB interactive scripts should have ``$|=1;`` among their first lines to disable the built-in buffering of the Perl interpreter, which prevents any output before the script has ended.
159159

160-
* **SIGTERM handling**
161-
PEB sends the ``SIGTERM`` signal to all interactive scripts on exit for a graceful shutdown and to prevent them from becoming zombie processes. All interactive scripts must exit in 3 seconds after the ``SIGTERM`` signal is given by PEB. All unresponsive scripts are killed before PEB exits. The ``SIGTERM`` signal may be handled by any interactive script for a graceful shutdown using the following code:
162-
163-
```perl
164-
$SIG{TERM} = sub {
165-
# your shutdown code goes here...
166-
exit();
167-
};
168-
```
169-
170160
* **Failsafe print**
171-
If a PEB instance crashes, it can still leave its interactive scripts as zombie processes consuming large amounts of memory. To prevent this scenario, all interactive scripts should test for a successful ``print`` on the STDOUT. This could be implemented using the following code:
161+
Failsafe print is necessary for a graceful shutdown of Perl scripts on normal PEB exit and when PEB unexpectedly crashes. PEB closes the STDOUT and STDERR channels of all running Perl scripts when the close button is pressed - they must exit in 3 seconds or any unresponsive scripts are killed.
162+
163+
Failsafe print could be implemented using the following code:
172164

173165
```perl
174166
print "output string" or shutdown_procedure();
@@ -198,6 +190,7 @@ The following code shows how to start a PEB interactive Perl script right after
198190
interactive_script.inputData = function() {
199191
return document.getElementById('interactive-script-input').value;
200192
}
193+
201194
interactive_script.stdoutFunction = function (stdout) {
202195
var container = document.getElementById('interactive-script-output');
203196
container.innerText = stdout;
@@ -216,7 +209,7 @@ The following code shows how to start a PEB interactive Perl script right after
216209
</html>
217210
```
218211

219-
The [index.htm](https://github.com/ddmitov/perl-executing-browser/blob/master/resources/app/index.html) page of the demo package demonstrates how to start one script in two instances immediately after local page is loaded.
212+
The [index.htm of the demo package](https://github.com/ddmitov/perl-executing-browser/blob/master/resources/app/index.html) shows how to start one Perl script in two instances right after the PEB index page is loaded.
220213

221214
The [interactive.pl](https://github.com/ddmitov/perl-executing-browser/blob/master/resources/app/perl/interactive.pl) script of the demo package is an example of a Perl interactive script for PEB.
222215

resources/app/perl-scripts/interactive.pl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,3 @@ sub get_input {
7979
sub shutdown_procedure {
8080
exit(0);
8181
}
82-
83-
# Exit on SIGTERM signal from PEB:
84-
$SIG{TERM} = sub {
85-
exit(0);
86-
};

src/webengine-page.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class QPage : public QWebEnginePage
4242

4343
signals:
4444
void pageLoadedSignal();
45+
void hideWindowSignal();
4546
void closeWindowSignal();
4647

4748
public slots:
@@ -372,14 +373,10 @@ public slots:
372373
QScriptHandler *handler = iterator.value();
373374

374375
if (handler->scriptProcess.isOpen()) {
375-
#ifndef Q_OS_WIN
376-
handler->scriptProcess.terminate();
377-
#endif
378-
379-
#ifdef Q_OS_WIN
380-
handler->scriptProcess.kill();
381-
runningScripts.remove(iterator.key());
382-
#endif
376+
handler->scriptProcess
377+
.closeReadChannel(QProcess::StandardOutput);
378+
handler->scriptProcess
379+
.closeReadChannel(QProcess::StandardError);
383380
}
384381

385382
if (!handler->scriptProcess.isOpen()) {
@@ -388,22 +385,21 @@ public slots:
388385
}
389386
}
390387

391-
#ifndef Q_OS_WIN
392388
if (!runningScripts.isEmpty()) {
393389
int maximumTimeMilliseconds = 3000;
394390
QTimer::singleShot(maximumTimeMilliseconds,
395391
this, SLOT(qScriptsTimeoutSlot()));
396392
}
397-
#endif
398393

399394
if (runningScripts.isEmpty()) {
400395
emit closeWindowSignal();
396+
} else {
397+
emit hideWindowSignal();
401398
}
402399
}
403400

404401
void qScriptsTimeoutSlot()
405402
{
406-
#ifndef Q_OS_WIN
407403
if (!runningScripts.isEmpty()) {
408404
QHash<QString, QScriptHandler*>::iterator iterator;
409405
for (iterator = runningScripts.begin();
@@ -418,7 +414,6 @@ public slots:
418414
}
419415

420416
emit closeWindowSignal();
421-
#endif
422417
}
423418

424419
protected:

src/webengine-view.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ QViewWidget::QViewWidget()
4141
QObject::connect(this, SIGNAL(startWindowClosingSignal()),
4242
mainPage, SLOT(qStartWindowClosingSlot()));
4343

44+
QObject::connect(mainPage, SIGNAL(hideWindowSignal()),
45+
this, SLOT(qHideWindowSlot()));
46+
4447
QObject::connect(mainPage, SIGNAL(closeWindowSignal()),
4548
this, SLOT(qCloseWindowSlot()));
4649

src/webengine-view.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public slots:
138138
QViewWidget::triggerPageAction(QWebEnginePage::SelectAll);
139139
}
140140

141+
// ==============================
142+
// Hide window:
143+
// ==============================
144+
void qHideWindowSlot()
145+
{
146+
this->parentWidget()->hide();
147+
}
148+
141149
// ==============================
142150
// Close window:
143151
// ==============================

src/webkit-page.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class QPage : public QWebPage
4545

4646
signals:
4747
void pageLoadedSignal();
48+
void hideWindowSignal();
4849
void closeWindowSignal();
4950

5051
public slots:
@@ -385,14 +386,10 @@ public slots:
385386
QScriptHandler *handler = iterator.value();
386387

387388
if (handler->scriptProcess.isOpen()) {
388-
#ifndef Q_OS_WIN
389-
handler->scriptProcess.terminate();
390-
#endif
391-
392-
#ifdef Q_OS_WIN
393-
handler->scriptProcess.kill();
394-
runningScripts.remove(iterator.key());
395-
#endif
389+
handler->scriptProcess
390+
.closeReadChannel(QProcess::StandardOutput);
391+
handler->scriptProcess
392+
.closeReadChannel(QProcess::StandardError);
396393
}
397394

398395
if (!handler->scriptProcess.isOpen()) {
@@ -401,22 +398,21 @@ public slots:
401398
}
402399
}
403400

404-
#ifndef Q_OS_WIN
405401
if (!runningScripts.isEmpty()) {
406402
int maximumTimeMilliseconds = 3000;
407403
QTimer::singleShot(maximumTimeMilliseconds,
408404
this, SLOT(qScriptsTimeoutSlot()));
409405
}
410-
#endif
411406

412407
if (runningScripts.isEmpty()) {
413408
emit closeWindowSignal();
409+
} else {
410+
emit hideWindowSignal();
414411
}
415412
}
416413

417414
void qScriptsTimeoutSlot()
418415
{
419-
#ifndef Q_OS_WIN
420416
if (!runningScripts.isEmpty()) {
421417
QHash<QString, QScriptHandler*>::iterator iterator;
422418
for (iterator = runningScripts.begin();
@@ -431,7 +427,6 @@ public slots:
431427
}
432428

433429
emit closeWindowSignal();
434-
#endif
435430
}
436431

437432
protected:

src/webkit-view.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ QViewWidget::QViewWidget()
4949
QObject::connect(this, SIGNAL(startWindowClosingSignal()),
5050
mainPage, SLOT(qStartWindowClosingSlot()));
5151

52+
QObject::connect(mainPage, SIGNAL(hideWindowSignal()),
53+
this, SLOT(qHideWindowSlot()));
54+
5255
QObject::connect(mainPage, SIGNAL(closeWindowSignal()),
5356
this, SLOT(qCloseWindowSlot()));
5457

src/webkit-view.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public slots:
143143
inspector->showMaximized();
144144
}
145145

146+
// ==============================
147+
// Hide window:
148+
// ==============================
149+
void qHideWindowSlot()
150+
{
151+
this->parentWidget()->hide();
152+
}
153+
146154
// ==============================
147155
// Close window:
148156
// ==============================

0 commit comments

Comments
 (0)