/** * This is the threads main() method that initializes the application with the autoloader and * instantiates all the necessary manager instances. * * @return void * @codeCoverageIgnore */ public function run() { try { // register the default autoloader require SERVER_AUTOLOADER; // register shutdown handler register_shutdown_function(array(&$this, "shutdown")); // log a message that we now start to connect the application $this->getInitialContext()->getSystemLogger()->debug(sprintf('%s wait to be connected', $this->getName())); // register the class loaders $this->registerClassLoaders(); // initialize the managers $this->initializeManagers(); // provision the application if ($this->getContainer()->hasProvisioningEnabled()) { $this->provision(); } // initialize the profile logger and the thread context $profileLogger = null; /** @var \AppserverIo\Logger\ThreadSafeLoggerInterface $profileLogger */ if ($profileLogger = $this->getInitialContext()->getLogger(LoggerUtils::PROFILE)) { $profileLogger->appendThreadContext('application'); } // log a message that we has successfully been connected now $this->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->info(sprintf('%s has successfully been connected', $this->getName())); // the application has successfully been initialized $this->synchronized(function ($self) { $self->applicationState = ApplicationStateKeys::get(ApplicationStateKeys::INITIALIZATION_SUCCESSFUL); }, $this); // initialize the flag to keep the application running $keepRunning = true; // wait till application will be shutdown while ($keepRunning) { // query whether we've a profile logger, log resource usage if ($profileLogger) { $profileLogger->debug(sprintf('Application %s is running', $this->getName())); } // wait a second to lower system load $keepRunning = $this->synchronized(function ($self) { $self->wait(100000 * Application::TIME_TO_LIVE); return $self->applicationState->equals(ApplicationStateKeys::get(ApplicationStateKeys::INITIALIZATION_SUCCESSFUL)); }, $this); } // log a message that we has successfully been shutdown now $this->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->info(sprintf('%s start to shutdown managers', $this->getName())); // array for the manager shutdown threads $shutdownThreads = array(); // we need to stop all managers, because they've probably running threads /** @var \AppserverIo\Psr\Application\ManagerInterface $manager */ foreach ($this->getManagers() as $manager) { $shutdownThreads[] = new ManagerShutdownThread($manager); } // wait till all managers have been shutdown /** @var \AppserverIo\Appserver\Application\ManagerShutdownThread $shutdownThread */ foreach ($shutdownThreads as $shutdownThread) { $shutdownThread->join(); } // the application has been shutdown successfully $this->synchronized(function ($self) { $self->applicationState = ApplicationStateKeys::get(ApplicationStateKeys::SHUTDOWN); }, $this); // cleanup the naming directory with the application entries $this->unload(); // log a message that we has successfully been shutdown now $this->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->info(sprintf('%s has successfully been shutdown', $this->getName())); } catch (\Exception $e) { $this->getNamingDirectory()->search(NamingDirectoryKeys::SYSTEM_LOGGER)->error($e->__toString()); } }