/**
  * Pulls the next set of items from the queue and sends a batch from it
  * Callback for Batch Submission Cron
  *
  * @todo Add locking
  */
 public static function send_next_batch()
 {
     if (!self::ready_for_batch(Lift_Search::get_search_domain_name())) {
         delete_transient(self::BATCH_LOCK);
         Lift_Search::event_log('CloudSearch Not Ready for Batch ' . time(), 'The batch is locked or the search domain is either currently processing, needs indexing, or your domain does not have indexes set up.', array('send-queue', 'response-false', 'notice'));
         return;
     }
     $lock_key = md5(uniqid(microtime() . mt_rand(), true));
     if (!get_transient(self::BATCH_LOCK)) {
         set_transient(self::BATCH_LOCK, $lock_key, 300);
     }
     if (get_transient(self::BATCH_LOCK) !== $lock_key) {
         //another cron has this lock
         return;
     }
     update_option(self::LAST_CRON_TIME_OPTION, time());
     $closed_queue_id = Lift_Document_Update_Queue::get_closed_queue_id();
     $update_query = Lift_Document_Update_Queue::query_updates(array('per_page' => self::get_queue_all_set_size(), 'queue_ids' => array($closed_queue_id)));
     if (!count($update_query->meta_rows)) {
         //no documents queued up
         Lift_Document_Update_Queue::close_active_queue();
         delete_transient(self::BATCH_LOCK);
         return;
     }
     $batch = new Lift_Batch();
     $batched_meta_keys = array();
     $blog_id = get_current_blog_id();
     $site_id = lift_get_current_site_id();
     foreach ($update_query->meta_rows as $meta_row) {
         $update_data = get_post_meta($meta_row->post_id, $meta_row->meta_key, true);
         if ($update_data['document_type'] == 'post') {
             $action = $update_data['action'];
             if ($action == 'add') {
                 $post = get_post($update_data['document_id'], ARRAY_A);
                 $post_data = array('ID' => $update_data['document_id'], 'blog_id' => $blog_id, 'site_id' => $site_id);
                 foreach (Lift_Search::get_indexed_post_fields($post['post_type']) as $field) {
                     $post_data[$field] = isset($post[$field]) ? $post[$field] : null;
                 }
                 $sdf_field_data = apply_filters('lift_post_changes_to_data', $post_data, $update_data['fields'], $update_data['document_id']);
             } else {
                 $sdf_field_data = array('ID' => intval($update_data['document_id']));
             }
             $sdf_doc = Lift_Posts_To_SDF::format_post((object) $sdf_field_data, array('action' => $action, 'time' => time()));
             try {
                 $batch->add_document((object) $sdf_doc);
                 $batched_meta_keys[] = $meta_row->meta_key;
             } catch (Lift_Batch_Exception $e) {
                 if (isset($e->errors[0]['code']) && 500 == $e->errors[0]['code']) {
                     break;
                 }
                 Lift_Search::event_log('Batch Add Error ' . time(), json_encode($e), array('batch-add', 'error'));
                 //@todo log error, stop cron? --- update_option( self::$search_semaphore, 1 );
                 continue;
             }
         }
     }
     //send the batch
     $cloud_api = Lift_Search::get_search_api();
     if ($r = $cloud_api->sendBatch($batch)) {
         if ($r->status === "success") {
             $log_title = "Post Queue Sent ";
             $tag = 'success';
             foreach ($batched_meta_keys as $meta_key) {
                 delete_post_meta($closed_queue_id, $meta_key);
             }
         } else {
             $log_title = "Post Queue Send Error ";
             $tag = 'error';
         }
         Lift_Search::event_log($log_title . time(), json_encode($r), array('send-queue', 'response-true', $tag));
         //@todo delete sent queued items
     } else {
         $messages = $cloud_api->getErrorMessages();
         Lift_Search::event_log('Post Queue Error ' . time(), $messages, array('send-queue', 'response-false', 'error'));
     }
     delete_transient(self::BATCH_LOCK);
 }
 public function action__wp_ajax_lift_update_queue()
 {
     $response = (object) array('error' => false);
     $page = max(abs($_GET['paged']), 1);
     $update_query = Lift_Document_Update_Queue::query_updates(array('page' => $page, 'per_page' => 10, 'queue_ids' => array(Lift_Document_Update_Queue::get_active_queue_id(), Lift_Document_Update_Queue::get_closed_queue_id())));
     $response->current_page = $page;
     $response->per_page = 10;
     $response->found_rows = $update_query->found_rows;
     $response->updates = array();
     foreach ($update_query->meta_rows as $meta_row) {
         $meta_value = get_post_meta($meta_row->post_id, $meta_row->meta_key, true);
         switch ($meta_value['document_type']) {
             case 'post':
                 $post_id = $meta_value['document_id'];
                 if ($meta_value['action'] == 'add') {
                     $last_user = '';
                     if ($last_id = get_post_meta($post_id, '_edit_last', true)) {
                         $last_user = get_userdata($last_id);
                     }
                     $response->updates[] = array('id' => $post_id, 'action' => 'add', 'title' => get_the_title($post_id), 'edit_url' => esc_url(get_edit_post_link($post_id)), 'author_name' => isset($last_user->display_name) ? $last_user->display_name : '', 'queue_date' => mysql2date('D. M d Y g:ia', $meta_value['update_date']));
                 } else {
                     $response->updates[] = array('id' => $post_id, 'action' => 'delete', 'title' => sprintf('Post Deletion (%d)', $post_id), 'edit_url' => '#', 'author_name' => '', 'queue_date' => mysql2date('D. M d Y g:ia', $meta_value['update_date']));
                 }
                 break;
             default:
                 continue;
         }
     }
     header('Content-Type: application/json');
     die(json_encode($response));
 }