mohan2monks has asked for the wisdom of the Perl Monks concerning the following question:
Hi I am mostly used to writing CGI style scipts and trying to move a existing CGI script to a PSGI app and deploy it.
The website is running on APACHE and written in PHP.
As this will be the only perl script i can't deploy on mod_perl.
As APACHE already loads php5 module loading perl also in every apache child only for a script will be a waste.
I have been reading about PSGI Plack and converted my script to a PSGI complaint app and tested it.
#mycgiapp.psgi #!/usr/bin/perl use strict; use Plack::Request; use forks::BerkeleyDB; use XML::Simple; use JSON qw(encode_json); use MIME::Base64 qw(encode_base64); use DBI; use SOAP::Lite(); use POSIX qw(ceil); use Data::Dumper; $XML::Simple::PREFERRED_PARSER='XML::Parser'; $SOAP::Constants::DO_NOT_USE_CHARSET = 1; my $app = sub { my $env = shift; my $req = Plack::Request->new($env); my $input=$req->parameters(); my($out)=GetOutput($input); if ($input->{'format'} eq 'json'){ my $json=JSON->new->utf8->encode($out); return [ 200, ['Content-Type' => 'application/json'], [ $json ], ]; } else{ my $xml=XMLout($out,RootName=>"Result",keepRoot=>0,NoAttr=>1, Ke +yAttr => []); return [ 200, ['Content-Type' => 'text/xml'], [ $xml ], ]; } }; sub GetOutput { #CGI script code # Runs multiple threads/forks to collect data from various apis with S +OAP calls return ($headers,$out); } #RAN IT WITH plackup C:\httpd\myapp>plackup -D -r --host 192.168.110.181 --port 5010 --app +myapp.psgi Watching ./lib myapp.psgi for file updates. HTTP::Server::PSGI: Accepting connections at http://192.168.110.181:50 +10/ 10.1.1.4 - - [11/Sep/2014:11:33:57 +0530] "GET /
I have noticed that the command prompt never returns and if i close it service stops.
How can start this from command line and keep it running after i close putty or command prompt.
I can run this service as standalone service as it is used by other scripts and not directly by end users.
What would be best approach i can follow to deploy on production.
It should be available as apache is for other php pages.
Can i run it from apache itself.
If as a standalone server what should i use from plack like starman etc
Please help.
Update
I have set up ubic on my system to start and manage starman service as
While i get ubic status as not running the top command shows starman running and accepting connections.use Ubic::Service::Starman; my $Config={ server_args => { 'listen' =>'10.1.1.5:8080', 'workers' =>16, 'backlog' =>1024, 'max-requests'=>1000, 'daemonize' =>'', 'user' =>'perl', 'access-log' =>'/var/log/psgi/access.log', 'error-log' =>'/var/log/psgi/error.log', 'app' =>'/var/www/myapp/myapp.psgi', }, app => "/var/www/myapp/myapp.psgi", status => sub {}, ubic_log=> '/var/log/psgi/ubic.log', stdout => '/var/log/psgi/stdout.log', stderr => '/var/log/psgi/stderr.log', user =>'perl', group =>'perl', cwd =>'/var/www/myapp', pidfile =>'/var/log/psgi/myapp.pid', }; return Ubic::Service::Starman->new($Config); # ubic status myapp # ubic myapp not running Can't locate /var/www/myapp/Config.pl in @INC (@INC contains: /usr/lo +cal/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl / +usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at / +var/www/myapp/myapp.psgi line 166.
UPDATE
I think this is more to do with ubic as it status is unkown or not running it never changed to working directory that was provided in config options
earlier using use Ubic::Service::Starman;
i was getting ubic status as not runing.. and @INC
But now i changed it to below and ubic status is unknown.
starman service is running though ubic status is unknownuse Ubic::Service::Plack; my $Config={ server => "Plack::Handler::Starman", server_args => { 'listen' =>'10.1.1.5:8080', 'workers' =>16, 'backlog' =>1024, 'max-requests'=>1000, 'daemonize' =>'', 'reload' =>'', 'user' =>'perl', 'access-log' =>'/var/log/psgi/access.log', 'error-log' =>'/var/log/psgi/error.log', 'app' =>'/var/www/myapp/myapp.psgi', }, app => '/var/www/myapp/myapp.psgi', status => sub {}, ubic_log=> '/var/log/psgi/ubic.log', stdout => '/var/log/psgi/stdout.log', stderr => '/var/log/psgi/stderr.log', user =>'perl', group =>'perl', cwd =>'/var/www/myapp/', pidfile =>'/var/log/psgi/domfares.pid', }; return Ubic::Service::Plack->new($Config);
Why ubic status is unknown?Starman error log 2014/09/12-14:25:52 Worker processes cleaned up 2014/09/12-14:25:52 Server closing! Process Backgrounded 2014/09/12-14:26:53 Starman::Server (type Net::Server::PreFork) starti +ng! pid(20742) Binding to TCP port 8080 on host 10.1.1.5 with IPv4 2014/09/12-14:26:53 Can't connect to TCP port 8080 on 10.1.1.5 [Addres +s already in use] at line 68 in file /usr/local/share/perl5/Net/Server/Proto/TCP.pm 2014/09/12-14:26:53 Received QUIT. Running a graceful shutdown 2014/09/12-14:26:53 Worker processes cleaned up 2014/09/12-14:26:53 Server closing! ##Ubic log [Fri Sep 12 14:24:53 2014] 19878 guardian name: ubic-guardian st +arman --backlog 1024 --access-log /var/log/psgi/access.log --max-requ +ests 1000 --app /var/www/myapp/myapp.psgi --error-log /var/log/psgi/e +rror.log --workers 16 --reload --listen 10.1.1.5:8080 --user perl -- +server Plack::Handler::Starman --daemonize /var/www/myapp/myapp.psgi [Fri Sep 12 14:24:53 2014] 19878 obtaining lock... [Fri Sep 12 14:24:53 2014] 19878 got lock [Fri Sep 12 14:24:53 2014] 19878 guardian pid: 19878 [Fri Sep 12 14:24:53 2014] 19878 daemon pid: 19879 [Fri Sep 12 14:24:53 2014] 19878 child guid: 2916133834 [Fri Sep 12 14:25:52 2014] 19878 sending SIGTERM to 19879 [Fri Sep 12 14:25:52 2014] 19878 daemon 19879 exited [Fri Sep 12 14:25:52 2014] 20296 guardian name: ubic-guardian st +arman --backlog 1024 --access-log /var/log/psgi/access.log --max-requ +ests 1000 --app /var/www/myapp/myapp.psgi --error-log /var/log/psgi/e +rror.log --workers 16 --reload --listen 10.1.1.5:8080 --user perl -- +server Plack::Handler::Starman --daemonize /var/www/myapp/myapp.psgi [Fri Sep 12 14:25:52 2014] 20296 obtaining lock... [Fri Sep 12 14:25:52 2014] 20296 got lock [Fri Sep 12 14:25:52 2014] 20296 guardian pid: 20296 [Fri Sep 12 14:25:52 2014] 20296 daemon pid: 20297 [Fri Sep 12 14:25:52 2014] 20296 child guid: 2916139767 [Fri Sep 12 14:26:52 2014] 20296 sending SIGTERM to 20297 [Fri Sep 12 14:26:52 2014] 20296 daemon 20297 exited [Fri Sep 12 14:26:52 2014] 20738 guardian name: ubic-guardian st +arman --backlog 1024 --access-log /var/log/psgi/access.log --max-requ +ests 1000 --app /var/www/myapp/myapp.psgi --error-log /var/log/psgi/e +rror.log --workers 16 --reload --listen 10.1.1.5:8080 --user perl -- +server Plack::Handler::Starman --daemonize /var/www/myapp/myapp.psgi [Fri Sep 12 14:26:52 2014] 20738 obtaining lock... [Fri Sep 12 14:26:52 2014] 20738 got lock [Fri Sep 12 14:26:52 2014] 20738 guardian pid: 20738 [Fri Sep 12 14:26:52 2014] 20738 daemon pid: 20739 [Fri Sep 12 14:26:52 2014] 20738 child guid: 2916145800 [Fri Sep 12 14:26:55 2014] 20738 sending SIGTERM to 20739 [Fri Sep 12 14:26:55 2014] 20738 daemon 20739 exited
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Change cgi script to PSGI app
by tobyink (Canon) on Sep 11, 2014 at 09:00 UTC | |
by mohan2monks (Beadle) on Sep 11, 2014 at 09:13 UTC | |
by locked_user sundialsvc4 (Abbot) on Sep 12, 2014 at 01:12 UTC | |
by tobyink (Canon) on Sep 12, 2014 at 09:00 UTC | |
|
Re: Change cgi script to PSGI app
by Corion (Patriarch) on Sep 12, 2014 at 07:27 UTC | |
by mohan2monks (Beadle) on Sep 12, 2014 at 07:49 UTC | |
by Corion (Patriarch) on Sep 12, 2014 at 08:01 UTC | |
by mohan2monks (Beadle) on Sep 12, 2014 at 09:21 UTC | |
|
Re: Change cgi script to PSGI app
by Anonymous Monk on Sep 11, 2014 at 07:37 UTC | |
by Anonymous Monk on Sep 11, 2014 at 07:38 UTC |