= $this->get_last_processed_attachment();
$last_process_attachment['elapsed_time'] = $this->get_seconds_since_last_image_processing_started();
$this->set_process_item(
self::FIRST_STUCK_ATTACHMENT,
$last_process_attachment
);
}
private function is_process_stuck() {
$elapsed_time = $this->get_seconds_since_last_image_processing_started();
return $elapsed_time > self::PROCESS_TIME_OUT;
}
public function record_last_processed_attachment_elapsed_time() {
$last_process_attachment = $this->get_last_processed_attachment();
$last_process_attachment['attachment_elapsed_time'] = $this->get_last_process_attachment_elapsed_time();
$this->set_process_item( self::LAST_ATTACHMENT, $last_process_attachment );
}
public function get_last_process_attachment_elapsed_time() {
$last_process_attachment = $this->get_last_processed_attachment();
$attachment_elapsed_time = (int) $this->array_utils->get_array_value( $last_process_attachment, 'attachment_elapsed_time', - 1 );
if ( $attachment_elapsed_time > - 1 ) {
return $attachment_elapsed_time;
}
return $this->get_seconds_since_last_image_processing_started();
}
public function get_seconds_since_last_image_processing_started() {
$last_process_attachment = $this->get_last_processed_attachment();
$start_time = (int) $this->array_utils->get_array_value( $last_process_attachment, 'start_time' );
if ( empty( $start_time ) ) {
return 0;
}
$end_time = time();
return $end_time - $start_time;
}
public function get_last_process_attachment_id() {
$last_process_attachment = $this->get_last_processed_attachment();
return $this->array_utils->get_array_value( $last_process_attachment, 'id', 0 );
}
private function get_last_processed_attachment() {
return $this->get_process_item( self::LAST_ATTACHMENT );
}
public function record_process_start_time() {
$this->reset_process_option();
$this->set_process_start_time();
}
public function record_process_end_time() {
$this->set_process_end_time();
}
private function reset_process_option() {
delete_option( self::PROCESS_KEY );
wp_cache_delete( self::PROCESS_KEY, 'options' );
}
private function set_process_start_time() {
$this->set_process_item( self::START_TIME, microtime( true ) );
}
private function set_process_end_time() {
$this->set_process_item( self::END_TIME, microtime( true ) );
}
public function get_process_elapsed_time() {
$start_time = $this->get_process_start_time();
$end_time = $this->get_process_end_time();
return (int) ( $end_time - $start_time );
}
public function get_process_start_time() {
return $this->get_process_item( self::START_TIME );
}
private function get_process_end_time() {
return $this->get_process_item( self::END_TIME, time() );
}
private function get_process_item( $item, $default_value = false ) {
$process_option = $this->get_process_option();
return $this->array_utils->get_array_value( $process_option, $item, $default_value );
}
private function set_process_item( $item, $value ) {
( new Mutex( self::PROCESS_KEY ) )->execute( function () use ( $item, $value ) {
$process_option = $this->get_process_option();
$process_option[ $item ] = $value;
$this->update_process_option( $process_option );
} );
}
private function get_process_option() {
$last_process = get_option( self::PROCESS_KEY, array() );
return $this->array_utils->ensure_array( $last_process );
}
private function update_process_option( $last_process_option ) {
update_option( self::PROCESS_KEY, $last_process_option, false );
}
}