This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mischa Braam
committed
Jun 8, 2020
0 parents
commit 5b097ad
Showing
10 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace WeProvide\SessionCleaner\Cron; | ||
|
||
use Exception; | ||
use Psr\Log\LoggerInterface; | ||
use WeProvide\SessionCleaner\Service\SessionServiceInterface; | ||
|
||
class CleanSessions | ||
{ | ||
/** @var SessionServiceInterface */ | ||
protected $sessionService; | ||
|
||
/** @var LoggerInterface */ | ||
protected $logger; | ||
|
||
/** | ||
* CleanSessions constructor. | ||
* | ||
* @param SessionServiceInterface $sessionService | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(SessionServiceInterface $sessionService, LoggerInterface $logger) | ||
{ | ||
$this->sessionService = $sessionService; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function execute() | ||
{ | ||
$startTime = microtime(true); | ||
$cleanedSessions = 0; | ||
$this->logger->info('------- Start Cleanup ---------'); | ||
|
||
try { | ||
$cleanedSessions = $this->sessionService->cleanExpiredSessions(); | ||
} catch (Exception $e) { | ||
$this->logger->error($e->getMessage()); | ||
} | ||
|
||
$executionTime = round((microtime(true) - $startTime), 2); | ||
$this->logger->info(sprintf('------- End Cleanup (execution time: %s sec, cleaned sessions: %d) ---------', $executionTime, $cleanedSessions)); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 We Provide | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# We Provide Magento 2 Module Session Cleaner | ||
|
||
Magento 2 can store sessions in the database table `session`. However, sessions pile up which results in an enormous database. This module periodically cleans expired sessions. | ||
|
||
## Installation | ||
|
||
`composer require weprovide/magento2-module-sessioncleaner` | ||
|
||
## License | ||
|
||
This module for Magento 2 has been released under the MIT license and open for improvements, please share your thoughts which will be much appreciated. | ||
|
||
## Authors | ||
|
||
- Mischa Braam ([@mischabraam](https://github.com/mischabraam)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace WeProvide\SessionCleaner\Service; | ||
|
||
use Magento\Framework\Exception\SessionException; | ||
|
||
class SessionService implements SessionServiceInterface | ||
{ | ||
protected $scopeConfig; | ||
protected $connection; | ||
protected $sessionTable; | ||
protected $logger; | ||
|
||
const SCOPE_CONFIG_COOKIE_LIFETIME_PATH = 'web/cookie/cookie_lifetime'; | ||
|
||
/** | ||
* SessionService constructor. | ||
* | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | ||
* @param \Magento\Framework\App\ResourceConnection $resource | ||
* @param \Psr\Log\LoggerInterface $logger | ||
* @throws SessionException | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | ||
\Magento\Framework\App\ResourceConnection $resource, | ||
\Psr\Log\LoggerInterface $logger | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
$this->connection = $resource->getConnection(); | ||
$this->sessionTable = $resource->getTableName('session'); | ||
$this->logger = $logger; | ||
|
||
$this->checkConnection(); | ||
} | ||
|
||
/** | ||
* Checks database connection and throws exception if failed. | ||
* | ||
* @return void | ||
* @throws \Magento\Framework\Exception\SessionException | ||
*/ | ||
protected function checkConnection(): void | ||
{ | ||
if (!$this->connection) { | ||
throw new SessionException(__('Write DB connection is not available')); | ||
} | ||
if (!$this->connection->isTableExists($this->sessionTable)) { | ||
throw new SessionException(__('DB storage table does not exist')); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function cleanExpiredSessions(): int | ||
{ | ||
$expired = $this->connection->delete($this->sessionTable, ['session_expires < UNIX_TIMESTAMP() - ?' => $this->scopeConfig->getValue(self::SCOPE_CONFIG_COOKIE_LIFETIME_PATH)]); | ||
$this->connection->query('OPTIMIZE TABLE ' . $this->sessionTable); | ||
|
||
return $expired; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace WeProvide\SessionCleaner\Service; | ||
|
||
interface SessionServiceInterface | ||
{ | ||
/** | ||
* Cleans expired sessions and returns the number of sessions that were expired. | ||
* | ||
* @return int number of sessions that were expired | ||
*/ | ||
public function cleanExpiredSessions(): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "weprovide/magento2-module-sessioncleaner", | ||
"type": "magento2-module", | ||
"version": "1.0.0", | ||
"require": { | ||
"php": "~7.1.3||~7.2.0||~7.3.0", | ||
"magento/framework": "102.0.*" | ||
}, | ||
"authors": [ | ||
{ | ||
"email": "[email protected]", | ||
"name": "Mischa Braam" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"WeProvide\\SessionCleaner\\": "" | ||
}, | ||
"files": [ | ||
"registration.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> | ||
<group id="default"> | ||
<job name="session_clean_expired" instance="WeProvide\SessionCleaner\Cron\CleanSessions" method="execute"> | ||
<!--run the cron every hour --> | ||
<schedule>0 * * * *</schedule> | ||
</job> | ||
</group> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="WeProvide\SessionCleaner\Service\SessionServiceInterface" type="WeProvide\SessionCleaner\Service\SessionService"/> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="WeProvide_SessionCleaner" setup_version="1.0.0"/> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register( | ||
ComponentRegistrar::MODULE, | ||
'WeProvide_SessionCleaner', | ||
__DIR__ | ||
); |