Skip to content

Commit

Permalink
Improved scheduled episodes sync, max 3 attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
zahardev committed Apr 30, 2024
1 parent 6b5339b commit 128ecd0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion php/classes/controllers/class-ads-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function is_ads_enabled_in_castos() {

$series_id = $this->get_current_feed_series_id();

if ( empty( $podcasts['data']['podcast_list'] ) ) {
if ( ! is_array( $podcasts ) || empty( $podcasts['data']['podcast_list'] ) ) {
return false;
}

Expand Down
45 changes: 36 additions & 9 deletions php/classes/controllers/class-cron-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ class Cron_Controller {
* */
protected $upgrade_handler;

const SYNC_SCHEDULE_META = 'podmotor_schedule_upload';

const ATTEMPTS_META = 'castos_sync_attempts';

const MAX_ATTEMPTS = 3;

/**
* @param Castos_Handler $castos_handler
* @param Episode_Repository $episodes_respository
* @param Upgrade_Handler $upgrade_handler
*/
public function __construct( $castos_handler, $episodes_respository, $upgrade_handler ) {

$this->castos_handler = $castos_handler;
$this->castos_handler = $castos_handler;
$this->episodes_respository = $episodes_respository;
$this->upgrade_handler = $upgrade_handler;
$this->upgrade_handler = $upgrade_handler;

add_action( 'admin_init', array( $this, 'schedule_events' ) );
add_filter( 'cron_schedules', array( $this, 'add_cron_intervals' ) );
Expand All @@ -55,7 +61,7 @@ public function __construct( $castos_handler, $episodes_respository, $upgrade_ha
/**
* @return void
*/
protected function run_actions(){
protected function run_actions() {
add_action( 'ssp_cron_hook', array( $this, 'upload_scheduled_episodes' ) );
$this->upgrade_handler->run_upgrade_actions();
}
Expand All @@ -80,7 +86,7 @@ public function schedule_events() {
if ( ! wp_next_scheduled( 'ssp_cron_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'ssp_cron_hook' );
}
if( ! wp_next_scheduled('ssp_check_ads') ){
if ( ! wp_next_scheduled( 'ssp_check_ads' ) ) {
wp_schedule_event( time(), 'daily', 'ssp_check_ads' );
}
}
Expand All @@ -90,19 +96,36 @@ public function schedule_events() {
*/
public function upload_scheduled_episodes() {
$uploaded = 0;
$logger = new Log_Helper();
$logger = new Log_Helper();

foreach ( $this->episodes_respository->get_scheduled_episodes() as $episode ) {
$response = $this->castos_handler->upload_episode_to_castos( $episode );

if ( 'success' === $response['status'] ) {
delete_post_meta( $episode->ID, 'podmotor_schedule_upload' );
$this->unschedule_episode( $episode->ID );
$this->episodes_respository->update_episode_sync_status( $episode->ID, Sync_Status::SYNC_STATUS_SYNCED );
$this->episodes_respository->delete_episode_sync_error( $episode->ID );
$uploaded++;
$uploaded ++;
} else {
$attempts = get_post_meta( $episode->ID, self::ATTEMPTS_META, true );
if ( $attempts < self::MAX_ATTEMPTS ) {
update_post_meta( $episode->ID, self::ATTEMPTS_META, ++ $attempts );
} else {
$this->unschedule_episode( $episode->ID );
}
}

if ( 404 == $response['code'] ) {
delete_post_meta( $episode->ID, 'podmotor_schedule_upload' );
if ( isset( $response['code'] ) && 404 == $response['code'] ) {
$castos_episode_id = get_post_meta( $episode->ID, 'podmotor_episode_id', true );

// Episode does not exists anymore, remove connection
if ( $castos_episode_id ) {
delete_post_meta( $episode->ID, 'podmotor_episode_id' );
delete_post_meta( $episode->ID, 'podmotor_file_id' );
}

$this->unschedule_episode( $episode->ID );

$logger->log( sprintf( 'Cron: could not upload episode %d', $episode->ID ) );
}
}
Expand All @@ -113,4 +136,8 @@ public function upload_scheduled_episodes() {

return $uploaded;
}

protected function unschedule_episode( $episode_id ) {
delete_post_meta( $episode_id, self::SYNC_SCHEDULE_META );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ public function sync_episode( $id, $post ) {
// Schedule uploading with a cronjob.1
// If it's 404, something wrong with the file ID. We don't try to reupload it since result will be the same.
if ( 404 != $response['code'] ) {
update_post_meta( $id, 'podmotor_schedule_upload', true );
update_post_meta( $id, Cron_Controller::SYNC_SCHEDULE_META, true );
update_post_meta( $id, Cron_Controller::ATTEMPTS_META, 1 );
}
$this->admin_notices_handler->add_predefined_flash_notice(
Admin_Notifications_Handler::NOTICE_API_EPISODE_ERROR
Expand Down
12 changes: 6 additions & 6 deletions php/classes/handlers/class-castos-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,19 @@ public function upload_episode_to_castos( $post ) {

$app_response = wp_remote_post( $api_url, $options );

if ( isset( $app_response['response']['code'] ) ) {
$this->update_response( 'code', intval( $app_response['response']['code'] ) );
}

$this->logger->log( 'Upload Podcast app_response', $app_response );

if ( is_wp_error( $app_response ) ) {
$this->logger->log( 'An unknown error occurred sending podcast data to castos: ' . $app_response->get_error_message() );
$this->update_response( 'message', 'An unknown error occurred: ' . $app_response->get_error_message() );

return $this->response;
}

if ( isset( $app_response['response']['code'] ) ) {
$this->update_response( 'code', intval( $app_response['response']['code'] ) );
}

$this->logger->log( 'Upload Podcast app_response', $app_response );

$response_object = json_decode( wp_remote_retrieve_body( $app_response ) );

$this->logger->log( 'Upload Podcast Response', $response_object );
Expand Down

0 comments on commit 128ecd0

Please sign in to comment.