yf_manage_cron admin module

Fileplugins/sys/admin_modules/yf_manage_cron.class.php
Useadmin URL ?object=manage_cron

Public properties

A site sets them in its config with $PROJECT_CONF['manage_cron']['NAME'] = ...;

PropertyDefaultDescription
$_interval null
$_table null
$exec_type null

Public methods 8

active

public function active()
    public function active()
    {
        return _class('admin_methods')->active($this->_table);
    }

cron_logs

public function cron_logs()
    public function cron_logs()
    {
        return table(
            db()->from('cron_logs')->where('cron_id', (int) ($_GET['id'] ?? null))->order_by('time_start', 'desc')
        )
            ->date('time_start', ['format' => 'full'])
            ->text('log')
            ->text('time_spent');
    }

edit

public function edit()
    public function edit()
    {
        $id = (int) ($_GET['id'] ?? null);
        if ( ! $id) {
            return js_redirect(url('/@object'), false, 'Empty ID');
        }
        $a = db()->from('cron_tasks')->whereid($id)->get();
        if (empty($a['id'])) {
            common()->message_error('Cron task not found');
            return js_redirect(url('/@object'), false, 'Cron task not found');
        }
        $file_content = nl2br(file_get_contents($a['dir'] . $a['name']));
        return form($a, ['autocomplete' => 'off'])
            ->validate([
                'exec_time' => 'trim|decimal|required',
            ])
            ->on_post(function () use ($a) {
                db()->update_safe('cron_tasks', [
                    'comment' => ($_POST['comment'] ?? null),
                    'frequency' => ($_POST['digits'] ?? null) . ' ' . ($_POST['units'] ?? null),
                    'update_date' => time(),
                    'admin_id' => main()->ADMIN_ID,
                    'active' => ($_POST['active'] ?? null),
                    'exec_type' => ($_POST['exec_type'] ?? null),
                    'exec_time' => ($_POST['exec_time'] ?? null) ?: '600',
                ], 'id=' . (int) ($_GET['id'] ?? null));
                common()->admin_wall_add(['cron tasks updated: ' . $a['name'], $a['id']]);
                return js_redirect(url('/@object'));
            })
            ->info('name')
            ->container('<pre>' . $file_content . '</pre>', 'Content')
            ->select_box('exec_type', $this->exec_type)
            ->container($this->_select_f(), 'Start every')
            ->integer('exec_time', 'Max exec time, s', [
                'placeholder' => 'execution time, seconds',
            ])
            ->textarea('comment')
            ->active_box()
            ->save_and_back();
    }

show

public function show()
    public function show()
    {
        foreach ($this->_cron_files() as $name) {
            $cron_name = basename($name);
            $cron_dir = str_replace($cron_name, '', $name);
            $return = db()->from('cron_tasks')->where('name', $cron_name)->get();
            if (empty($return)) {
                db()->insert_safe('cron_tasks', [
                    'name' => $cron_name,
                    'dir' => $cron_dir,
                    'update_date' => time(),
                    'admin_id' => main()->ADMIN_ID,
                ]);
            }
        }
        return table(
            'SELECT c.*, a.login FROM ' . db('cron_tasks') . ' as c
				LEFT JOIN ' . db('sys_admin') . ' as a ON a.id = c.admin_id
				ORDER BY c.name ASC'
        )
            ->text('name')
            ->text('dir', 'Directory')
            ->text('comment', ['width' => 300])
            ->text('frequency')
            ->func('exec_type', function ($extra, $r, $_this) {
                return $this->exec_type[$extra];
            })
            ->text('exec_time', 'Max exec time, s')
            ->text('login')
            ->date('update_date', ['format' => '%d-%m-%Y %H:%M'])
            ->btn_edit(['no_ajax' => 1])
            ->btn_func('Logs', function ($value, $extra, $row_info) {
                $action_url = url('/@object/cron_logs/' . $value['id']);
                return '<a href="' . $action_url . '" class="btn btn-default btn-mini btn-xs" title="View"><i class="fa fa-lg fa-eye eye_view"></i>Logs</a>';
            }, ['no_ajax' => 1])
            ->btn_active();
    }

Methods starting with _ hooks and helpers; not routable as page actions

_cron_files

public function _cron_files()

Cron job files of the app, project and framework and their plugins; the old share/ folders are kept for sites that still use them. App / project first: the first file of a name is the one registered.

    public function _cron_files()
    {
        $globs = [];
        foreach (['cron_jobs/', 'share/cron_jobs/'] as $folder) {
            foreach ($this->_cron_globs($folder) as $place => $glob) {
                $globs[$place][] = $glob;
            }
        }
        $files = [];
        foreach ($globs as $place_globs) {
            foreach ($place_globs as $glob) {
                foreach ((array) glob($glob) as $f) {
                    $files[$f] = $f;
                }
            }
        }
        return array_values($files);
    }

_cron_globs

public function _cron_globs($folder)

Glob of each place (app, project, framework, their plugins) for a folder, as yf_core_api::get_globs() gives it, so yf_core_api::$SOURCE_ONLY_FRAMEWORK limits it to the framework.

  • $folder string
    public function _cron_globs($folder)
    {
        return _class('core_api')->get_globs($folder, 'cron.php');
    }

_init

public function _init()
    public function _init()
    {
        $this->_interval = [
            'minutes',
            'hours',
            'days',
        ];
        $this->_table = [
            'table' => 'cron_tasks',
            'fields' => [
                'name',
                'frequency',
                'active',
            ],
        ];
        $this->exec_type = [
            'sh' => 'sh',
            'include_php' => 'include_php',
            'php_script' => 'php_script',
        ];
    }

_select_f

public function _select_f()
    public function _select_f()
    {
        $options_d = $options_u = null;
        $digits = range(1, 59);
        $units = [
            'minutes' => 'minutes',
            'hours' => 'hours',
            'days' => 'days',
        ];
        $options_d = null;
        $options_u = null;
        foreach ($digits as $d) {
            $options_d .= '<option value="' . $d . '">' . $d . '</option>';
        }
        foreach ($units as $u) {
            $options_u .= '<option value="' . $u . '">' . $u . '</option>';
        }
        return '
			<select id="select_box_1" class="form-inline" class="span1" name="digits">' . $options_d . '</select>
			<select id="select_box_2" class="form-inline" class="span1" name="units">' . $options_u . '</select>
		';
    }