さくらレンタルサーバーとかで制限以上のクロンタブ(crontab)を使う方法。

20181108

Logging

さくらレンタルサーバーとかで制限以上のクロンタブ(crontab)を使う方法
10年ぐらいまえのコードを見直して改善したコードが下記になります。
10年前はクラスを使わない方法で構築したのだけど、今回はクラスの概念を
使って構築。ここ何年かで自分のコーディングの技術は上がっているかといえば
そうでもないですが、昔よりかは技術の幅は広がっていると思いたいw
※qiita日付は数年前にUPしたのですけど、コード自体は10年前のコードです。

追記:2022/10/08 修正:詳しくはコチラ
https://qiita.com/question909/items/8f1df9b62ab4fba76243

<?php
//5分刻みに対して有効な無限クローン 処理が負荷の場合どうなるかは知りません。
date_default_timezone_set('Asia/Tokyo');
class cron{

    public function d_m($obj)
    {
            if($obj->m==="*")return true;
            if(preg_match("/,/",$obj->m))return $this->comma($obj->m,date("m"));
            if((int)$obj->m === (int)date("m"))return true;

            return false;
    }

    public function d_d($obj)
    {
            if($obj->d==="*")return true;
            if(preg_match("/,/",$obj->d))return $this->comma($obj->d,date("d"));
            if((int)$obj->d === (int)date("d"))return true;

            return false;
    }

    public function d_H($obj)
    {
            if(preg_match("/\*\/[0-9]{1,2}/",$obj->H)){
                $H = explode("/",$obj->H);
                if((int)$H[1]>0 && ((int)date("H") % (int)$H[1])===0)return true;
            }else{
                if($obj->H==="*")return true;
                if(preg_match("/,/",$obj->H))return $this->comma($obj->H,date("H"));
                if((int)$obj->H === (int)date("H"))return true;

            }
            return false;
    }

    public function d_i($obj)
    {
            if(preg_match("/\*\/[0-9]{1,2}/",$obj->i)){
                $i = explode("/",$obj->i);
                if((int)$i[1]>0 && ((int)date("i") % (int)$i[1])===0)return true;
            }else{
                if($obj->i==="*")return true;
                if(preg_match("/,/",$obj->i))return $this->comma($obj->i,date("i"));
                if((int)$obj->i === (int)date("i"))return true;

            }
            return false;
    }

    //曜日 0=日曜日 6=土曜日
    public function d_w($obj)
    {
            if((int)$obj->w[date("w")]===1)return true;
            return false;
    }

    public  function comma($c="",$t=""){
        if($c==="")return false;
        if($t==="")return false;
        $flg = false;
        foreach(explode(",",$c) as $cc){
            if((int)$cc===(int)$t)$flg = true;
        }
        return $flg;
    }


    public function d_command($obj){
        if($obj->command){
            exec($obj->command . " > /dev/null &");
        }
        return true;
    }

}
if ($argv[1]) {
   $filename = $argv[1];
    if(is_file($filename)){
        $jsn = json_decode(file_get_contents($filename));
        $cron = new cron();
        try {
            foreach($jsn as $obj){
                if($cron->d_m($obj)){
                    if($cron->d_d($obj)){
                        if($cron->d_H($obj)){
                            if($cron->d_i($obj)){
                                if($cron->d_w($obj)){
                                    $cron->d_command($obj);//波動拳{{{{
                                }
                            }
                        }
                    }
                }
            }
        } catch (\Throwable $th) {
            print $th->getMessage();
        }
    }
}

コメントは受け付けていません。

タグ

08, 10, 2022, 39, 5, Asia, class, cron, crontab, date, default, function, if, lt, obj, php, public, qiita, set, timezone, Tokyo, UP, , クラス, クローン, クロン, コーディング, コード, ここ, コチラ, サーバー, さくら, タブ, まえ, レンタル, 下記, 今回, 何年か, 修正, 処理, 分刻み, 制限, 場合, , 技術, 改善, , 方法, 日付, , 有効, 概念, 構築, 無限, 自体, 自分, 負荷, 追記,