Skip to content

Commit a43b624

Browse files
Merge pull request #13 from kirtangajjar/refactor-db
Fix cron command according to refactored DB class
2 parents 8bc1437 + 24ad705 commit a43b624

1 file changed

Lines changed: 31 additions & 25 deletions

File tree

src/Cron_Command.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use EE\Model\Cron;
4+
35
/**
46
* Manages cron on easyengine.
57
*
@@ -92,13 +94,12 @@ public function add( $args, $assoc_args ) {
9294
$this->validate_command( $command );
9395
$command = $this->add_sh_c_wrapper( $command );
9496

95-
EE::db()->insert(
96-
[
97-
'sitename' => $site,
98-
'command' => $command,
99-
'schedule' => $schedule
100-
], 'cron'
101-
);
97+
Cron::create([
98+
'site_url' => $site,
99+
'command' => $command,
100+
'schedule' => $schedule
101+
]);
102+
102103

103104
$this->update_cron_config();
104105

@@ -163,12 +164,13 @@ public function update( $args, $assoc_args ) {
163164
$site = EE\Utils\get_flag_value( $assoc_args, 'site' );
164165
$command = EE\Utils\get_flag_value( $assoc_args, 'command' );
165166
$schedule = EE\Utils\get_flag_value( $assoc_args, 'schedule' );
167+
$cron_id = $args[0];
166168

167169
if ( ! $site && ! $command && ! $schedule ) {
168170
EE::error( 'You should specify atleast one of - site, command or schedule to update' );
169171
}
170172
if ( $site ) {
171-
$data_to_update['sitename'] = $site;
173+
$data_to_update['site_url'] = $site;
172174
}
173175
if ( $command ) {
174176
$this->validate_command( $command );
@@ -185,8 +187,7 @@ public function update( $args, $assoc_args ) {
185187
$data_to_update['schedule'] = $schedule;
186188
}
187189

188-
189-
EE::db()->update( $data_to_update, [ 'id' => $args[0] ], 'cron' );
190+
Cron::find( $cron_id )->update( $data_to_update );
190191

191192
$this->update_cron_config();
192193

@@ -223,16 +224,18 @@ public function _list( $args, $assoc_args ) {
223224
}
224225

225226
if ( isset( $args[0] ) ) {
226-
$where = [ 'sitename' => $args[0] ];
227+
$crons = Cron::where( 'site_url', $args[0] );
228+
}
229+
else {
230+
$crons = Cron::all();
227231
}
228232

229-
$crons = EE::db()->select( [], $where, 'cron' );
230233

231-
if ( false === $crons ) {
234+
if ( empty( $crons ) ) {
232235
EE::error( 'No cron jobs found.' );
233236
}
234237

235-
EE\Utils\format_items( 'table', $crons, [ 'id', 'sitename', 'command', 'schedule' ] );
238+
EE\Utils\format_items( 'table', $crons, [ 'id', 'site_url', 'command', 'schedule' ] );
236239
}
237240

238241

@@ -252,17 +255,17 @@ private function update_cron_config() {
252255
*/
253256
private function generate_cron_config() {
254257
$config_template = file_get_contents( __DIR__ . '/../templates/config.ini.mustache' );
255-
$crons = EE::db()->select( [], [], 'cron' );
256-
$crons = $crons === false ? [] : $crons;
258+
$crons = Cron::all();
259+
257260
foreach ( $crons as &$cron ) {
258-
$job_type = $cron['sitename'] === 'host' ? 'job-local' : 'job-exec';
259-
$id = $cron['sitename'] . '-' . preg_replace( '/[^a-zA-Z0-9\@]/', '-', $cron['command'] ) . '-' . EE\Utils\random_password( 5 );
261+
$job_type = $cron['site_url'] === 'host' ? 'job-local' : 'job-exec';
262+
$id = $cron['site_url'] . '-' . preg_replace( '/[^a-zA-Z0-9\@]/', '-', $cron['command'] ) . '-' . EE\Utils\random_password( 5 );
260263
$id = preg_replace( '/--+/', '-', $id );
261264
$cron['job_type'] = $job_type;
262265
$cron['id'] = $id;
263266

264-
if ( $cron['sitename'] !== 'host' ) {
265-
$cron['container'] = $this->site_php_container( $cron['sitename'] );
267+
if ( $cron['site_url'] !== 'host' ) {
268+
$cron['container'] = $this->site_php_container( $cron['site_url'] );
266269
}
267270
}
268271

@@ -287,12 +290,14 @@ private function generate_cron_config() {
287290
* @subcommand run-now
288291
*/
289292
public function run_now( $args ) {
290-
$result = EE::db()->select( [ 'sitename', 'command' ], [ 'id' => $args[0] ], 'cron' );
293+
294+
$result = Cron::find( $args[0] );
295+
291296
if ( empty( $result ) ) {
292297
EE::error( 'No such cron with id ' . $args[0] );
293298
}
294-
$container = $this->site_php_container( $result[0]['sitename'] );
295-
$command = $result[0]['command'];
299+
$container = $this->site_php_container( $result['site_url'] );
300+
$command = $result['command'];
296301
EE::exec( "docker exec $container $command", true, true );
297302
}
298303

@@ -313,12 +318,13 @@ public function run_now( $args ) {
313318
public function delete( $args ) {
314319

315320
$id = $args[0];
321+
$cron = Cron::find( $id );
316322

317-
if ( ! EE::db()->select( [ 'id' ], [ 'id' => $id ], 'cron' ) ) {
323+
if ( ! $cron ) {
318324
EE::error( 'Unable to find cron with id ' . $id );
319325
}
320326

321-
EE::db()->delete( [ 'id' => $id ], 'cron' );
327+
$cron->delete();
322328
$this->update_cron_config();
323329

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

0 commit comments

Comments
 (0)