I already made a Beat Mechanism for Yii; quite tight to that project which works pretty fine.

Right now I'm working on a beat mechanism totally decoupled from any project. A Module, maybe. Right now works but not in a very elegant way.

Basic Structure

requires yiisoft/yii2-queue, yiisoft/mutex

beco\\yii-beat\\BeatController

extends yii\\console\\Controller

beat/start - queues a beat/run process to run with 0 delay (if there is no BeatJob instance queued ) beat/run [register next run to queue, default 0] - immediately runs a BeatJob

beat/index - shows beat info

beco\\yii-beat\\jobs\\BeatJob

implements: yii\\queue\\JobInterface

Has two functions:

  1. looks for all Models which implement BeatInterface and invoke such a static function
  2. queues a beat with a 60 second delay

beco\\yii-beat\\interfaces\\BeatInterface

interface BeatInterface {
	public static function executeBeat():void;
}

Models

If a model implements BeatInterface, it will be asked to execute the static executeBeat function, such function has the responsibility to run or pass (according to time, business rules or whatever), furthermore this static function can (and most likely will) delegate one invocation into other static functions in the same Model

My code is here, I warn you it is not as elegant as I would like to, but would really appreciate any suggestions.

Example

Measuring rooms’ temperature every eight hours. Using my own ActiveRecord.