From bdc3d6a8357085def09fc1876225722706603bac Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 12 Feb 2014 13:47:40 +1300 Subject: [PATCH] Add Resque_Event::listen() call to register the testsession state switcher with the silverstripe-resque module. --- _config.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/_config.php b/_config.php index f854753..f849844 100644 --- a/_config.php +++ b/_config.php @@ -1,4 +1,27 @@ get('TestSessionEnvironment')->loadFromFile(); \ No newline at end of file +Injector::inst()->get('TestSessionEnvironment')->loadFromFile(); + +/** + * This closure will run every time a Resque_Event is forked (just before it is forked, so it applies to the parent + * and child process). + */ +if(class_exists('Resque_Event') && class_exists('SSResqueRun')) { + Resque_Event::listen('beforeFork', function($data) { + global $databaseConfig; + + // Reconnect to the database - this may connect to the old DB first, but is required because these processes + // are long-lived, and MySQL connections often get closed in between worker runs. We need to connect before + // calling {@link TestSessionEnvironment::loadFromFile()}. + DB::connect($databaseConfig); + + $testEnv = Injector::inst()->get('TestSessionEnvironment'); + + if($testEnv->isRunningTests()) { + $testEnv->loadFromFile(); + } else { + $testEnv->endTestSession(); + } + }); +}