Cleaning DB

Added 2 cronjobs to clean up stale data
related to openid/oauth2

Change-Id: I71fbac0dae738833b81c9185e76005a31cbe8f32
This commit is contained in:
Sebastian Marcet 2017-08-12 01:18:46 -03:00
parent 76c37b61e6
commit 7aea9b139a
3 changed files with 128 additions and 5 deletions

View File

@ -0,0 +1,56 @@
<?php namespace App\Console\Commands;
/**
* Copyright 2017 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* Class CleanOAuth2StaleData
* @package Console\Commands
*/
final class CleanOAuth2StaleData extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'idp:oauth2-clean';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'idp:oauth2-clean';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clean OAuth2 stale data';
const IntervalInSeconds = 86400; // 1 day;
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// delete void access tokens
DB::raw("delete from oauth2_access_token where DATE_ADD(created_at, INTERVAL lifetime second) <= UTC_TIMESTAMP();");
}
}

View File

@ -0,0 +1,63 @@
<?php namespace App\Console\Commands;
/**
* Copyright 2017 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* Class CleanOpenIdStaleData
* @package Console\Commands
*/
final class CleanOpenIdStaleData extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'idp:openid-clean';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'idp:openid-clean';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clean OpenId stale data';
const IntervalInSeconds = 86400; // 1 day;
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$interval = self::IntervalInSeconds;
// delete void associations
DB::raw("delete from openid_associations where DATE_ADD(issued, INTERVAL lifetime second) <= UTC_TIMESTAMP();");
// delete old exceptions trails
DB::raw("delete from user_exceptions_trail where DATE_ADD(created_at, INTERVAL {$interval} second) <= UTC_TIMESTAMP();");
// delete old user actions
DB::raw("delete from user_actions where DATE_ADD(created_at, INTERVAL 1 year) <= UTC_TIMESTAMP()");
}
}

View File

@ -1,10 +1,12 @@
<?php
namespace App\Console;
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
/**
* Class Kernel
* @package App\Console
*/
class Kernel extends ConsoleKernel
{
/**
@ -14,6 +16,8 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
// Commands\Inspire::class,
Commands\CleanOAuth2StaleData::class,
Commands\CleanOpenIdStaleData::class,
];
/**
@ -24,7 +28,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('idp:oauth2-clean')->dailyAt("02:30")->withoutOverlapping();
$schedule->command('idp:openid-clean')->dailyAt("03:30")->withoutOverlapping();
}
}