Skip to content

Fix code structure #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use EE;
use EE\Migration\Base;
use EE\Model\Cron;
use EE_DOCKER;
use EE\Cron\Utils as Cron_Utils;

class NoOverlapMigration extends Base {

Expand Down Expand Up @@ -51,7 +53,7 @@ public function up() {
}
}
// Fix no-overlap.
EE\Cron\Utils\update_cron_config();
Cron_Utils\update_cron_config();
}

/**
Expand All @@ -62,6 +64,6 @@ public function up() {
public function down() {

$this->fs->copy( $this->backup_file, $this->source_file );
\EE_DOCKER::restart_container( EE_CRON_SCHEDULER );
EE_DOCKER::restart_container( EE_CRON_SCHEDULER );
}
}
50 changes: 26 additions & 24 deletions src/Cron_Command.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use EE\Model\Cron;
use function EE\Site\Utils\auto_site_name;
use EE\Site\Utils as Site_Utils;
use EE\Utils as EE_Utils;
use EE\Cron\Utils as Cron_Utils;

/**
* Manages cron on easyengine sites and host machine.
Expand Down Expand Up @@ -69,26 +71,26 @@ class Cron_Command extends EE_Command {
public function create( $args, $assoc_args ) {

if ( 'running' !== EE_DOCKER::container_status( EE_CRON_SCHEDULER ) ) {
$img_versions = \EE\Utils\get_image_versions();
$img_versions = EE_Utils\get_image_versions();
$cron_scheduler_run_command = 'docker run --name ' . EE_CRON_SCHEDULER . ' --restart=always -d -v ' . EE_ROOT_DIR . '/services/cron:/etc/ofelia:ro -v /var/run/docker.sock:/var/run/docker.sock:ro easyengine/cron:' . $img_versions['easyengine/cron'];
if ( ! EE_DOCKER::boot_container( EE_CRON_SCHEDULER, $cron_scheduler_run_command ) ) {
EE::error( 'There was some error in starting ' . EE_CRON_SCHEDULER . ' container. Please check logs.' );
}
}

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

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

$site = EE\Utils\remove_trailing_slash( $args[0] );
$command = EE\Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE\Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE\Utils\get_flag_value( $assoc_args, 'user' );
$site = EE_Utils\remove_trailing_slash( $args[0] );
$command = EE_Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE_Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE_Utils\get_flag_value( $assoc_args, 'user' );

if ( 'host' !== $args[0] ) {
$site_info = \EE\Site\Utils\get_site_info( $args );
$site_info = Site_Utils\get_site_info( $args );
if ( ! EE_DOCKER::service_exists( 'php', $site_info['site_fs_path'] ) ) {
EE::error( $site . ' does not have PHP container.' );
}
Expand Down Expand Up @@ -120,10 +122,10 @@ public function create( $args, $assoc_args ) {

Cron::create( $cron_data );

EE\Cron\Utils\update_cron_config();
Cron_Utils\update_cron_config();

EE::success( 'Cron created successfully' );
EE\Utils\delem_log( 'ee cron add end' );
EE_Utils\delem_log( 'ee cron add end' );
}

/**
Expand All @@ -133,7 +135,7 @@ public function create( $args, $assoc_args ) {
*
* @param string $command Command whose syntax needs to be validated.
*
* @throws \EE\ExitException
* @throws EE\ExitException
*/
private function validate_command( $command ) {

Expand Down Expand Up @@ -218,13 +220,13 @@ private function add_sh_c_wrapper( $command ) {
*/
public function update( $args, $assoc_args ) {

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

$data_to_update = [];
$site = EE\Utils\get_flag_value( $assoc_args, 'site' );
$command = EE\Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE\Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE\Utils\get_flag_value( $assoc_args, 'user' );
$site = EE_Utils\get_flag_value( $assoc_args, 'site' );
$command = EE_Utils\get_flag_value( $assoc_args, 'command' );
$schedule = EE_Utils\get_flag_value( $assoc_args, 'schedule' );
$user = EE_Utils\get_flag_value( $assoc_args, 'user' );
$cron_id = $args[0];

if ( ! $site && ! $command && ! $schedule && ! $user ) {
Expand Down Expand Up @@ -253,11 +255,11 @@ public function update( $args, $assoc_args ) {

Cron::update( [ 'id' => $cron_id ], $data_to_update );

EE\Cron\Utils\update_cron_config();
Cron_Utils\update_cron_config();

EE::success( 'Cron update Successfully' );

EE\Utils\delem_log( 'ee cron add end' );
EE_Utils\delem_log( 'ee cron add end' );
}

/**
Expand All @@ -283,10 +285,10 @@ public function update( $args, $assoc_args ) {
*/
public function _list( $args, $assoc_args ) {

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

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

if ( isset( $args[0] ) ) {
Expand All @@ -300,7 +302,7 @@ public function _list( $args, $assoc_args ) {
EE::error( 'No cron jobs found.' );
}

EE\Utils\format_items( 'table', $crons, [ 'id', 'site_url', 'command', 'schedule' ] );
EE_Utils\format_items( 'table', $crons, [ 'id', 'site_url', 'command', 'schedule' ] );
}

/**
Expand All @@ -326,7 +328,7 @@ public function run_now( $args ) {
EE::error( 'No such cron with id ' . $args[0] );
}

$container = EE\Cron\Utils\site_php_container( $cron->site_url );
$container = Cron_Utils\site_php_container( $cron->site_url );
$command = $cron->command;
$user = empty( $cron->user ) ? 'root' : $cron->user;

Expand Down Expand Up @@ -363,7 +365,7 @@ public function delete( $args ) {
}

$cron->delete();
EE\Cron\Utils\update_cron_config();
Cron_Utils\update_cron_config();

EE::success( 'Deleted cron with id ' . $id );

Expand Down
9 changes: 6 additions & 3 deletions src/helper/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use EE;
use EE\Model\Cron;
use EE_DOCKER;
use EE\Utils as EE_Utils;
use Mustache_Engine;

/**
* Generates cron config from DB
Expand All @@ -12,7 +15,7 @@ function update_cron_config() {

$config = generate_cron_config();
file_put_contents( EE_ROOT_DIR . '/services/cron/config.ini', $config );
\EE_DOCKER::restart_container( EE_CRON_SCHEDULER );
EE_DOCKER::restart_container( EE_CRON_SCHEDULER );
}

/**
Expand All @@ -25,7 +28,7 @@ function generate_cron_config() {

foreach ( $crons as &$cron ) {
$job_type = 'host' === $cron->site_url ? 'job-local' : 'job-exec';
$id = $cron->site_url . '-' . preg_replace( '/[^a-zA-Z0-9\@]/', '-', $cron->command ) . '-' . EE\Utils\random_password( 5 );
$id = $cron->site_url . '-' . preg_replace( '/[^a-zA-Z0-9\@]/', '-', $cron->command ) . '-' . EE_Utils\random_password( 5 );
$id = preg_replace( '/--+/', '-', $id );
$cron->job_type = $job_type;
$cron->id = $id;
Expand All @@ -35,7 +38,7 @@ function generate_cron_config() {
}
}

$me = new \Mustache_Engine();
$me = new Mustache_Engine();

return $me->render( $config_template, $crons );
}
Expand Down