Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/Cron_Command.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use EE\Model\Cron;
use function EE\Site\Utils\auto_site_name;

/**
* Manages cron on easyengine.
*
* @package ee-cli
*/

class Cron_Command extends EE_Command {

/**
Expand Down Expand Up @@ -40,11 +40,11 @@ public function __construct() {
*
* Entry | Description | Equivalent To
* ----- | ----------- | -------------
* @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
* @monthly | Run once a month, midnight, first of month | 0 0 1 * *
* @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
* @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
* @hourly | Run once an hour, beginning of hour | 0 * * * *
* @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 *
* @monthly | Run once a month, midnight, first of month | 0 0 1 * *
* @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0
* @daily (or @midnight) | Run once a day, midnight | 0 0 * * *
* @hourly | Run once an hour, beginning of hour | 0 * * * *
*
* You may also schedule a job to execute at fixed intervals, starting at the time it's added or cron is run.
* This is supported by following format:
Expand All @@ -71,13 +71,13 @@ public function __construct() {
*
* # Adds a cron job to host running EasyEngine
* $ ee cron add host --command='wp media regenerate --yes' --schedule='@weekly'
*
*/
public function add( $args, $assoc_args ) {

EE\Utils\delem_log( 'ee cron add start' );

if ( ! isset( $args[0] ) || $args[0] !== 'host' ) {
$args = EE\SiteUtils\auto_site_name( $args, 'cron', __FUNCTION__ );
$args = auto_site_name( $args, 'cron', __FUNCTION__ );
}

$site = EE\Utils\remove_trailing_slash( $args[0] );
Expand All @@ -94,11 +94,11 @@ public function add( $args, $assoc_args ) {
$this->validate_command( $command );
$command = $this->add_sh_c_wrapper( $command );

Cron::create([
Cron::create( [
'site_url' => $site,
'command' => $command,
'schedule' => $schedule
]);
] );


$this->update_cron_config();
Expand Down Expand Up @@ -155,9 +155,9 @@ public function add( $args, $assoc_args ) {
*
* # Updates schedule of cron
* $ ee cron update 1 --schedule='@every 1m'
*
*/
public function update( $args, $assoc_args ) {

EE\Utils\delem_log( 'ee cron add start' );

$data_to_update = [];
Expand Down Expand Up @@ -216,17 +216,16 @@ public function update( $args, $assoc_args ) {
* @subcommand list
*/
public function _list( $args, $assoc_args ) {
$where = [];
$all = EE\Utils\get_flag_value( $assoc_args, 'all' );

$all = EE\Utils\get_flag_value( $assoc_args, 'all' );

if ( ( ! isset( $args[0] ) || $args[0] !== 'host' ) && ! $all ) {
$args = EE\SiteUtils\auto_site_name( $args, 'cron', 'list' );
$args = auto_site_name( $args, 'cron', 'list' );
}

if ( isset( $args[0] ) ) {
$crons = Cron::where( 'site_url', $args[0] );
}
else {
} else {
$crons = Cron::all();
}

Expand All @@ -245,7 +244,6 @@ public function _list( $args, $assoc_args ) {
private function update_cron_config() {

$config = $this->generate_cron_config();

file_put_contents( EE_CONF_ROOT . '/cron/config.ini', $config );
EE_DOCKER::restart_container( 'ee-cron-scheduler' );
}
Expand All @@ -254,6 +252,7 @@ private function update_cron_config() {
* Generates and returns cron config from DB
*/
private function generate_cron_config() {

$config_template = file_get_contents( __DIR__ . '/../templates/config.ini.mustache' );
$crons = Cron::all();

Expand Down Expand Up @@ -317,7 +316,7 @@ public function run_now( $args ) {
*/
public function delete( $args ) {

$id = $args[0];
$id = $args[0];
$cron = Cron::find( $id );

if ( ! $cron ) {
Expand All @@ -332,18 +331,26 @@ public function delete( $args ) {


/**
* Returns php container name of a site
* Returns php container name of a site.
*
* @param string $site Name of the site whose container name is needed.
*
* @return string Container name.
*/
private function site_php_container( $site ) {

return str_replace( '.', '', $site ) . '_php_1';
}

/**
* Ensures given command will not create problem with INI syntax
* Ensures given command will not create problem with INI syntax.
* Semicolons and Hash(#) in commands do not work for now due to limitation of INI style config ofelia uses.
* See https://github.com/EasyEngine/cron-command/issues/4.
*
* @param string $command Command whose syntax needs to be validated.
*/
private function validate_command( $command ) {
// Semicolons and Hash(#) in commands do not work for now due to limitation of INI style config ofelia uses
// See https://github.com/EasyEngine/cron-command/issues/4

if ( strpos( $command, ';' ) !== false ) {
EE::error( 'Command chaining using `;` - semi-colon is not supported currently. You can either use `&&` or `||` or creating a second cron job for the chained command.' );
}
Expand All @@ -352,6 +359,13 @@ private function validate_command( $command ) {
}
}

/**
* Adds wrapper of `sh -c` to execute composite commands through docker exec properly.
*
* @param string $command Passed command.
*
* @return string Command with properly added wrapper.
*/
private function add_sh_c_wrapper( $command ) {
if ( strpos( $command, 'sh -c' ) !== false ) {
return $command;
Expand Down