Serving koha-common with Plack

This post builds on my last Koha post about running the OPAC on Plack and will modify some of the files from that post. If you haven’t read it, it is a good background post but the snippets are no longer up-to-date.

Except where noted, I put these files in a new /usr/share/koha/misc/plack folder. This guide and these code samples assume you will too.

Hey! Do you want the speed of Plack but don’t want to deal with all of these files? Check out Plackify koha-common on github! This post will eventually go out of date, but the github project will always have the latest scripts and a painless installer.

Let’s begin!

1. Create the PSGI application file

koha.psgi:

This is the file that will do the heavy lifting for your Koha install. It is used to run both Intranet and OPAC front-end sites.

#!/usr/bin/perl
use Plack::Builder;
use Plack::App::CGIBin;
use Plack::App::Directory;
 
use lib("/usr/share/koha/lib");
use C4::Context;
use C4::Languages;
use C4::Members;
use C4::Dates;
use C4::Boolean;
use C4::Letters;
use C4::Koha;
use C4::XSLT;
use C4::Branch;
use C4::Category;
 
my $root = ( $ENV{INTRANETDIR} ) ?
                $ENV{INTRANETDIR} . '/cgi-bin' :
                $ENV{OPACDIR} . '/cgi-bin/opac' ;
 
my $app=Plack::App::CGIBin->new(root => $root);
 
builder {
 
        mount "/opac-tmpl" => Plack::App::File->new(root => "/usr/share/koha/opac/htdocs/opac-tmpl");
        mount "/intranet-tmpl" => Plack::App::File->new(root => "/usr/share/koha/intranet/htdocs/intranet-tmpl");
 
        mount "/cgi-bin/koha" => $app;
 
};

2. Create startup scripts for both the intranet and OPAC

These scripts will handle starting your Plack OPAC and Intranet (respectively) in a way that can be controlled by `koha-common` style start and stop commands.

Note: These scripts assume that you installed Starman from CPAN. If you used your distro’s package manager, you should check your starman path and update these scripts if needed.

opac-plack.sh:

Make sure this script is executable!

#!/bin/sh
# --max-requests = decreased from 1000 to 50 to keep memory usage sane
# --workers = number of cores on machine
 
test ! -z "$1" && site=$1 && shift
dir=`dirname $0`
 
export KOHA_CONF=/etc/koha/sites/$site/koha-conf.xml
export OPACDIR="$( xmlstarlet sel -t -v 'yazgfs/config/opacdir' $KOHA_CONF | sed 's,/cgi-bin/opac,,' )"
export LOGDIR="$( xmlstarlet sel -t -v 'yazgfs/config/logdir' $KOHA_CONF )"
 
export MEMCACHED_SERVERS=localhost:11211
export MEMCACHED_NAMESPACE=$site
 
PIDFILE=/var/run/koha/$site/opac-plack.pid
 
SOCKET=/var/run/koha/$site/opac-plack.sock
#PORT=5000
 
# uncomment to enable logging
opt="$opt --access-log $LOGDIR/opac-access.log --error-log $LOGDIR/opac-error.log"
opt="$opt -M FindBin --max-requests 50 --workers 2 -E deployment"
 
if [ $SOCKET ]; then
    opt="$opt --listen $SOCKET -D --pid $PIDFILE"
elif [ $PORT ]; then
    opt="$opt --port $PORT -D --pid $PIDFILE"
fi
 
/usr/local/bin/starman $opt $dir/koha.psgi

intranet-plack.sh:

Make sure this script is executable!

#!/bin/sh
# --max-requests = decreased from 1000 to 50 to keep memory usage sane
# --workers = number of cores on machine
 
test ! -z "$1" && site=$1 && shift
dir=`dirname $0`
 
export KOHA_CONF=/etc/koha/sites/$site/koha-conf.xml
export INTRANETDIR="$( xmlstarlet sel -t -v 'yazgfs/config/intranetdir' $KOHA_CONF | sed 's,/cgi-bin,,' )"
export LOGDIR="$( xmlstarlet sel -t -v 'yazgfs/config/logdir' $KOHA_CONF )"
 
export MEMCACHED_SERVERS=localhost:11211
export MEMCACHED_NAMESPACE=$site
 
PIDFILE=/var/run/koha/$site/intranet-plack.pid
 
SOCKET=/var/run/koha/$site/intranet-plack.sock
#PORT=5000
 
