psf1/cron-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

psf1/cron-bundle

Composer 安装命令:

composer require psf1/cron-bundle

包简介

Manage dynamically cron jobs with requests, or command, in your PHP site.

关键字:

README 文档

README

This bundle it's a fork from 'valentinmari/cron-bundle', original code by Valentin Mari.

Installation

  1. Download CronBundle using composer
  2. Enable the Bundle, if required
  3. Define cron jobs in your config

Step 1: Download CronBundle using composer

Add CronBundle by running the command:

$ composer require psf1/cron-bundle

Composer will install the bundle to your project's vendor/psf1 directory.

Step 2: Enable the bundle

Edit your .env file and set the LOCK_DSN value.

# /.env.local

LOCK_DSN=flock

If you are using Flex this step it's not required.

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new CronBundle\CronBundle(),
    );
}

Step 3: Define cron jobs in your config

Your site is ready to run cron jobs. Now, write them in a new file named cron.yml in your config folder.

#config/packages/cron.yaml

# Cron jobs.
cron:
  jobs:
    - { name: 'Test_job_1', format: '*/1 * * * *', service: 'App\Cron\TestJob1' }
    - { name: 'Test_job_2', format: '*/1 * * * *', service: 'App\Cron\TestJob2' }

  # Uncomment to execute jobs after each user request.
  # run_on_request: true

The format is like Cron, from Unix. You must define a job class, that must implement JobInterface and redefine the run() method. Inside run() you can put your Job and do anything you want. You can inject things in your job class too.

// src/Cron/CacheClearCron.php
namespace App\Cron;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use VM\Cron\JobInterface;

/**
 * Run cache clear job.
 */
class CacheClearCron implements JobInterface
{
    protected ?Application $application;

    /**
     * @param KernelInterface $kernel
     */
    public function __construct(KernelInterface $kernel)
    {
        $this->application = new Application($kernel);
        $this->application->setAutoExit(false);
    }

    /**
     * Execute job.
     *
     * @return void
     *
     * @throws \Exception
     */
    public function run(): void
    {
        $input = new ArrayInput([
            'command' => 'cache:clear',
        ]);

        $output = new NullOutput();
        $this->application->run($input, $output);
    }
}

Usage

This bundle allows two work methods.

  • Run cron jobs in each request, if it's the time. Note that you must define job format with enough time to not duplicate executions.
  • Run cron jobs by console command.

Console commands

  • cron:job:list: List all defined cron jobs with status.
  • cron:job:run: Run cron jobs.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2022-01-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固