8

At my current employer we use Ant to execute our build scripts, and I need to add a target to our build.xml file that will start up 4 PHP scripts that are Gearman workers in the background, and then stop those scripts once the build is done.

I've looked at the 'parallel' and 'daemons' directives (is that the right word?) but I am not experienced enough with Ant to track down the info I'm missing on how to make sure the script runs in the background.

GrumpyCanuck
  • 1,438
  • 3
  • 18
  • 30
  • So you say you need to stop the PHP scripts at the end of your build process, rather than just launching them into the background and letting them run? Do you need to capture output from these scripts? – Mike Brant Aug 20 '12 at 16:16
  • I do not need to capture the output from these scripts, but they need to be stopped once the build is over. It's for running Behat tests in parallel using Gearman, and I do not have any sort of access on the server it will be running on to start them up, leave them running, and restart if they crash (for whatever reason). – GrumpyCanuck Aug 20 '12 at 16:24
  • Is it possible to have more than one build at a time? How will the gearman server and its jobs handle that? – Graham Christensen Aug 20 '12 at 16:30
  • Sure, it could happen that more than build is running at a time. Gearman should be able to handle it AFAICT – GrumpyCanuck Aug 20 '12 at 16:34
  • I found that running multiple builds lead to an indeterminate build getting any particular job. so build A would process a job for build B, causing false positives / false negatives. I ended up abstracting away the actual integration with Beanstalk so I would be able to instead create an "inline" executor to avoid dealing with namespacing the jobs per build. **Note:** we actually use Beanstalk. – Graham Christensen Aug 20 '12 at 16:36
  • Fair enough, but switching to Beanstall (or anything else) is not possible at this point. ;) – GrumpyCanuck Aug 20 '12 at 16:38

1 Answers1

2

As you're not getting many answers I'll suggest a low tech method that might get you start...

Use an ant exec task to fire off 4 background php processes writing their pid to a file which includes the build number (from environment presumably) to identify it.

Once build is complete run script again with a stop parameter and use the file naming system to find process ids, kill take and delete piddling files. Probably worth you having some sort of stale job cleaner in there too.

Shouldn't be too hard to knock up something that works until you can find a more elegant solution.

Edit:

Is this any good for you:

test.php: (this would be your worker script)

<?php while (true) { echo "Hello world" . PHP_EOL; sleep(5); }

runner.sh:

#!/usr/bin/bash

FILE_TO_RUN=test.php

if [ -z $TEST_RUNNERS ]; then
  TEST_RUNNERS=4;
fi;

if [ -z $BUILD_NUMBER ]; then
  echo "Can not run without a build number";
  exit 1;
fi;

FILE="${BUILD_NUMBER}.run"

if [ -e $FILE ]; then
    while read line;
    do
        echo "Killing process " $line
        kill -9 $line
    done
    echo "Deleting PID file"
    rm -f $FILE
    exit 0
fi  < $FILE

for ((i=1; i<=$TEST_RUNNERS; i++)); do
  echo "Setting up test runner number " $i " of " $TEST_RUNNERS;
  php $FILE_TO_RUN &
  echo "PID number: " $!
  echo $! >> "${BUILD_NUMBER}.run"
done
exit 0
Lloyd Watkin
  • 485
  • 4
  • 10
  • Hey Lloyd, thanks for the suggestion. One of the potential problems here is that I can't really alter the worker script. I'm using Behat (a BDD tool for PHP) and they have an extension for allowing you to run it as a Gearman worker. Not very transparent, I know, but extremely limiting in what I can do without heavy modification to the script itself – GrumpyCanuck Aug 20 '12 at 20:30
  • Wrote this on a mobile so you'll have to forgive it autocorrecting me and being too lazy to read back – Lloyd Watkin Aug 20 '12 at 20:32