Opencart: setting config defaults before db connection

Created on 23 Jul 2015  路  6Comments  路  Source: opencart/opencart

My database host recently had some downtime and i notices that the store front still shows but with the default template. (i would rather have a white screen of death)
in index.php i have changed a line which sets maintenance mode as its default

// Config
$config = new Config();
$registry->set('config', $config);

to

// Config
$config = new Config();
$config->set('config_maintenance',1);
$registry->set('config', $config);

another fix would be to load a file with all or some defaults like if you wanted a custom maintenance page you would need to set the template.

// Config
$config = new Config();
$config->load('defaults');
$registry->set('config', $config);

// defaults.php

$_['config_maintenance'] = 1;
$_['config_template'] = 'custom';

this will give you the right result (EVENTUALLY), unfortunately users will probably leave before the maintenance page actually loads because of the vast amount of calls to a database which wont connect.

Most helpful comment

I think it's better to consider what kind of maintenance is needed.
I'm thinking something like this on index.php or whatever file as early as possible

//
// Maintenance check
// =========================================

// Emergency maintenance
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(!$db->connected()) {
    mail('[email protected]', 'DB Down!', 'Lorem ipsum');
    echo '<h2>Database Connection Issue</h2><p>Could not connect to MySQL</p>'; // // or use html for better design
    exit();
}

// Hard maintenance
// - Use to update core or any task required complete site off
// - Admin also unable to visit front
if (file_exists('.maintenance')) {
    echo '<h2>Maintenance</h2><p>Please wait and back in a minutes..</p>'; // or use html for better design
    exit();
}

// Soft maintenance
// - Nothing here. Use Admin maintenance setting for site development
// - Admin able to visit front
// - Nice custom maintenance page for visitor (from theme)

All 6 comments

The construct method causes 14 errors, this doesn't take into account the call itself ($db->query() causes an additional 4) ;
The script should be exited after a connection hasn't been established, and a public property created which can be accessed by the driver;

public $connected;
public function __construct($hostname, $username, $password, $database) {

    $this->link = new \mysqli($hostname, $username, $password, $database);

    if ($this->link->connect_error) {
        trigger_error('Error: Could not make a database link (' . $this->link->connect_errno . ') ' . $this->link->connect_error);
        exit();
    }
    $this->connected = true;
    $this->link->set_charset("utf8");
    $this->link->query("SET SQL_MODE = ''");
}
.....

public function __destruct() {
    if($this->connected){
        $this->link->close();           
    }
}    

now only 2 errors occur one of which is triggered intentionally.

//db.php
// additional method

public function connected(){
    return $this->db->connected;
}

usage
index.php

$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, 'wrongpassword', DB_DATABASE);
if(!$db->connected()){
    $config->set('config_maintenance',1);       
    $config->set('config_template','');  


    /// set required classes in registry
    $response = new Response();
    ....
    ....
    $registry->set('document', new Document());
    ....

    $registry->set('response', $response);

    $controller = new Front($registry);
    $controller->dispatch(new Action('common/maintenance'), new Action('error/not_found'));
    $response->output();
}

A maintenance mode should be a configuration settable configurations through admin panel.
I don't think hardcoding anywhere it ALSO in maintenance active is a good idea.

Hard-coding is the only way to achieve this, if your database is down then those config settings cant be accessed.

If you db goes down then a custom page should display rather than a site which resembled a completely fresh install of opencart.

looking into this now

I think it's better to consider what kind of maintenance is needed.
I'm thinking something like this on index.php or whatever file as early as possible

//
// Maintenance check
// =========================================

// Emergency maintenance
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if(!$db->connected()) {
    mail('[email protected]', 'DB Down!', 'Lorem ipsum');
    echo '<h2>Database Connection Issue</h2><p>Could not connect to MySQL</p>'; // // or use html for better design
    exit();
}

// Hard maintenance
// - Use to update core or any task required complete site off
// - Admin also unable to visit front
if (file_exists('.maintenance')) {
    echo '<h2>Maintenance</h2><p>Please wait and back in a minutes..</p>'; // or use html for better design
    exit();
}

// Soft maintenance
// - Nothing here. Use Admin maintenance setting for site development
// - Admin able to visit front
// - Nice custom maintenance page for visitor (from theme)

ok fixed. will get my staff to a better maintenance page before the release.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emilioj picture emilioj  路  4Comments

danceleisure picture danceleisure  路  3Comments

osworx picture osworx  路  5Comments

RajatJain4061 picture RajatJain4061  路  5Comments

calderwood picture calderwood  路  3Comments