/**
  * 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 apply_access_policy($domain_name, $policies = false, $region = false)
 {
     if (!$policies) {
         $policies = $this->get_default_access_policies($domain_name, $region);
         if (!$policies) {
             return false;
         }
     }
     if (!$this->config_api->UpdateServiceAccessPolicies($domain_name, $policies, $region)) {
         Lift_Search::event_log('There was an error while applying the default access policy to the domain.', $this->config_api->get_last_error(), array('access policy', 'error'));
         return new WP_Error('There was an error while applying the default access policy to the domain.');
     }
     return true;
 }