MongoDB Tutorial 4: Apache Support

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MongoDB Production Plugin
UniServer 6-Carbo.

MongoDB adding Apache Support

Introduction

Currently our plugin reflects the standalone version as shown on right.

It has a few shortcomings

  • Lacks Apache support
  • Lacks phpMoAdmin support

This page covers Apache support

Top

Adding Apache support

Adding Apache support is easy just add the following as shown:.

 //== Start Apache
 $cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden
 run_cmd_hidden($cmd);                               // Run command

 //== Start MongoDB
 start_mongo_auth();

//-------------

 //== Stop Apache
 $cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1";  // Cmd to run hidden
 run_cmd_hidden($cmd);                            // Run command

 //== Stop MongoDB
 stop_mongo_auth();

You place these in the appropriate Mongo sections.

Problems

Each of the above starts a new-detached process. All script instruction are executed in quick succession hence that section of code may be completed before servers have time to either start or stop.

If user clicks start-client buttons before the server has started an error message can’t connect will be produced. Similarly the start button toggles if clicked again produces an error because it is trying to stop a nonexistent server.

Added to this a user is left in the dark there is no feedback informing what the current status is.

New code

Solution to the above; add a status bar for user feedback and recode to check server status and include a safety timer.

New code as follows:

   //=== Start-Stop Servers ===================================================
   case ID_DB_BUTTON:          // Button toggles
     $text =  wb_get_text(wb_get_control($window, ID_DB_BUTTON));  // get button text

     if($text == START_DB_BTXT ){ // Start server

     //== Start Apache
     $cmd = "../../usr/local/php/php.exe -n ../main/start_servers.php 1"; // Cmd to run hidden
     run_cmd_hidden($cmd);                               // Run command
     wb_set_text($statusbar_mongo, " Starting Apache"); // Inform user 

       // Check started
       $tim_count_1 = 0;                  // Reset
       $tim_str_1 = " Starting Apache ";  // Reset
       while(!apache_running()){                        // Wait for server to start
         $tim_str_1 .= "#";                             // Add progress marker
         wb_set_text($statusbar_mongo, $tim_str_1);     // Inform user 

         if($tim_count_1 >=30){                         // Failed to start
            wb_set_text($statusbar_mongo, " Failed to start Apache");  // Inform user 
          }
          wb_wait($window, 1000);                  // 1 second delay
          $tim_count_1 = $tim_count_1 + 1;         // Increment counter
       }
       wb_set_text($statusbar_mongo, " Apache started");  // Inform user 

       //== Start MongoDB
       start_mongo_auth();
       wb_set_text($statusbar_mongo, " Starting MongoDB");  // Inform user 

       // Check started
       $tim_count_1 = 0;                  // Reset
       $tim_str_1 = " Starting MongoDB "; // Reset
       while(!mongodb_running()){                       // Wait for server to start
         $tim_str_1 .= "#";                             // Add progress marker
         wb_set_text($statusbar_mongo, $tim_str_1);     // Inform user 

         if($tim_count_1 >=30){                         // Failed to start
            wb_set_text($statusbar_mongo, " Failed to start MongoDB");  // Inform user 
          }
          wb_wait($window, 1000);                  // 1 second delay
          $tim_count_1 = $tim_count_1 + 1;         // Increment counter
       }
       wb_set_text($statusbar_mongo, " MongoDB started");  // Inform user 

       //== Set buttons
       wb_set_text(wb_get_control($window,    ID_DB_BUTTON),STOP_DB_BTXT);  // set button text
       wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), TRUE);  // Enable Client button 
     }

     else{                         // Stop   server
       if(mongo_client_running()){ // Client must be stopped before Mongo Server
         $str  = "Before you can stop MongoDB \n";
         $str .= "Please close Mongo Client \n\n";

         $str .= "Type EXIT to close client\n";
         $str .= "Data from RAM needs to be written to disk.\n";

         wb_message_box($window, $str, "CLIENT RUNNING", WBC_INFO);          
         break;
       }
       else{                   // Client not running

       //== Stop Apache
       $cmd = "../../usr/local/php/php.exe -n ../main/stop_servers.php 1";  // Cmd to run hidden
       run_cmd_hidden($cmd);                               // Run command
       wb_set_text($statusbar_mongo, " Stopping Apache");  // Inform user 

       // Check stopped
       $tim_count_1 = 0;                  // Reset
       $tim_str_1 = " Stopping Apache ";  // Reset
       while(apache_running()){                         // Wait for server to stop
         $tim_str_1 .= "#";                             // Add progress marker
         wb_set_text($statusbar_mongo, $tim_str_1);     // Inform user 

         if($tim_count_1 >=30){                         // Failed to start
            wb_set_text($statusbar_mongo, " Failed to stop Apache");  // Inform user 
          }
          wb_wait($window, 1000);                  // 1 second delay
          $tim_count_1 = $tim_count_1 + 1;         // Increment counter
       }
       wb_set_text($statusbar_mongo, " Apache stopped");  // Inform user 

       //== Stop MongoDB
       stop_mongo_auth();

       // Check stopped
       $tim_count_1 = 0;                  // Reset
       $tim_count_2 = 0;                  // Reset
       $tim_str_1 = " Stopping MongoDB "; // Reset
       while(mongodb_running()){                        // Wait for server to start

         $tim_str_1 .= "#";                             // Add progress marker
         wb_set_text($statusbar_mongo, $tim_str_1);     // Inform user 

         if($tim_count_2 >=20){               // Number of # to display
           $tim_str_1 = " Stopping MongoDB "; // Reset
           $tim_count_2 = 0;                  // Reset
         }

         if($tim_count_1 >=600){                   // Failed to stop
            wb_set_text($statusbar_mongo, " Failed to stop MongoDB");  // Inform user 
          }

          wb_wait($window, 2000);                  // 2 second delay
          $tim_count_1 = $tim_count_1 + 1;         // Increment counter
          $tim_count_2 = $tim_count_2 + 1;         // Increment counter
       }
       wb_set_text($statusbar_mongo, " MongoDB stopped");  // Inform user 


         //==Set button text
         wb_set_text(wb_get_control($window,    ID_DB_BUTTON),START_DB_BTXT);  // set button text
         wb_set_enabled(wb_get_control($window, ID_CLIENT_BUTTON), FALSE);  // DISABLE Client button 
      }
     }

   break; 
   //================================================ END Start-Stop Servers ==

To end of componet section add following linr:

$statusbar_mongo = wb_create_control($win_mongo, StatusBar, " Displayed important information");

Top

Summary

With the above modifications both server are started and stopped from a single button.

Button toggles between start and stop servers.

A status bar is included this is dual purpose it displays status messages and acts as a progress bar.

Next page covers adding support for phpMoAdmin.


Top