定制 vikkio88/mashtru 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vikkio88/mashtru

Composer 安装命令:

composer require vikkio88/mashtru

包简介

A framework-agnostic php library usefull to manage scheduled tasks on shared webhosting or not

README 文档

README

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

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固