定制 dz/easy-cron-manager 二次开发

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

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

dz/easy-cron-manager

Composer 安装命令:

composer require dz/easy-cron-manager

包简介

Flexible cron manager bundle for Symfony2

README 文档

README

This bundle provide manager for managing cron table.

Setting up DZCronManagerBundle

Step 1 - Requirements and Installing the bundle

The first step is to tell composer that you want to download DZCronManagerBundle which can be achieved by typing the following at the command prompt:

$ php composer.phar require dz/easy-cron-manager

Step 2 - Enable the bundle in your kernel

The bundle must be added to your AppKernel

# app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new DZ\CronManagerBundle\DZCronManagerBundle(),
        // ...
    );
}

Example of using DZCronManagerBundle

<?php

namespace Acme\CronManagerBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

class CronController extends Controller
{
    /**
     * Displays the current crons.
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
     */
    public function listAction(Request $request)
    {
        $cronManager = $this->get('dz_cron.manager');
        $cronManager->parseCronFile();

        $this->addFlash('message', $cronManager->getOutput()); //Gets the output of crontab
        $this->addFlash('error', $cronManager->getError()); //Gets the error of crontab

        $form = $this->get('dz_cron.manager');
        return $this->render(/*list of cron page*/, array(
            'crons' => $cronManager->getCrons(), //Gets the collections of crons
            'raw'   => $cronManager->getRaw(),  //Gets a representation of the cron table file
            'form'  => $form->createView(),
        ));
    }

    /**
     * Add a cron to the cron table
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\Response|\Symfony\Bundle\FrameworkBundle\Controller\Response
     */
    public function addAction(Request $request)
    {
        $cronManager = $this->get('dz_cron.manager');
        $cronManager->parseCronFile();

        $cron = $cronManager->create(); //Create cron instance

        $form = $this->get('dz_cron.form.cron');
        $form->setData($cron);

        if ('POST' == $request->getMethod()) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $cronManager->add($cron); //Add cron to the cron table collection
                
                //Write the current crons in cron table and save process outputs
                $cronManager->writeCronFileAndSaveProcessOutputs(); 

                $this->addFlash('message', $cronManager->getOutput());
                $this->addFlash('error', $cronManager->getError());

                return $this->redirect($this->generateUrl(/*list of cron page*/));
            }
        }

        return $this->render(/*list of cron page*/, array(
            'crons' => $cronManager->getCrons(),
            'raw'   => $cronManager->getRaw(),
            'form'  => $form->createView(),
        ));
    }

    /**
     * Edit a cron
     *
     * @param integer $id - The line of the cron in the cron table
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\RedirectResponse|\Symfony\Bundle\FrameworkBundle\Controller\Response
     */
    public function editAction($id = null, Request $request)
    {
        $cronManager = $this->get('dz_cron.manager');
        $cronManager->parseCronFile();

        if (null === $id || ! $cron = $cronManager->getCrons()->get($id)) {
            throw new NotFoundHttpException(sprintf('Unable to find the object with id : %s', $id));
        }

        $form = $this->get('dz_cron.form.cron');
        $form->setData($cron);
        if ('POST' == $request->getMethod()) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $cronManager->getCrons()->set($id, $cron);
                $cronManager->writeCronFileAndSaveProcessOutputs();

                $this->addFlash('message', $cronManager->getOutput());
                $this->addFlash('error', $cronManager->getError());

                return $this->redirect($this->generateUrl(/*list of cron page*/));
            }
        }

        return $this->render(/*cron edit page*/, array(
            'form'  => $form->createView(),
        ));
    }

    /**
     * Wake up a cron from the cron table
     *
     * @param integer $id - The line of the cron in the cron table
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\RedirectResponse
     */
    public function wakeupAction($id)
    {
        $cronManager = $this->get('dz_cron.manager');

        $cronManager->parseCronFile();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        $cronManager->getCrons()->get($id)->wakeup(); //Wakeup a cron

        $cronManager->writeCronFileAndSaveProcessOutputs();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        return $this->redirect($this->generateUrl(/*list of cron page*/));
    }

    /**
     * Suspend a cron from the cron table
     *
     * @param integer $id - The line of the cron in the cron table
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\RedirectResponse
     */
    public function suspendAction($id)
    {
        $cronManager = $this->get('dz_cron.manager');

        $cronManager->parseCronFile();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        $cronManager->getCrons()->get($id)->suspend(); //Suspend a cron

        $cronManager->writeCronFileAndSaveProcessOutputs();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        return $this->redirect($this->generateUrl(/*list of cron page*/));
    }

    /**
     * Remove a cron from the cron table
     *
     * @param $id The line of the cron in the cron table
     *
     * @return \Symfony\Bundle\FrameworkBundle\Controller\RedirectResponse
     */
    public function deleteAction($id, Request $request = null)
    {
        $cronManager = $this->get('dz_cron.manager');

        $cronManager->parseCronFile();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        $cronManager->remove($id);

        $cronManager->writeCronFileAndSaveProcessOutputs();
        $this->addFlash('message', $cronManager->getOutput());
        $this->addFlash('error', $cronManager->getError());

        return $this->redirect($this->generateUrl(/*list of cron page*/));
    }

    /**
     * Get log file action
     *
     * @param integer $id   - The line of the cron in the cron table
     * @param string  $type - The type of log file
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function logFileAction($id, $type)
    {
        $cronManager = $this->get('dz_cron.manager');
        $cronManager->parseCronFile();

        $cron = $cronManager->getCrons()->get($id);

        $data = array();
        $data['file'] =  ($type == 'log') ? $cron->getLogFile() : $cron->getErrorFile();
        $data['content'] = file_get_contents($data['file']);

        $serializer = new Serializer(array(), array('json' => new JsonEncoder()));

        return new Response($serializer->serialize($data, 'json'));
    }
}

dz/easy-cron-manager 适用场景与选型建议

dz/easy-cron-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「cron management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dz/easy-cron-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dz/easy-cron-manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 dz/easy-cron-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-17