/
child-process.tag
131 lines (91 loc) · 3.34 KB
/
child-process.tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2008, 2020 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
UserTag child-process addAttr
UserTag child-process HasEndTag
UserTag child-process NoReparse 0
UserTag child-process Interpolate 0
UserTag child-process Version 1.4
UserTag child-process Documentation <<EOD
=head1 NAME
child_process - Execute ITL code in a forked child process
=head1 SYNOPSIS
[child-process] ... ITL ... [/child-process]
=head1 DESCRIPTION
Runs Interchange markup code in a forked child process.
Useful for off-loading processes that take a relatively long time to complete.
Has no effect if the body is empty or contains only whitespace.
Options are:
=over 4
=item filename
File name relative to catalog directory to file where output from forked
process should be stored.
=item label
Optional descriptive label for this process that will be put in the operating
system process list. Default is "child-process tag".
=item notifyname
File name relative to catalog directory where a file of zero length will
be created if the file in option 'filename' is created successfully.
This empty file could be used for notification purposes, e.g. as an
indicator that the child process has delivered its output. When placed
in web docroot space one could poll for the existence of this file and
when it exists bounce to a page that will display the results.
=back
=head1 EXAMPLES
This is the parent process.
Child process starts here.
[child-process filename="tmp/report_[time]%Y%m%d%H%M%S[/time].txt"]
[query
list=1
sql="
... some long-running SQL query ...
"
][sql-line]
[/query]
[/child-process]
Child process ends here.
Some more parent stuff....
=head1 AUTHORS
Ton Verhagen <tverhagen@alamerce.nl>
Jon Jensen <jon@endpointdev.com>
=cut
EOD
UserTag child-process Routine <<EOR
use POSIX ();
sub {
my ($opt, $body) = @_;
use vars qw/ $Tag /;
return unless defined($body) and $body =~ /\S/;
defined(my $kid = fork) or die "Cannot fork: $!\n";
if ($kid) {
waitpid($kid, 0);
return;
}
else {
Vend::Server::sever_database();
defined (my $grandkid = fork) or die "Kid cannot fork: $!\n";
exit if $grandkid;
Vend::Server::cleanup_for_exec();
Vend::Session::close_session();
# Disconnect from parent's terminal
POSIX::setsid() == -1 and die "Can't start a new session: $!\n";
defined $opt->{label} or $opt->{label} = 'child-process tag';
Vend::Server::set_process_name($opt->{label});
my $output = interpolate_html($body, 1);
my $filename = $opt->{filename};
if (defined($filename) and length($filename)) {
$filename = $Tag->filter('filesafe', $filename);
my $status = $Tag->write_relative_file($filename, $$output);
my $notifyname = $opt->{notifyname};
if ($status and defined($notifyname) and length($notifyname)) {
$notifyname = $Tag->filter('filesafe', $notifyname);
$Tag->write_relative_file($notifyname, $opt, '');
}
}
exit;
}
}
EOR