sfMCronPlugin - 1.1.1

This plugin allows to execute function & class methods according certain time specification.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

sfMCron plugin

The sfCron is a symfony plugin that provides a convenient work with cron.

This plugin allows to execute function & class methods according certain time specification.

With this plugin you can add, delete handlers, watch their list. Display priorities launch, monitor the work of handlers.

Installation

  • Create table in database

      CREATE TABLE `sf_cron` (
         `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
         `handler_spec` VARCHAR(255) COLLATE utf8_general_ci NOT NULL DEFAULT '',
          `vars` TEXT COLLATE latin1_swedish_ci,
          `exec_spec` VARCHAR(255) COLLATE utf8_general_ci NOT NULL DEFAULT '',
          `start_process` DATETIME DEFAULT NULL,
          `start_exec` DATETIME DEFAULT NULL,
          `finish_exec` DATETIME DEFAULT NULL,
          `exec_time` INTEGER(11) NOT NULL DEFAULT '0',
          `active` TINYINT(1) NOT NULL DEFAULT '1',
          `priority` INTEGER(11) NOT NULL DEFAULT '100',
          `cdate` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
          PRIMARY KEY (`id`)
       )ENGINE=MyISAM
       AUTO_INCREMENT=11 CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
    
  • Add to cron

       * * * * * user php /path_to_project/symfony --application=frontend cron:process
       30 * * * * user php /path_to_project/symfony --application=frontend cron:check
    
  • Set your preferences in plugins/sfMCronPlugin/config/settings.yml

       [yml]
       all:
         .settings:
    
          # sfMCronPlugin
           cron:
             table: "sf_cron"
             lock_name: "cron"
             crash-time: 3600
             mail_callable: "sfCron::sendAdminMail" # standart notify handler
             mail_settings:
               mailer: mail
               charset: "utf-8"
               sender: "test@test.ru"
               from: "test@test.ru"
               addReplyTo: "test@test.ru"
               addAddress: "test1@test.ru"
    

Details

       // execute handlers every minutes
       // use table lock that the handler did not work in parallel
       * * * * * user php /path_to_project/symfony cron:process

       // check handlers and notify developers if handler did not work
       30 * * * * user php /path_to_project/symfony cron:check

Use

  • Add handler:

      $ symfony cron:add "MyClass->execHandler" "* * * * *"
      $ symfony cron:add "MyClass->execHandlerA" "30 0 * * *"
    
  • Delete handler

      $ symfony cron:remove "MyClass->execHandler" //delete all MyClass->execHandler handlers
      $ symfony cron:remove "MyClass->execHandler" "* * * * *" //delete MyClass->execHandler handler with exec time  * * * *
    
  • List of handlers

      $ symfony cron:list
    

Debug

  • Test email function

      $ symfony cron:test-email
    
  • Execute handler for test

      $ symfony cron:exec "MyClass->execHandler"
    

Own notify function

  • Set option

       [yml]
       mail_callable: "MyNotify::notifyDevelopers"
    
  • Write simple code

        [php]
        class MyNotify
        {
            public static function notifyDevelopers($args)
            {
                echo $args['subject'];
                echo $args['body'];
            }
        }
    

Call syntax

        MyClass->execHandler //call method execHandler of class MyClass

        MyUser->getSomeClass->execSomeHandler // get object from MyUser->getSomeClass() and exec method execSomeHandler

        MyClass::execHandler //call static method execHandler of class MyClass

Synopsis:

        // Add handler to cron table
        sfCron::getInstance()->addHandler('MyClass->MyHandlerA', '24 * * * *', array("p1" => "v1", "p2" => "v2"));
        sfCron::getInstance()->addHandler('MyClass::MyMethod', '* * * * *');
        sfCron::getInstance()->addHandler('MyFunction', '* * * * *');

        // Delete handler from cron table
        sfCron::getInstance()->deleteHandler('MyClass->MyHandlerA');
        sfCron::getInstance()->deleteHandler('MyLibrary.MyMethod', '* 10-18 * * *');

        // Process all cron table
        sfCron::getInstance()->process();

        // Process one cron table record
        sfCron::getInstance()->process($record_id);

        // Process search for crashed handlers
        // This function should be executed every 30 minutes, you need to add this function into the crontab file
        sfCron::getInstance()->check();