Skip to content

Commit

Permalink
Enable session clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
robbanl committed Jan 24, 2018
1 parent 6a11b44 commit ce30334
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
94 changes: 92 additions & 2 deletions Cron/Sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,113 @@

namespace Codepeak\Optimize\Cron;

use Codepeak\Core\Logger\Logger;
use Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Framework\App\ResourceConnection;

/**
* Class Sessions
*
* @package Codepeak\Optimize\Cron
* @license MIT License https://opensource.org/licenses/MIT
* @license GNU License http://www.gnu.org/licenses/
* @author Robert Lord, Codepeak AB <robert@codepeak.se>
* @link https://codepeak.se
*/
class Sessions
{
/**
* @var
*/
const DELETE_LIMIT = 1000;

/**
* @var Logger
*/
protected $logger;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var ResourceConnection
*/
protected $resourceConnection;

/**
* Sessions constructor.
*
* @param Logger $logger
* @param ScopeConfigInterface $scopeConfig
* @param ResourceConnection $resourceConnection
*/
public function __construct(
Logger $logger,
ScopeConfigInterface $scopeConfig,
ResourceConnection $resourceConnection
) {
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
$this->resourceConnection = $resourceConnection;
}

/**
* Execute the cron
*
* @return void
*/
public function execute()
{
// Do the magic clean up here
// Make sure function is enabled
if ($this->scopeConfig->getValue('codepeak_optimize/session/enabled') == '1') {
// Fetch the expiry limit
$expiryLimit = intval($this->scopeConfig->getValue('codepeak_optimize/session/expiry_limit'));

// Fetch the delete limit
$deleteLimit = intval($this->scopeConfig->getValue('codepeak_optimize/session/delete_limit'));

// Set default value if nothing was given
if (!$deleteLimit) {
$deleteLimit = 1000;
}

// Make a note in the log about this
$this->logger->info('Looking for expired sessions with an additional ' . $expiryLimit . ' days...');

// Calculate the expiry limit in unix timestamp
$expiryLimit = time() - ($expiryLimit * 86400);

// Fetch the table name
$sessionTableName = $this->resourceConnection->getTableName('session');

// Setup the count SQL
$sqlCount = 'SELECT COUNT(*) as `count` FROM `%s` WHERE `session_expires` <= %s LIMIT ' . $deleteLimit;
$sqlCount = sprintf(
$sqlCount,
$sessionTableName,
$expiryLimit
);

// Setup the removal SQL
$sqlRemove = 'DELETE FROM `%s` WHERE `session_expires` <= %s LIMIT ' . $deleteLimit;
$sqlRemove = sprintf(
$sqlRemove,
$sessionTableName,
$expiryLimit
);

// Fetch a database connection
$connection = $this->resourceConnection->getConnection();

// Count the number of items to be removed
$removalCount = intval($connection->query($sqlCount)->fetchColumn(0));

// Remove the sessions
$connection->query($sqlRemove);

// Make a note in the log about this
$this->logger->info('Finished cleaning up. Removed ' . $removalCount . ' expired sessions');
}
}
}
8 changes: 6 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="optimize" showInDefault="1" showInStore="0" showInWebsite="1" sortOrder="10" translate="label">
<section id="codepeak_optimize" showInDefault="1" showInStore="0" showInWebsite="1" sortOrder="10" translate="label">
<label>Optimize</label>
<tab>codepeak</tab>
<resource>Codepeak_Optimize::config_codepeak_optimize</resource>
<group id="session" showInDefault="1" showInStore="0" showInWebsite="1" sortOrder="10" translate="label">
<group id="session" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="10" translate="label">
<label>Session</label>
<field id="enabled" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="10" translate="label" type="select">
<label>Enable</label>
Expand All @@ -16,6 +16,10 @@
<label>Expiry limit</label>
<comment>How many extra days to keep an expired session when keeping them in the database</comment>
</field>
<field id="delete_limit" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="20" translate="label" type="text">
<label>Delete limit</label>
<comment>How many entries to delete on each run. Method is executed every other hour.</comment>
</field>
</group>
</section>
</system>
Expand Down
5 changes: 3 additions & 2 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<optimize>
<codepeak_optimize>
<session>
<expiry_limit>14</expiry_limit>
<delete_limit>1000</delete_limit>
</session>
</optimize>
</codepeak_optimize>
</default>
</config>

0 comments on commit ce30334

Please sign in to comment.