Skip to content

Commit

Permalink
Merge branch 'feature/rekognition_importer' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Sep 12, 2017
2 parents 4e933ec + 26c16ce commit 2bd7fba
Show file tree
Hide file tree
Showing 8 changed files with 557 additions and 8 deletions.
115 changes: 115 additions & 0 deletions classes/tasks/ilab-recognizer-process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

// Copyright (c) 2016 Interfacelab LLC. All rights reserved.
//
// Released under the GPLv3 license
// http://www.gnu.org/licenses/gpl-3.0.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

if (!defined('ABSPATH')) { header('Location: /'); die; }

require_once('wp-background-process.php');

require_once(ILAB_CLASSES_DIR.'/ilab-media-tools-manager.php');
require_once(ILAB_CLASSES_DIR.'/tools/rekognition/ilab-media-rekognition-tool.php');
require_once(ILAB_CLASSES_DIR.'/utils/ilab-media-tool-logger.php');

/**
* Class ILABRekognizerProcess
*
* Background processing job for processing existing media with AWS Rekognizer
*/
class ILABRekognizerProcess extends ILAB_WP_Background_Process {
protected $action = 'ilab_rekognizer_import_process';

protected function shouldHandle() {
$result = !get_option('ilab_rekognizer_should_cancel', false);
return $result;
}

public function task($item) {
ILabMediaToolLogger::info('Start Task', $item);
if (!$this->shouldHandle()) {
ILabMediaToolLogger::info('Task cancelled', $item);
return false;
}

$index = $item['index'];
$post_id = $item['post'];

update_option('ilab_rekognizer_current', $index+1);
$data = wp_get_attachment_metadata($post_id);

if (empty($data)) {
ILabMediaToolLogger::info('Missing metadata', $item);
return false;
}

if (!isset($data['s3'])) {
ILabMediaToolLogger::info('Missing s3 metadata', $item);
return false;
}

$fileName = basename($data['file']);
update_option('ilab_rekognizer_current_file', $fileName);


$rekognizerTool = ILabMediaToolsManager::instance()->tools['rekognition'];
$data = $rekognizerTool->processImageMeta($post_id, $data);
wp_update_attachment_metadata($post_id, $data);

return false;
}

public function dispatch() {
ILabMediaToolLogger::info('Task dispatch');
parent::dispatch();
}

protected function complete() {
ILabMediaToolLogger::info('Task complete');
delete_option('ilab_rekognizer_status');
delete_option('ilab_rekognizer_total_count');
delete_option('ilab_rekognizer_current');
delete_option('ilab_rekognizer_current_file');
parent::complete();
}

public function cancel_process() {
ILabMediaToolLogger::info('Cancel process');

parent::cancel_process();

delete_option('ilab_rekognizer_status');
delete_option('ilab_rekognizer_total_count');
delete_option('ilab_rekognizer_current');
delete_option('ilab_rekognizer_current_file');
}

public static function cancelAll() {
ILabMediaToolLogger::info('Cancel all processes');

wp_clear_scheduled_hook('wp_ilab_rekognizer_import_process_cron');

global $wpdb;

$res = $wpdb->get_results("select * from {$wpdb->options} where option_name like 'wp_ilab_rekognizer_import_process_batch_%'");
foreach($res as $batch) {
ILabMediaToolLogger::info("Deleting batch {$batch->option_name}");
delete_option($batch->option_name);
}

delete_option('ilab_rekognizer_status');
delete_option('ilab_rekognizer_total_count');
delete_option('ilab_rekognizer_current');
delete_option('ilab_rekognizer_current_file');

ILabMediaToolLogger::info("Current cron", get_option('cron', []));
ILabMediaToolLogger::info('End cancel all processes');
}
}
12 changes: 11 additions & 1 deletion classes/tasks/wp-async-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

if (!defined('ABSPATH')) { header('Location: /'); die; }

require_once(ILAB_CLASSES_DIR.'/utils/ilab-media-tool-logger.php');

/**
* WP Async Request
*
Expand Down Expand Up @@ -87,6 +89,8 @@ public function dispatch() {
$url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
$args = $this->get_post_args();

ILabMediaToolLogger::info("Background dispatching $url", $args);

return wp_remote_post( esc_url_raw( $url ), $args );
}

Expand Down Expand Up @@ -144,7 +148,13 @@ protected function get_post_args() {
* Check for correct nonce and pass to handler.
*/
public function maybe_handle() {
check_ajax_referer( $this->identifier, 'nonce' );
$check = check_ajax_referer( $this->identifier, 'nonce', false );

ILabMediaToolLogger::info("Maybe handle, check nonce: ".(($check) ? 'true' : 'false'));

if (!$check) {
wp_die();
}

$this->handle();

Expand Down
28 changes: 24 additions & 4 deletions classes/tasks/wp-background-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
if (!defined('ABSPATH')) { header('Location: /'); die; }

require_once('wp-async-request.php');
require_once(ILAB_CLASSES_DIR.'/utils/ilab-media-tool-logger.php');

/**
* WP Background Process
Expand Down Expand Up @@ -104,6 +105,7 @@ public function save() {
$key = $this->generate_key();

if ( ! empty( $this->data ) ) {
ILabMediaToolLogger::info("Saving queue: $key", $this->data);
update_site_option( $key, $this->data );
}

Expand Down Expand Up @@ -230,6 +232,7 @@ protected function is_process_running() {
* defined in the time_exceeded() method.
*/
protected function lock_process() {
ILabMediaToolLogger::info("Locking process {$this->identifier}");
$this->start_time = time(); // Set start time of current process.

$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
Expand All @@ -246,6 +249,7 @@ protected function lock_process() {
* @return $this
*/
protected function unlock_process() {
ILabMediaToolLogger::info("Unlocking process {$this->identifier}");
delete_site_transient( $this->identifier . '_process_lock' );

return $this;
Expand Down Expand Up @@ -301,11 +305,22 @@ protected function shouldHandle() {
protected function handle() {
$this->lock_process();

$shouldCancelAll = false;

do {
$batch = $this->get_batch();

if ((count($batch->data) == 0) || (!$this->shouldHandle())) {
$shouldCancelAll = true;
ILabMediaToolLogger::info("No batch to process", $batch);
break;
}

ILabMediaToolLogger::info("Processing Batch", $batch->data);
foreach ( $batch->data as $key => $value ) {
if ($this->shouldHandle()) {
ILabMediaToolLogger::info("Running task", $value);

$task = $this->task( $value );

if ( false !== $task ) {
Expand All @@ -331,11 +346,15 @@ protected function handle() {

$this->unlock_process();

// Start next batch or complete process.
if ( ! $this->is_queue_empty() ) {
$this->dispatch();
} else {
if ($shouldCancelAll) {
$this->complete();
static::cancelAll();
} else {
if ( ! $this->is_queue_empty() ) {
$this->dispatch();
} else {
$this->complete();
}
}

wp_die();
Expand Down Expand Up @@ -509,5 +528,6 @@ public function cancel_process() {
*/
abstract public function task( $item );

abstract public static function cancelAll();
}
}
Loading

0 comments on commit 2bd7fba

Please sign in to comment.