Skip to content
This repository was archived by the owner on Jan 18, 2024. It is now read-only.

Installation & Setup

Miguel Muscat edited this page Feb 13, 2019 · 3 revisions

Requirements

  • PHP >= 5.4.0
  • WordPress >= 4.7

1. Install

Option A - Using Composer: (Recommended)

composer require rebelcode/cronarchy

Option B - Manually: (Not recommended)

  1. Download a release version of this repository from the Releases page.
  2. Extract the cronarchy directory into your project.
  3. In your project, require_once the files in the cronarchy/src/ directory.
  4. Give up and use Composer πŸ˜…

2. Set up

In the coming sections, we will be assuming the below file structure. You can use whatever file structure you wish, so long as you make sure to use the correct paths when setting things up.

my-project/
β”œβ”€β”€ cron/
β”‚   β”œβ”€β”€ daemon.php
β”‚   └── config
└── my-project.php

The daemon.php script

You will need to set up the entry point for the daemon yourself. This will be the PHP file that Cronarchy will ping directly through an HTTP(S) request.

This script should set up a Daemon instance and run it, as demonstrated below:

use RebelCode\Cronarchy\Daemon;

require __DIR__ . '/../vendor/autoload.php';

$daemon = new Daemon('example', __DIR__ . '/config');
$daemon->run();

The arguments passed to the Daemon class are:

  1. the ID of the Cronarchy instance to use
  2. the path to the config file

In this example, we're naming our instance "example" and telling the daemon to load its config from a cron-config file from the same directory (see next step).

The Cronarchy instance

The term "the client" in this section refers to the code in your plugin or theme that is being run normally within WordPress. The client must do three things:

  • create a Cronarchy instance
  • hook into the Cronarchy instance filter and return this instance for the correct ID
  • call the Runner, which in turn might ping the daemon script from step 2

The easiest way to achieve all three is by using the Cronarchy::setup() static method.

$daemonUrl = plugin_dir_url(__FILE__) . '/cron/daemon.php';
$configFile = __DIR__ . '/cron/config';

$cronarchy = Cronarchy::setup('example', $daemonUrl, $configFile);

⚠️ Important: do not call the setup() method from inside a hook! Call it directly from within your plugin's file-level scope or theme's functions.php file.

The arguments passed to setup() are:

  1. A unique ID for this instance, which should match the ID given in step 2
  2. The absolute URL to the daemon script created in step 2
  3. The path to the daemon config file, which must point to the same file as in step 2

In the above demonstration, we set up our client to use our "example" instance, as set up in step 2. We're also assuming that the above code is running from a plugin's main file since we're using plugin_dir_url(__FILE__) to obtain the URL to the script.

The return value of Cronarchy::setup() will be your plugin or theme's own Cronarchy instance, automatically initialized and hooked into WordPress. Keep this instance stored somewhere handly to be able to schedule and manage your cron jobs.

That's it! πŸŽ‰

You're done. Cronarchy will take care of the rest for you, including auto-creating the config file if it does not exist. Your theme/plugin may now start scheduling cron jobs to Cronarchy. Check out the API Documentation to learn how.

Clone this wiki locally