Skip to content

Commit

Permalink
Improve ActionScheduler_Logger abstraction
Browse files Browse the repository at this point in the history
By moving common hooks/callbacks out of ActionScheduler_wpCommentLogger
and into the base ActionScheduler_Logger.

While looking at these, I noticed they had no dependency on the Comment
store (or anything else in ActionScheduler_wpCommentLogger), so suspected
they were better suited to be in ActionScheduler_Logger. I then checked
DB_Logger in Action_Scheduler\Custom_Tables and found it had duplicates
of all of it, confirming they belong in the shared base class.
  • Loading branch information
Brent Shepherd committed Aug 14, 2018
1 parent 7a93e55 commit bca46a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 43 deletions.
48 changes: 47 additions & 1 deletion classes/ActionScheduler_Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,53 @@ abstract public function get_entry( $entry_id );
*/
abstract public function get_logs( $action_id );

abstract public function init();

/**
* @codeCoverageIgnore
*/
public function init() {
add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ), 10, 1 );
add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 );
add_action( 'action_scheduler_before_execute', array( $this, 'log_started_action' ), 10, 1 );
add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 1 );
add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 2 );
add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 );
add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 );
add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 );
}

public function log_stored_action( $action_id ) {
$this->log( $action_id, __( 'action created', 'action-scheduler' ) );
}

public function log_canceled_action( $action_id ) {
$this->log( $action_id, __( 'action canceled', 'action-scheduler' ) );
}

public function log_started_action( $action_id ) {
$this->log( $action_id, __( 'action started', 'action-scheduler' ) );
}

public function log_completed_action( $action_id ) {
$this->log( $action_id, __( 'action complete', 'action-scheduler' ) );
}

public function log_failed_action( $action_id, \Exception $exception ) {
$this->log( $action_id, sprintf( __( 'action failed: %s', 'action-scheduler' ), $exception->getMessage() ) );
}

public function log_timed_out_action( $action_id, $timeout ) {
$this->log( $action_id, sprintf( __( 'action timed out after %s seconds', 'action-scheduler' ), $timeout ) );
}

public function log_unexpected_shutdown( $action_id, $error ) {
if ( ! empty( $error ) ) {
$this->log( $action_id, sprintf( __( 'unexpected shutdown: PHP Fatal error %s in %s on line %s', 'action-scheduler' ), $error[ 'message' ], $error[ 'file' ], $error[ 'line' ] ) );
}
}

public function log_reset_action( $action_id ) {
$this->log( $action_id, __( 'action reset', 'action_scheduler' ) );
}
}

45 changes: 3 additions & 42 deletions classes/ActionScheduler_wpCommentLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,9 @@ public function delete_comment_count_cache() {
public function init() {
add_action( 'action_scheduler_before_process_queue', array( $this, 'disable_comment_counting' ), 10, 0 );
add_action( 'action_scheduler_after_process_queue', array( $this, 'enable_comment_counting' ), 10, 0 );
add_action( 'action_scheduler_stored_action', array( $this, 'log_stored_action' ), 10, 1 );
add_action( 'action_scheduler_canceled_action', array( $this, 'log_canceled_action' ), 10, 1 );
add_action( 'action_scheduler_before_execute', array( $this, 'log_started_action' ), 10, 1 );
add_action( 'action_scheduler_after_execute', array( $this, 'log_completed_action' ), 10, 1 );
add_action( 'action_scheduler_failed_execution', array( $this, 'log_failed_action' ), 10, 2 );
add_action( 'action_scheduler_failed_action', array( $this, 'log_timed_out_action' ), 10, 2 );
add_action( 'action_scheduler_unexpected_shutdown', array( $this, 'log_unexpected_shutdown' ), 10, 2 );
add_action( 'action_scheduler_reset_action', array( $this, 'log_reset_action' ), 10, 1 );

parent::init();

add_action( 'pre_get_comments', array( $this, 'filter_comment_queries' ), 10, 1 );
add_action( 'wp_count_comments', array( $this, 'filter_comment_count' ), 20, 2 ); // run after WC_Comments::wp_count_comments() to make sure we exclude order notes and action logs
add_action( 'comment_feed_where', array( $this, 'filter_comment_feed' ), 10, 2 );
Expand All @@ -241,38 +236,4 @@ public function enable_comment_counting() {
wp_defer_comment_counting(false);
}

public function log_stored_action( $action_id ) {
$this->log( $action_id, __('action created', 'action-scheduler') );
}

public function log_canceled_action( $action_id ) {
$this->log( $action_id, __('action canceled', 'action-scheduler') );
}

public function log_started_action( $action_id ) {
$this->log( $action_id, __('action started', 'action-scheduler') );
}

public function log_completed_action( $action_id ) {
$this->log( $action_id, __('action complete', 'action-scheduler') );
}

public function log_failed_action( $action_id, Exception $exception ) {
$this->log( $action_id, sprintf(__('action failed: %s', 'action-scheduler'), $exception->getMessage() ));
}

public function log_timed_out_action( $action_id, $timeout) {
$this->log( $action_id, sprintf( __('action timed out after %s seconds', 'action-scheduler'), $timeout ) );
}

public function log_unexpected_shutdown( $action_id, $error ) {
if ( !empty($error) ) {
$this->log( $action_id, sprintf(__('unexpected shutdown: PHP Fatal error %s in %s on line %s', 'action-scheduler'), $error['message'], $error['file'], $error['line'] ) );
}
}

public function log_reset_action( $action_id ) {
$this->log( $action_id, __('action reset', 'action_scheduler') );
}

}

0 comments on commit bca46a8

Please sign in to comment.