# uncomment to enable logging
opt="$opt --access-log $LOGDIR/intranet-access.log --error-log $LOGDIR/intranet-error.log"
opt="$opt -M FindBin --max-requests 50 --workers 2 -E deployment"
 
if [ $SOCKET ]; then
    opt="$opt --listen $SOCKET -D --pid $PIDFILE"
elif [ $PORT ]; then
    opt="$opt --port $PORT -D --pid $PIDFILE"
fi
 
/usr/local/bin/starman $opt $dir/koha.psgi

2a. Scripts for opening access to socket files

Note: If you’re using sockets instead of TCP ports on Linux, you’ll need to make sure they are world-writable. The following two scripts can be run by koha-start-plack to make sure the sockets are available to any application.

unlock-opac-plack.sh:

Make sure this script is executable!

#!/bin/sh
 
test ! -z "$1" && site=$1 && shift
 
timeout=0
 
echo "Waiting for OPAC Plack socket for $site"
while [ ! -S /var/run/koha/$site/opac-plack.sock ] && [ $timeout -lt 10 ]
do
        sleep 1
        timeout=$(($timeout + 1))
done
 
chmod 777 /var/run/koha/$site/opac-plack.sock
echo "OPAC Plack socket open for $site"

unlock-intranet-plack.sh:

Make sure this script is executable!

#!/bin/sh
 
test ! -z "$1" && site=$1 && shift
 
timeout=0
 
echo "Waiting for Intranet Plack socket for $site"
while [ ! -S /var/run/koha/$site/intranet-plack.sock ] && [ $timeout -lt 10 ]
do
        sleep 1
        timeout=$(($timeout + 1))
done
 
chmod 777 /var/run/koha/$site/intranet-plack.sock
echo "Intranet Plack socket open for $site"

3. Create control scripts for startup and shutdown

Finally, create the koha-common style start and stop scripts to control your Plack processes.

/usr/sbin/koha-start-plack:

This script will start Plack processes for both the Intranet and the OPAC. Like other koha-common scripts, it takes the name of the instance as a parameter. Make sure it’s executable!

#!/bin/sh
#
# koha-start-plack -- Start plack processes for named Koha instances
#
 
set -e
 
for name in "$@"
do
    echo "Starting OPAC Plack server for $name"
    exec start-stop-daemon \
        --start \
        --chuid $name-koha \
        --exec /usr/share/koha/misc/plack/opac-plack.sh -- $name \
        | /usr/share/koha/misc/plack/unlock-opac-plack.sh $name
 
    echo "Starting Intranet Plack server for $name"
    exec start-stop-daemon \
        --start \
        --chuid $name-koha \
        --exec /usr/share/koha/misc/plack/intranet-plack.sh -- $name \
        | /usr/share/koha/misc/plack/unlock-intranet-plack.sh $name
 
done

/usr/sbin/koha-stop-plack:

This script will stop Plack processes for both the Intranet and the OPAC. It also takes the name of the instance as a parameter. Make sure it’s executable!

#!/bin/sh
#
# koha-stop-plack -- Stop plack processes for named Koha instances
 
stopopac() {
   local name="$1"
   exec start-stop-daemon \
        --stop \
        --quiet \
        --pidfile /var/run/koha/$name/opac-plack.pid
}
 
stopstaff() {
   local name="$1"
   exec start-stop-daemon \
        --stop \
        --quiet \
        --pidfile /var/run/koha/$name/intranet-plack.pid
}
 
for name in "$@"
do
echo "Stopping Plack servers for $name"
    stopopac "$name" | stopstaff "$name"
 
done
echo "Servers stopped"

4. Prepare Koha to launch Plack

/etc/init.d/koha-common:

There are two final changes needed to make Plack a first class part of your koha-common install. Both are made to your /etc/init.d/koha-common file, but they’re both really simple.

First, add the new koha-start-plack script to the do_start() function, just after the koha-start-zebra line.

     koha-start-plack $(koha-list --enabled)

Then add the new koha-stop-plack script to the do_stop() function, just after the koha-stop-zebra line.

    koha-stop-plack $(koha-list) || true

That’s it! Stop and start your koha-common service to enjoy your new Plack infrastructure.

Problems? — Check:

  • log directory is writable by your *-koha user
  • tmp directory for compiled templates is writable by your *-koha user

1 Response

Leave a Reply to pongtawat Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>