1 # Copyright 2008 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: child-process.tag,v 1.3 2009-01-08 12:05:16 markj Exp $
10 UserTag child-process addAttr
11 UserTag child-process HasEndTag
12 UserTag child-process NoReparse 0
13 UserTag child-process Interpolate 0
14 UserTag child-process Version $Revision: 1.3 $
15 UserTag child-process Documentation <<EOD
19 child_process - Execute ITL code in a forked child process
23 [child-process] ... ITL ... [/child-process]
27 Runs Interchange markup code in a forked child process.
28 Useful for off-loading processes that take a relatively long time to complete.
30 Has no effect if the body is empty or contains only whitespace.
38 File name relative to catalog directory to file where output from forked
39 process should be stored.
43 Optional descriptive label for this process that will be put in the operating
44 system process list. Default is "child-process tag".
48 File name relative to catalog directory where a file of zero length will
49 be created if the file in option 'filename' is created successfully.
51 This empty file could be used for notification purposes, e.g. as an
52 indicator that the child process has delivered its output. When placed
53 in web docroot space one could poll for the existence of this file and
54 when it exists bounce to a page that will display the results.
60 This is the parent process.
62 Child process starts here.
63 [child-process filename="tmp/report_[time]%Y%m%d%H%M%S[/time].txt"]
67 ... some long-running SQL query ...
72 Child process ends here.
74 Some more parent stuff....
78 Ton Verhagen <tverhagen@alamerce.nl>
80 Jon Jensen <jon@endpoint.com>
86 UserTag child-process Routine <<EOR
91 my ($opt, $body) = @_;
94 return unless defined($body) and $body =~ /\S/;
96 defined(my $kid = fork) or die "Cannot fork: $!\n";
103 Vend::Server::sever_database();
105 defined (my $grandkid = fork) or die "Kid cannot fork: $!\n";
108 Vend::Server::cleanup_for_exec();
110 # Disconnect from parent's terminal
111 POSIX::setsid() or die "Can't start a new session: $!\n";
113 defined $opt->{label} or $opt->{label} = 'child-process tag';
114 Vend::Server::set_process_name($opt->{label});
116 my $output = interpolate_html($body, 1);
118 my $filename = $opt->{filename};
119 if (defined($filename) and length($filename)) {
120 $filename = $Tag->filter('filesafe', $filename);
121 my $status = $Tag->write_relative_file($filename, $$output);
123 my $notifyname = $opt->{notifyname};
124 if ($status and defined($notifyname) and length($notifyname)) {
125 $notifyname = $Tag->filter('filesafe', $notifyname);
126 $Tag->write_relative_file($notifyname, $opt, '');