Skip to content

Add cron as a global service #50

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 3 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
@@ -0,0 +1,108 @@
<?php

namespace EE\Migration;

use EE;
use EE\Migration\Base;
use http\Exception;

class AddCronGlobalService extends Base {

/** @var RevertableStepProcessor $rsp Keeps track of migration state. Reverts on error */
private static $rsp;

public function __construct() {

parent::__construct();
if ( $this->is_first_execution ) {
$this->skip_this_migration = true;
}
}

/**
* Execute global service container name update.
*
* @throws EE\ExitException
*/
public function up() {

if ( $this->skip_this_migration ) {
EE::debug( 'Skipping add-cron-global-service migration as it is not needed.' );

return;
}

EE::debug( 'Starting add-cron-global-service' );
self::$rsp = new EE\RevertableStepProcessor();

$global_compose_file_path = EE_ROOT_DIR . '/services/docker-compose.yml';
$global_compose_file_backup_path = EE_BACKUP_DIR . '/services/docker-compose.yml.backup';

$old_container = 'running' === \EE_DOCKER::container_status( 'ee-cron-scheduler' );

/**
* Backup old docker-compose file.
*/
self::$rsp->add_step(
'backup-global-docker-compose-file',
'EE\Migration\SiteContainers::backup_restore',
'EE\Migration\SiteContainers::backup_restore',
[ $global_compose_file_path, $global_compose_file_backup_path ],
[ $global_compose_file_backup_path, $global_compose_file_path ]
);

/**
* Generate new docker-compose file.
*/
self::$rsp->add_step(
'generate-global-docker-compose-file',
'EE\Service\Utils\generate_global_docker_compose_yml',
null,
[ new \Symfony\Component\Filesystem\Filesystem() ],
null
);

if ( $old_container ) {
/**
* Start global-cron service container.
*/
self::$rsp->add_step(
'enable-global-cron-service',
'EE\Migration\GlobalContainers::global_service_up',
null,
[ EE_CRON_SERVICE ],
[]
);

/**
* Remove ee-cron-scheduler container.
*/
self::$rsp->add_step(
'remove-ee-cron-scheduler-container',
'EE\Migration\AddCronGlobalService::remove_old_cron_container',
null,
[],
[]
);
}

if ( ! self::$rsp->execute() ) {
throw new \Exception( 'Unable run add-cron-global-service migrations.' );
}
}

/**
* No need for down.
*
* @throws EE\ExitException
*/
public function down() {
}

public static function remove_old_cron_container() {

if ( ! EE::exec( 'docker rm -f ee-cron-scheduler' ) ) {
throw new \Exception( 'Unable to remove ee-cron-scheduler container' );
}
}
}
9 changes: 9 additions & 0 deletions src/helper/service-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ function generate_global_docker_compose_yml( Filesystem $fs ) {
'global-backend-network',
],
],
[
'name' => EE_CRON_SERVICE,
'image' => 'easyengine/cron:' . $img_versions['easyengine/cron'],
'restart' => 'always',
'volumes' => [
'/opt/easyengine/services/cron:/etc/ofelia:ro',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use function: EE_DOCKER::get_mounting_volume_array() as in all of above to generate volumes. As the volume generation differs in Linux and macOS.

For the volume /var/run/docker.sock - You can use skip_volume:

[
'name' => '/var/run/docker.sock',
'path_to_symlink' => '/var/run/docker.sock',
'container_path' => '/tmp/docker.sock:ro',
'skip_volume' => true,
],

Once, this change is done, for Linux systems, the cron directory inside the EE_SERVICE_DIR will be a docker volume. So, you'll have to write migrations to take backup of the previous cron directory and then once volume directory get's symlinked, need to restore that data.

'/var/run/docker.sock:/var/run/docker.sock:ro',
],
],
];

Option::set( GLOBAL_DB, $password );
Expand Down