Git Product home page Git Product logo

mashtru's Introduction

mashtru

Build Status

mashtru will manage scheduled task on shared hosting (and everywhere else really)

Problem

Shared php web hosting, usually give you a limited number of scheduled task script (from 1 to 5), and if you need you app to perform some async cron jobs, and you dont want to forget about single responsibility principle, you will just need to mashtru.

Hook script

Set the only scheduled task to be the hook.php script, which will be something like this

<?php
require '../vendor/autoload.php';

use Mashtru\JobManager;
use Mashtru\Libs\Helpers\DBConfig;
use Mashtru\Libs\Helpers\RunnerConfig;

$jobs = new JobManager(
    new DBConfig(
        'localhost',
        'mashtru',
        'root',
        'root'
    ),
    new RunnerConfig(
        [
            'App\Scripts\\'
        ]
    )
);

$jobs->fire();

and imagine to have a job si defined in the file DosomeStuff.php

<?php

namespace App\Scripts;

use Mashtru\Libs\Interfaces\Job;

class DoSomeStuff implements Job
{

    public function fire(array $parameters = [])
    {

        echo "I do some cron stuff!";
        //SOME STUFF


        //RETURN AN INT
        return 0;
    }

    public function getName()
    {
        return 'DoSomeStuff';
    }
}

The fire method of this class will be called how many times you defined it in the Job configuration.

Configure Jobs

mashtru uses a single table called mashtru_jobs, there the structure is as follows

CREATE TABLE mashtru_jobs(
    id SMALLINT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255),
    class_name VARCHAR(255) DEFAULT NULL,
    args TEXT DEFAULT NULL,
    delta_minutes INT DEFAULT NULL,
    active BOOL DEFAULT 1,
    fire_time TIMESTAMP NULL DEFAULT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(),
    created_at TIMESTAMP NOT NULL,
    PRIMARY KEY(id),
    INDEX `name` (`name`)
);
column description
id the incremental id for the job
name the job name (used to identify a job)
args a text field that will be passed into the fire function (I would cast it with json_decode as config param)
class_name the name of the class which fire() method will be called
delta_minutes the difference on minutes of the iteration for this job (it will be executed every delta_minutes minutes)
priority a TINYINT value which jobs will be order by with DESC direction (0 by default) (from -128 to 127)
active whether or not the job is active
fire_time calculated column that will tell mashtru whether the job is ready to be fired or not

The class JobManager offers utility to install/uninstall/add/delete/update/get jobs from this table. The implementeation of those action is delegated to the library user. I will use it within a protected rest-api i.e. so I would be able to set manage the jobs using a rest-api.

Collabs

This is a silly lib that I wrote for me, every help of feedback is well appreciated.

ToDos

  • Make table_name configurable
  • Replace delta_minutes with cronjob-like configuration

mashtru's People

Contributors

vikkio88 avatar

Watchers

 avatar  avatar  avatar

mashtru's Issues

Log hook

we should loginfo the hook as well

FireTime issues

  • Calculate next fire time from old fire time not from now, to prevent the job to skip ahead
  • Add functionality to force the cronjob to run

force hook run

if you force the hook to run manually the next hook firetime it should be blocked

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.