Crontab Library



( ZN >= 3.1.0 )

The Crontab library is a library built to manage scheduled tasks on linux operating systems. With this library, you can get the jobs done at specified times. 

 

# Requirements


1 - The operating systemmust be Linux .
2 - Crontab must be installed in the operating system.

 

# Configuration


Below  is the sequence used to configure crontab settings.

File: Config / Services.php
Processor
String $ driver = 'exec' Specifies the method used to run the command.
Options exec , shell , system , ssh
String $ path'/ usr / bin / php' It is the path of PHP to be executed.

 

# Methods


this command ( String $ command )
this controller ( String $ controller )
this wget ( String $ url )
String list ( Void )
Bool remove ( Scalar $ id )
void limit ( Int $ id , Int $ limit = 1 )
void queue ( Int $ id , Callable $ callable , Int $ decrement = 1 )
this path ( String $ path )
this interval ( String $ interval )

 

# Time Methods


The following list shows which time adjustments can be made by the time method.

Method Parameter Meaning
hourly () void Every hour.
daily () void Every day.
midnight () void Every night is one.
Monthly () void Every month.
weekly () void Every week.
yearly () void Every year.
clock () 12:30 24:48 20:00 One at the specified time.
minute () 5 5-10 53,55,57 At the specified minute.
perminute () 30 Every specified minute.
hour () 23 7-13 7-1311 At specified times.
Perho is () 2nd Every one hour.
day () monday sunday saturday On specified days.
perday () friday Every one on specified days.
daynumb is () 30 10 15 Every day on a particular day.

 

# Time Formation


+ ---------------- -> minute ()
| + ------------- -> hour ()
| | + ---------- -> dayNumber ()
| | | + ------- -> month ()
| | | | + ---- -> day ()
| | | | |
* * * * * [ -> path () ] [ -> controller () / command () ]

 

# Use with Console ( ZN> = 5.1.0 )


On Linux systems you can create scheduled tasks using the terminal. Below is an example of running a code for a controller and a command library.

php zerocore run-cron home/sendmail minute /1
php zerocore run-cron ExampleCommand:sendMail daily
php zerocore run-cron http://example.site/example/page
php zerocore cron-list
php zerocore remove-cron 1

 

# Command ( ZN> = 5.1.0 )


ZN Framework Commands Used to execute custom commands that you type in the / directory. 

Parameters

String $ commander Class and method information to run. 
return Mixed

Use of

Crontab::day('monday')->clock('10:30')->command('EmailCommand:send');
Crontab::daily()->command('EmailCommand:send');

 

# Controller ( ZN >= 5.1.0 )


It is used to run ZN Framework controllers and its methods.

Parameters

String $ Controller Controller and method information to run. This parameter contains information in the controller / function form. The controller name is written according to the file name.
return Mixed

Use of

Crontab::day('monday')->clock('10:30')->controller('Accounts/sendEmail');
Crontab::daily()->controller('Accounts/sendEmail');

 

# Wget ( ZN >= 5.1.0 )


Used to run URL.

Parameters

String $ url The URL to run.
return Mixed

Use of

Crontab::day('monday')->clock('10:30')->wget('http://site.com/example/page');

 

# Path ( ZN >= 3 1.0 )


The way to run Crontab is PHP. Predefined: / Usr / bin / php

Parameters

String $ path PHP Road.
return this

Use of

Crontab::month(1)
       ->day(1)
       ->clock('12:30')
       ->path('/opt/lampp/bin/php')
       ->controller('home/test');

 

# List ( ZN >= 5.1.0 )


Gives a list of scheduled tasks.

Parameters

void
return String

Use of

echo Crontab::list();

 

# Remove ( ZN> = 5.1.0 )


Used to delete the scheduled task.

Parameters

scaler $ id = NULL With the crontab :: list () method, you can delete the cron jobs listed by their id value.
return void

Use of

Crontab::remove(1);

To delete all scheduled tasks;

Crontab::remove();

 

# Limit ( ZN >= 5.3.6 )


Used to specify the number of times the scheduled task will run. This method is used in the script.

Parameters

scaler $ id = NULL Cron ID. Which ID's task will be limited.
Int $ limit = 1 How many times the command will be executed.
return void

Use of

Script: Commands / CreateFileCommand.php
<?php namespace Project\Commands;

use File, Crontab;

class CreateFileCommand extends Command
{
    public function do()
    {
        File::create(md5(rand()));

        Crontab::limit(1, 2);
    }
}

Above script below 1 is located in the console command to run a day.

php zerocore run-cron CreateFileCommand:do perday 1

After the command has been run 2 times, the scheduled task is terminated.

 

# Queue ( ZN >= 5.3.6 )


The scheduled task is used to determine whether to continue or terminate according to certain conditions. This method is used in the script. Especially useful for mass mailing.

Parameters

scaler $ id = NULL Cron ID. Which ID's task will be limited.
callable $ Callable The function to be executed. There are 2 parameters.$ queındex and $ decrement queueındex 0 'and returns the last index on which the tail is based, depending on the amount of increase.
Int $ decrement = The increase will run away.
return void

Use of

Script: Commands / Example.php
<?php namespace Project\Commands;

use File, Crontab;

class Example extends Command
{
    public function do()
    {
        Crontab::queue(1, function($queueIndex, $decrement)
        {
            File::create($queueIndex);
            
            if( $queueIndex === 2 )
            {
                return false;
            }
        });
    }
}

Below is the console command to run the above script every 1 minute.

php zerocore run-cron Example:do perminute 1

After executing the command , 3 files named 0 , 1 and 2 will be created and the task will be completed.

Capture the Last Stake

The last value your tail has left. Written to parameter 2function ( $ queIndex ) {...}is caught with the variable to be assigned to parameter 1 of the function .

Capture Increase Amount

Queue () method to post 3. To capture parameters 2. parameters typedfunction ( $ queIndex , $ decrement ) {...}is caught with the variable to be assigned to the 2nd parameter of the function .

Task Termination

The function to terminate the Task false to return should be.

E-mail Sending Example

Below is a simple example of sending a bulk email.

Script: Commands / SendEmailAllCommand.php
<?php namespace Project\Commands;

use Email, DB, Crontab;

class SendEmailAllCommand extends Command
{
    public function do()
    {
        Crontab::queue(1, function($queueIndex, $decrement)
        {
            $result = DB::limit($queueIndex, $decrement)->accountsResult();

            foreach( $result as $row )
            {
                Email::bcc($row->email, $row->name);
            }
           
            return Email::from('SiteCom')->send('Subject', 'Message');
        }, 35);
    }
}

Below is the console command to run the above script every 1 minute.

php zerocore run-cron SendEmailAllCommand:do perminute 1

Now our script sends bulk email with 35 email addresses every minute. Email :: send () method false continue till their email submissions. If the command is requested to continue for subsequent emails in case of incorrect email chances$ resultvariable return ( bool ) count ( $ result ) ; .