PHPackages                             incapption/load-balanced-cron-task - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. incapption/load-balanced-cron-task

ActiveLibrary[Queues &amp; Workers](/categories/queues)

incapption/load-balanced-cron-task
==================================

Distributes cron jobs among identical webservers based on a single remote mysql database.

2.1.0(1y ago)15211MITPHPPHP &gt;=7.2

Since Feb 9Pushed 1y agoCompare

[ Source](https://github.com/incapption-public/LoadBalancedCronTask)[ Packagist](https://packagist.org/packages/incapption/load-balanced-cron-task)[ RSS](/packages/incapption-load-balanced-cron-task/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (8)Used By (0)

Load Balanced Cron Task
=======================

[](#load-balanced-cron-task)

Introduction
------------

[](#introduction)

This is a lightweight package for load balancing your cron tasks. It is used to distribute tasks across a network of **identical servers** to make sure they are run only once and not multiple times.

> It is important that your application network uses only one single central mysql database. A table is created in the database in which current tasks are inserted. A primary key ensures that the task only runs once (a kind of locking process).
>
> the ***unique\_hash*** is a md5 hash of the task name, the schedule and the current timestamp ***without*** seconds. With this, the nodes can run asynchronously but the task runs only once at a time.

---

### Scheme of operation

[](#scheme-of-operation)

[![alt text](scheme.png)](scheme.png)

Requirements
------------

[](#requirements)

- MySQL Database
- PHP PDO Extension (ext-pdo, ext-pdo\_mysql)

1. Installation
---------------

[](#1-installation)

### 1.1 Composer

[](#11-composer)

Install `Load Balanced Cron Task` using Composer.

```
$ composer require incapption/load-balanced-cron-task
```

### 1.2 Create MySQL Table

[](#12-create-mysql-table)

This package needs a small table in your database. It's used for locking running tasks, to make sure that only on app instance could run it.

```
CREATE TABLE IF NOT EXISTS `lbct_tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`unique_hash` varchar(32) NOT NULL,
`task` varchar(256) NOT NULL,
`schedule` varchar(256) NOT NULL,
`date_created` datetime NOT NULL,
`worker` varchar(256),
PRIMARY KEY (`unique_hash`),
KEY `id` (`id`)
);
```

### 1.3 Install Crontab

[](#13-install-crontab)

The idea is that you have only one single cron task running every minute. This cron task contains all other tasks wrapped by **LoadBalancedCronTask**

Open your crontab console (e.g. linux: `crontab -e`)

```
* * * * /usr/bin/php /your/local/path/cron.php
```

### 1.4 Create your cron file

[](#14-create-your-cron-file)

Maybe you want to integrate into your application, or you create a new plain file.

```
