2016-06-04 01:10:45 +02:00
< ? php
2014-01-06 05:15:47 +01:00
App :: uses ( 'Folder' , 'Utility' );
App :: uses ( 'File' , 'Utility' );
require_once 'AppShell.php' ;
class ServerShell extends AppShell
{
2016-03-09 02:31:55 +01:00
public $uses = array ( 'Server' , 'Task' , 'Job' , 'User' , 'Feed' );
2016-06-04 01:08:16 +02:00
2014-01-06 05:15:47 +01:00
public function pull () {
2018-04-16 07:56:17 +02:00
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ])) {
2018-12-13 10:30:04 +01:00
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'console_automation_tasks' ][ 'data' ][ 'pull' ] . PHP_EOL );
2018-04-16 07:56:17 +02:00
}
2014-01-06 05:15:47 +01:00
$userId = $this -> args [ 0 ];
2018-04-14 23:37:07 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2018-04-16 07:56:17 +02:00
if ( empty ( $this -> args [ 1 ])) die ();
2014-01-06 05:15:47 +01:00
$serverId = $this -> args [ 1 ];
2018-04-14 23:37:07 +02:00
if ( ! empty ( $this -> args [ 2 ])) {
$technique = $this -> args [ 2 ];
} else {
2018-04-16 07:56:17 +02:00
$technique = 'full' ;
2018-04-14 23:37:07 +02:00
}
if ( ! empty ( $this -> args [ 3 ])) {
$jobId = $this -> args [ 3 ];
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'pull' ,
'job_input' => 'Server: ' . $serverId ,
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'message' => 'Pulling.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
2014-01-06 05:15:47 +01:00
$this -> Server -> id = $serverId ;
$server = $this -> Server -> read ( null , $serverId );
2015-10-06 01:16:48 +02:00
$result = $this -> Server -> pull ( $user , $serverId , $technique , $server , $jobId );
2014-02-20 17:28:39 +01:00
$this -> Job -> id = $jobId ;
2014-01-07 11:08:21 +01:00
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => 'Job done.' ,
'progress' => 100 ,
'status' => 4
));
2018-08-22 17:00:13 +02:00
if ( is_array ( $result )) {
$message = sprintf ( __ ( 'Pull completed. %s events pulled, %s events could not be pulled, %s proposals pulled.' , count ( $result [ 0 ]), count ( $result [ 1 ]), count ( $result [ 2 ])));
2018-04-16 07:56:17 +02:00
} else {
2018-08-22 17:00:13 +02:00
$message = sprintf ( __ ( 'ERROR: %s' ), $result );
2014-01-06 05:15:47 +01:00
}
2018-08-22 17:00:13 +02:00
$this -> Job -> saveField ( 'message' , $message );
echo $message . PHP_EOL ;
2014-01-06 05:15:47 +01:00
}
2016-06-04 01:08:16 +02:00
2014-01-06 05:15:47 +01:00
public function push () {
2018-04-16 07:56:17 +02:00
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ])) {
2018-12-13 10:30:04 +01:00
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'console_automation_tasks' ][ 'data' ][ 'push' ] . PHP_EOL );
2018-04-16 07:56:17 +02:00
}
$userId = $this -> args [ 0 ];
2018-04-15 22:40:17 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2018-04-16 07:56:17 +02:00
if ( empty ( $user )) die ( 'Invalid user.' . PHP_EOL );
$serverId = $this -> args [ 1 ];
2018-04-15 22:40:17 +02:00
if ( ! empty ( $this -> args [ 2 ])) {
$jobId = $this -> args [ 2 ];
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'push' ,
'job_input' => 'Server: ' . $serverId ,
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
2018-04-16 07:56:17 +02:00
'message' => 'Pushing.' ,
2018-04-15 22:40:17 +02:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
2014-01-06 05:15:47 +01:00
$this -> Job -> read ( null , $jobId );
2014-01-29 15:52:09 +01:00
$server = $this -> Server -> read ( null , $serverId );
2014-01-16 08:47:25 +01:00
App :: uses ( 'SyncTool' , 'Tools' );
$syncTool = new SyncTool ();
$HttpSocket = $syncTool -> setupHttpSocket ( $server );
2015-12-03 00:27:56 +01:00
$result = $this -> Server -> push ( $serverId , 'full' , $jobId , $HttpSocket , $user );
$message = 'Job done.' ;
2018-08-22 17:00:13 +02:00
if ( $result !== true && ! is_array ( $result )) $message = 'Job failed. Reason: ' . $result ;
2014-01-07 11:08:21 +01:00
$this -> Job -> save ( array (
'id' => $jobId ,
2016-03-09 02:31:55 +01:00
'message' => $message ,
2014-01-07 11:08:21 +01:00
'progress' => 100 ,
'status' => 4
));
2015-01-15 13:46:11 +01:00
if ( isset ( $this -> args [ 4 ])) {
$this -> Task -> id = $this -> args [ 5 ];
2018-04-15 22:40:17 +02:00
$message = 'Job(s) started at ' . date ( 'd/m/Y - H:i:s' ) . '.' ;
$this -> Task -> saveField ( 'message' , $message );
2018-04-16 07:56:17 +02:00
echo $message . PHP_EOL ;
2014-01-29 15:52:09 +01:00
}
}
2016-06-04 01:08:16 +02:00
2016-03-09 02:31:55 +01:00
public function fetchFeed () {
2018-04-16 07:56:17 +02:00
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ])) {
2018-12-13 10:30:04 +01:00
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'console_automation_tasks' ][ 'data' ][ 'fetchFeed' ] . PHP_EOL );
2018-04-16 07:56:17 +02:00
}
2016-03-09 02:31:55 +01:00
$userId = $this -> args [ 0 ];
2018-04-15 22:40:17 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2018-04-16 07:56:17 +02:00
if ( empty ( $user )) {
echo 'Invalid user.' ;
die ();
}
2016-03-09 02:31:55 +01:00
$feedId = $this -> args [ 1 ];
2018-04-15 22:40:17 +02:00
if ( ! empty ( $this -> args [ 2 ])) {
$jobId = $this -> args [ 2 ];
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'fetch_feeds' ,
'job_input' => 'Feed: ' . $feedId ,
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'message' => 'Starting fetch from Feed.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
2016-03-09 02:31:55 +01:00
$this -> Job -> read ( null , $jobId );
2018-11-08 13:10:27 +01:00
$outcome = array (
'id' => $jobId ,
'message' => 'Job done.' ,
'progress' => 100 ,
'status' => 4
);
if ( $feedId == 'all' ) {
$feedIds = $this -> Feed -> find ( 'list' , array (
'fields' => array ( 'Feed.id' , 'Feed.id' ),
'conditions' => array ( 'Feed.enabled' => 1 )
2017-05-08 14:22:27 +02:00
));
2018-11-08 13:10:27 +01:00
$feedIds = array_values ( $feedIds );
$successes = 0 ;
$fails = 0 ;
foreach ( $feedIds as $k => $feedId ) {
$jobStatus = array (
2017-05-08 14:22:27 +02:00
'id' => $jobId ,
2018-11-08 13:10:27 +01:00
'message' => 'Fetching feed: ' . $feedId ,
'progress' => 100 * $k / count ( $feedIds ),
'status' => 0
);
$this -> Job -> id = $jobId ;
$this -> Job -> save ( $jobStatus );
$result = $this -> Feed -> downloadFromFeedInitiator ( $feedId , $user );
if ( $result ) {
$successes ++ ;
} else {
$fails ++ ;
}
}
$outcome [ 'message' ] = 'Job done. ' . $successes . ' feeds pulled successfuly, ' . $fails . ' feeds could not be pulled.' ;
} else {
$temp = $this -> Feed -> find ( 'first' , array (
'fields' => array ( 'Feed.id' , 'Feed.id' ),
'conditions' => array ( 'Feed.enabled' => 1 , 'Feed.id' => $feedId )
2017-05-08 14:22:27 +02:00
));
2018-11-08 13:10:27 +01:00
if ( ! empty ( $temp )) {
$result = $this -> Feed -> downloadFromFeedInitiator ( $feedId , $user , $jobId );
if ( ! $result ) {
$outcome [ 'progress' ] = 0 ;
$outcome [ 'status' ] = 3 ;
$outcome [ 'message' ] = 'Job failed.' ;
}
}
2017-05-08 14:22:27 +02:00
}
2018-11-08 13:10:27 +01:00
$this -> Job -> id = $jobId ;
$this -> Job -> save ( $outcome );
echo $outcome [ 'message' ] . PHP_EOL ;
2017-05-08 14:22:27 +02:00
}
2019-01-18 09:06:23 +01:00
public function cacheServer () {
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ])) {
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'console_automation_tasks' ][ 'data' ][ 'cacheServer' ] . PHP_EOL );
}
$userId = $this -> args [ 0 ];
$user = $this -> User -> getAuthUser ( $userId );
if ( empty ( $user )) die ( 'Invalid user.' . PHP_EOL );
$scope = $this -> args [ 1 ];
if ( ! empty ( $this -> args [ 2 ])) {
$jobId = $this -> args [ 2 ];
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'cache_servers' ,
'job_input' => 'Server: ' . $id ,
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'message' => 'Starting server caching.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
$this -> Job -> read ( null , $jobId );
$result = $this -> Server -> cacheServerInitiator ( $user , $id , $jobId );
$this -> Job -> id = $jobId ;
if ( $result !== true ) {
$message = 'Job Failed. Reason: ' ;
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => $message . $result ,
'progress' => 0 ,
'status' => 3
));
} else {
$message = 'Job done.' ;
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => $message ,
'progress' => 100 ,
'status' => 4
));
}
echo $message . PHP_EOL ;
}
2018-04-16 07:56:17 +02:00
public function cacheFeed () {
if ( empty ( $this -> args [ 0 ]) || empty ( $this -> args [ 1 ])) {
2018-12-13 10:30:04 +01:00
die ( 'Usage: ' . $this -> Server -> command_line_functions [ 'console_automation_tasks' ][ 'data' ][ 'cacheFeed' ] . PHP_EOL );
2018-04-16 07:56:17 +02:00
}
2017-05-08 14:22:27 +02:00
$userId = $this -> args [ 0 ];
2018-04-15 22:40:17 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2018-04-16 07:56:17 +02:00
if ( empty ( $user )) die ( 'Invalid user.' . PHP_EOL );
$scope = $this -> args [ 1 ];
if ( ! empty ( $this -> args [ 2 ])) {
$jobId = $this -> args [ 2 ];
2018-04-15 22:40:17 +02:00
} else {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
2018-04-15 22:41:13 +02:00
'job_type' => 'cache_feeds' ,
2018-04-16 07:56:17 +02:00
'job_input' => 'Feed: ' . $scope ,
2018-04-15 22:40:17 +02:00
'status' => 0 ,
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
2018-04-15 22:41:13 +02:00
'message' => 'Starting feed caching.' ,
2018-04-15 22:40:17 +02:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
}
2017-05-08 14:22:27 +02:00
$this -> Job -> read ( null , $jobId );
$result = $this -> Feed -> cacheFeedInitiator ( $user , $jobId , $scope );
$this -> Job -> id = $jobId ;
if ( $result !== true ) {
$message = 'Job Failed. Reason: ' ;
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => $message . $result ,
'progress' => 0 ,
'status' => 3
));
2016-10-07 20:08:57 +02:00
} else {
$message = 'Job done.' ;
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => $message ,
'progress' => 100 ,
'status' => 4
));
}
2018-04-16 07:56:17 +02:00
echo $message . PHP_EOL ;
2016-03-09 02:31:55 +01:00
}
2016-06-04 01:08:16 +02:00
2014-01-29 15:52:09 +01:00
public function enqueuePull () {
$timestamp = $this -> args [ 0 ];
$userId = $this -> args [ 1 ];
$taskId = $this -> args [ 2 ];
$task = $this -> Task -> read ( null , $taskId );
if ( $timestamp != $task [ 'Task' ][ 'next_execution_time' ]) {
return ;
}
2016-12-07 13:50:45 +01:00
if ( $task [ 'Task' ][ 'timer' ] > 0 ) $this -> Task -> reQueue ( $task , 'default' , 'ServerShell' , 'enqueuePull' , $userId , $taskId );
2015-04-20 13:50:14 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2015-12-27 23:29:12 +01:00
$servers = $this -> Server -> find ( 'all' , array ( 'recursive' => - 1 , 'conditions' => array ( 'pull' => 1 )));
2014-01-29 15:52:09 +01:00
$count = count ( $servers );
2015-06-03 23:49:37 +02:00
$failCount = 0 ;
2014-01-29 15:52:09 +01:00
foreach ( $servers as $k => $server ) {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
2015-01-27 10:42:31 +01:00
'job_type' => 'pull' ,
2014-01-29 15:52:09 +01:00
'job_input' => 'Server: ' . $server [ 'Server' ][ 'id' ],
'retries' => 0 ,
2015-04-20 13:50:14 +02:00
'org' => $user [ 'Organisation' ][ 'name' ],
'org_id' => $user [ 'org_id' ],
2015-01-15 13:46:11 +01:00
'process_id' => 'Part of scheduled pull' ,
2015-01-27 10:42:31 +01:00
'message' => 'Pulling.' ,
2014-01-29 15:52:09 +01:00
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
App :: uses ( 'SyncTool' , 'Tools' );
$syncTool = new SyncTool ();
2015-12-26 01:35:34 +01:00
$result = $this -> Server -> pull ( $user , $server [ 'Server' ][ 'id' ], 'full' , $server , $jobId );
2014-01-29 15:52:09 +01:00
$this -> Job -> save ( array (
'id' => $jobId ,
'message' => 'Job done.' ,
'progress' => 100 ,
'status' => 4
));
if ( is_numeric ( $result [ 0 ])) {
switch ( $result [ 0 ]) {
case '1' :
$this -> Job -> saveField ( 'message' , 'Not authorised. This is either due to an invalid auth key, or due to the sync user not having authentication permissions enabled on the remote server.' );
break ;
case '2' :
$this -> Job -> saveField ( 'message' , $result [ 1 ]);
break ;
case '3' :
$this -> Job -> saveField ( 'message' , 'Sorry, incremental pushes are not yet implemented.' );
break ;
case '4' :
$this -> Job -> saveField ( 'message' , 'Invalid technique chosen.' );
break ;
2016-06-04 01:08:16 +02:00
2014-01-29 15:52:09 +01:00
}
2015-06-03 23:49:37 +02:00
$failCount ++ ;
2014-01-29 15:52:09 +01:00
}
}
2015-06-03 23:49:37 +02:00
$this -> Task -> id = $task [ 'Task' ][ 'id' ];
$this -> Task -> saveField ( 'message' , count ( $servers ) . ' job(s) completed at ' . date ( 'd/m/Y - H:i:s' ) . '. Failed jobs: ' . $failCount . '/' . $count );
2014-01-29 15:52:09 +01:00
}
2016-06-04 01:08:16 +02:00
2017-09-13 15:35:24 +02:00
public function enqueueFeedFetch () {
2017-08-25 14:38:32 +02:00
$timestamp = $this -> args [ 0 ];
$userId = $this -> args [ 1 ];
$taskId = $this -> args [ 2 ];
$task = $this -> Task -> read ( null , $taskId );
if ( $timestamp != $task [ 'Task' ][ 'next_execution_time' ]) {
return ;
}
2017-09-13 15:35:24 +02:00
if ( $task [ 'Task' ][ 'timer' ] > 0 ) $this -> Task -> reQueue ( $task , 'default' , 'ServerShell' , 'enqueueFeedFetch' , $userId , $taskId );
2017-08-25 14:38:32 +02:00
$user = $this -> User -> getAuthUser ( $userId );
$failCount = 0 ;
2017-09-13 15:35:24 +02:00
$feeds = $this -> Feed -> find ( 'all' , array (
'recursive' => - 1 ,
'conditions' => array ( 'enabled' => true )
));
foreach ( $feeds as $k => $feed ) {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'feed_fetch' ,
'job_input' => 'Feed: ' . $feed [ 'Feed' ][ 'id' ],
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'org_id' => $user [ 'org_id' ],
'process_id' => 'Part of scheduled feed fetch' ,
'message' => 'Pulling.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
$result = $this -> Feed -> downloadFromFeedInitiator ( $feed [ 'Feed' ][ 'id' ], $user , $jobId );
$this -> Job -> save ( array (
'message' => 'Job done.' ,
'progress' => 100 ,
'status' => 4
));
if ( $result !== true ) {
$this -> Job -> saveField ( 'message' , 'Could not fetch feed.' );
$failCount ++ ;
}
}
$this -> Task -> id = $task [ 'Task' ][ 'id' ];
$this -> Task -> saveField ( 'message' , count ( $feeds ) . ' job(s) completed at ' . date ( 'd/m/Y - H:i:s' ) . '. Failed jobs: ' . $failCount . '/' . count ( $feeds ));
2017-08-25 14:38:32 +02:00
}
2017-09-13 15:35:24 +02:00
public function enqueueFeedCache () {
$timestamp = $this -> args [ 0 ];
$userId = $this -> args [ 1 ];
$taskId = $this -> args [ 2 ];
$task = $this -> Task -> read ( null , $taskId );
if ( $timestamp != $task [ 'Task' ][ 'next_execution_time' ]) {
return ;
}
if ( $task [ 'Task' ][ 'timer' ] > 0 ) $this -> Task -> reQueue ( $task , 'default' , 'ServerShell' , 'enqueueFeedCache' , $userId , $taskId );
$user = $this -> User -> getAuthUser ( $userId );
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'feed_cache' ,
2018-01-16 23:22:56 +01:00
'job_input' => '' ,
2017-09-13 15:35:24 +02:00
'retries' => 0 ,
'org' => $user [ 'Organisation' ][ 'name' ],
'org_id' => $user [ 'org_id' ],
'process_id' => 'Part of scheduled feed caching' ,
'message' => 'Caching.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
$result = $this -> Feed -> cacheFeedInitiator ( $user , $jobId , 'all' );
$this -> Job -> save ( array (
'message' => 'Job done.' ,
'progress' => 100 ,
'status' => 4
));
$this -> Task -> id = $task [ 'Task' ][ 'id' ];
$this -> Task -> saveField ( 'message' , 'Job completed at ' . date ( 'd/m/Y - H:i:s' ));
}
2017-08-25 14:38:32 +02:00
2014-01-29 15:52:09 +01:00
public function enqueuePush () {
$timestamp = $this -> args [ 0 ];
$taskId = $this -> args [ 1 ];
2015-11-28 23:49:52 +01:00
$userId = $this -> args [ 2 ];
2014-01-29 15:52:09 +01:00
$this -> Task -> id = $taskId ;
$task = $this -> Task -> read ( null , $taskId );
if ( $timestamp != $task [ 'Task' ][ 'next_execution_time' ]) {
return ;
}
2015-06-03 23:49:37 +02:00
if ( $task [ 'Task' ][ 'timer' ] > 0 ) $this -> Task -> reQueue ( $task , 'default' , 'ServerShell' , 'enqueuePush' , $userId , $taskId );
2016-06-04 01:08:16 +02:00
2015-01-12 17:35:08 +01:00
$this -> User -> recursive = - 1 ;
2015-04-20 13:50:14 +02:00
$user = $this -> User -> getAuthUser ( $userId );
2014-01-29 15:52:09 +01:00
$servers = $this -> Server -> find ( 'all' , array ( 'recursive' => - 1 , 'conditions' => array ( 'push' => 1 )));
foreach ( $servers as $k => $server ) {
$this -> Job -> create ();
$data = array (
'worker' => 'default' ,
'job_type' => 'push' ,
'job_input' => 'Server: ' . $server [ 'Server' ][ 'id' ],
'retries' => 0 ,
2015-04-20 13:50:14 +02:00
'org' => $user [ 'Organisation' ][ 'name' ],
'org_id' => $user [ 'org_id' ],
2015-01-15 13:46:11 +01:00
'process_id' => 'Part of scheduled push' ,
2014-01-29 15:52:09 +01:00
'message' => 'Pushing.' ,
);
$this -> Job -> save ( $data );
$jobId = $this -> Job -> id ;
App :: uses ( 'SyncTool' , 'Tools' );
$syncTool = new SyncTool ();
$HttpSocket = $syncTool -> setupHttpSocket ( $server );
2016-03-09 11:25:05 +01:00
$result = $this -> Server -> push ( $server [ 'Server' ][ 'id' ], 'full' , $jobId , $HttpSocket , $user );
2014-01-29 15:52:09 +01:00
}
2015-06-03 23:49:37 +02:00
$this -> Task -> id = $task [ 'Task' ][ 'id' ];
$this -> Task -> saveField ( 'message' , count ( $servers ) . ' job(s) completed at ' . date ( 'd/m/Y - H:i:s' ) . '.' );
2014-01-06 05:15:47 +01:00
}
}