GithubHelp home page GithubHelp logo

spinen / laravel-garbage-man Goto Github PK

View Code? Open in Web Editor NEW
33.0 10.0 2.0 117 KB

Scheduled job to clean out Laravel's soft deleted records at configured interval

Home Page: https://spinen.com

PHP 100.00%
laravel laravel-5-package spinen soft-deletes laravel-garbage-man

laravel-garbage-man's Introduction

SPINEN's Laravel Garbage Man

Latest Stable Version Latest Unstable Version Total Downloads License

The soft deletes are great in Laravel to make sure that some deleted data can be recovered. This package allows you to configure an array of models with how many days that you want the soft deleted data to stay in the database.

Build Status

Branch Status Coverage Code Quality
Develop Build Status Code Coverage Scrutinizer Code Quality
Master Build Status Code Coverage Scrutinizer Code Quality

Prerequisite

NOTE: If you need to use < PHP 7.2, please stay with version 1.x

As side from Laravel >= 5.1.10 (5.1.10 is the first version that had the warn method, so that is the minimum for logging), there is 1 package that is required.

Install

Install Garbage Man:

$ composer require spinen/laravel-garbage-man

For >= Laravel 5.5, you are done with the installation

The package uses the auto registration feature of Laravel 5.

Upgrading to 2.x from 1.x

As of Laravel 5.8 (and deprecated in 5.4), the fire() method on the dispatcher contract was removed in favor of dispatch(). Therefore, we have updated our code to use dispatch(). You will need to change fire() to dispatch() in your config/garbageman.php file.

Using the command

The command is registered with laravel as garbageman:purge. You can run it one of 2 ways...

  1. from the command line php artisan garbageman:purge;
  2. via a scheduled task.

To automatically run the script as a scheduled job, then add the following to the schedule method of App\Console\Kernel.php:

$schedule->command('garbageman:purge')
         ->daily();

You can use whatever schedule that you need to keep the records purged out. Just review the list at http://laravel.com/docs/master/scheduling#schedule-frequency-options.

You can also use any of the advanced configuration options of the task scheduler like "Task Output" or "Task Hooks" as listed on the Laravel documentation.

Configuration

Publish the package config file to config/garbageman.php:

$ php artisan vendor:publish

This file is fully documented. You will need to make the changes to that file to suit your needs. There are 3 main configuration items...

  1. Dispatch purge events - Dispatch events on purge of each record.
  2. Logging level - Level to log.
  3. Schedule - Models & number of days to allow the soft deleted record to stay before purging.

Dispatch purge events (dispatch_purge_events)

You may hook into the purge of each record by throwing events before & after deleting of each record. There are 2 events thrown:

  • garbageman.purging:<full/model/name>
  • garbageman.purged:<full/model/name>

The model is passed with each of the events. The "purging" event is thrown just before the actual delete & "purged" is thrown just after the actual deletion.

Please note: This is an expensive operation as it requires a SQL command for each record to delete so that the record can be thrown with the events. Therefore, unless you need to catch the events to perform some other action, leave this false to allow all records per model to get deleted with a single SQL call.

Logging level (logging_level)

The level that log messages are generated, which will display information on the console output and in the logs.

Level Description
0 Emergency: system is unusable
1 Alert: action must be taken immediately
2 Critical: critical conditions
3 Error: error conditions
4 Warning: warning conditions
5 Notice: normal but significant condition
6 (default) Info: informational messages
7 Debug: debug - level messages

There is a key for the console & one for the log. Here is an example...

'logging_level' => [
    'console' => 3,
    'log'     => 6,
],

Alternatively, you can set the levels with environmental variables GARBAGEMAN_CONSOLE_LOG_LEVEL and GARBAGEMAN_LOG_LEVEL.

Schedule (schedule)

The age is in days for each model. Here is an example...

'schedule' => [
    App\ModelOne::class => 14,
    App\ModelTwo::class => 30,
],

This would purge any ModelOnes that were deleted over 14 days ago and any ModelTwos that were deleted over 30 days ago.

laravel-garbage-man's People

Contributors

jimmypuckett avatar laravel-shift avatar ssfinney avatar stephenantalis avatar thaisonle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

fishnux rdx11

laravel-garbage-man's Issues

Have hidden requirement

We are type hinting Illuminate\Database\Eloquent\Builder & Illuminate\Database\Eloquent\Model in our code, but we are not requiring illuminate/database in composer. This is not a major issue, as one will not use this package without laravel, but need to get it in the required section.

Is not working with Laravel 5.6's new logging

5.6 has new logging, which will break this package...

Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover

   ReflectionException  : Class Illuminate\Log\Writer does not exist

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:826
    822|
    823|             // If the class is null, it means the dependency is a string or some other
    824|             // primitive type which we can not resolve since it is not a class and
    825|             // we will just bomb out with an error since we have no-where to go.
  > 826|             $results[] = is_null($dependency->getClass())
    827|                             ? $this->resolvePrimitive($dependency)
    828|                             : $this->resolveClass($dependency);
    829|         }
    830|

  Exception trace:

  1   ReflectionParameter::getClass()
      /vendor/laravel/framework/src/Illuminate/Container/Container.php:826

  2   Illuminate\Container\Container::resolveDependencies()
      /vendor/laravel/framework/src/Illuminate/Container/Container.php:795

  Please use the argument -v to see more details.
Script @php artisan package:discover handling the post-autoload-dump event returned with error code 1

The model [App\User] does not support soft deleting.

The model [App\User] does not support soft deleting.

if (!method_exists($model, 'onlyTrashed') || !method_exists($model, 'forceDelete')) {

Soft-deleting is enabled for this model. forceDelete() exists, but onlyTrashed() doesn't:
https://github.com/laravel/framework/blob/9b10dcb31f1cc81322f9ec193aea0a8ba9e6db88/src/Illuminate/Database/Eloquent/SoftDeletes.php#L29
(Laravel 5.5)

Perhaps we could delete this check?

!method_exists($model, 'onlyTrashed')

P.S.: I would do a pull request, but I'm not familiar with PHP packages.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.