Git Product home page Git Product logo

selfdestruct's Introduction

Self Destruct

This package was built with the goal in mind of creating a repeatable way of having models delete themselves after a specified amount of time. Currently, the package is fairly basic but could be expanded upon to allow deeper customisation and the ability of having per-record expiration times rather than scoped to each model.

What's in the package

This can be used on Models to give it the ability to delete after a specified time.

Table that is used to manage the destruction of Models.

How it works

This package works by using the provided Trait on a Model which hooks in to the created event. When a Model is created, it takes the sum of created_at and life_time and stores it in a seperate table. Then, every minute this table is checked for expired records and each Model is deleted.

How to use

  1. Adding the package to your project.
composer require roberthucks/selfdestruct
  1. Run migration to create the table
php artisan migrate
  1. Configure models

The first step here is to add the Trait to the Model. Add use RobertHucks\SelfDestruct\Traits\SelfDestruct; and then user it inside your Models class like so use SelfDestruct; Once these have been added to your Model you need to then add a property called $life_time. This is the amount of seconds that a Model should stay alive for.

Here is how a Model looks when modified:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use RobertHucks\SelfDestruct\Traits\SelfDestruct;

class Post extends Model
{
  use SoftDeletes;
  use SelfDestruct;

  protected $table = 'post';
  protected $fillable = [
    'title',
    'body'
  ];
  protected $dates = ['deleted_at'];

  protected $life_time = 60;
}
  1. Set up Laravel's scheduler

This package requires the use of the Task Scheduler. You should make sure that it is configured and running correctly on your system or else this package will do nothing.

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Considerations

This package relies on having a created_at field on your Model. In this future this would be a configurable field or possibly even removed for just having the current time but for now, this is a requirement of the Model.

Another thing to keep in mind is that the Task for clearing expired Models runs every minute. This means that the deletion will almost never occur on the exact time of expiry. If there is another way to perform this action I am all ears but this wasn't an issue for my use-case and therefore was a limitation I was happy to accept.

selfdestruct's People

Contributors

robert-hucks avatar

Watchers

 avatar

selfdestruct's Issues

Call to member function delete() on null

There is an issue that occurs whenever a model is deleted from the database and therefore is unretrievable for SelfDestruct. It tries to fetch the model and call delete() on it, but the model fails to return and this leads to the function call failing also, resulting in a huge amount of error logs.

A check needs to be put in place to simply remove the SelfDestruct record if the object it is trying to delete() no longer exists.

Table is cleared immediately

Every minute, the table is cleared. This looks like some broken logic with the ttl's possibly but from what I can see the timestamps all seem to line up and make sense but when logging out the values it is clear that the models being selected include those which are still within their ttl.

Error deleting models

[2019-01-09 07:06:01] local.ERROR: Call to a member function delete() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function delete() on null at /var/www/api/ticketpass.co/vendor/roberthucks/selfdestruct/src/Console/Kernel.php:20)

There is a problem with deleting models which is causing a lot of errors to be generated and the table to be full of undeleted objects.

Models not being deleted when they should

There appears to be a problem with the way in which the models are pulled out of the table to be deleted. This definitely has something to do with how they are fetched as there doesn't appear to be any errors to indicate a failed deletion.

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.