/**
* Cron job to update the region of the bucket in s3 metadata
*/
public function do_upgrade()
{
// Check if the cron should even be running
if ($this->get_saved_upgrade_id() >= $this->upgrade_id || $this->get_upgrade_status() !== self::STATUS_RUNNING) {
$this->unschedule();
return;
}
// set the batch size limit for the query
$limit = apply_filters('as3cf_update_' . $this->upgrade_name . '_batch_size', 500);
$all_limit = $limit;
$finish = time() + apply_filters('as3cf_update_' . $this->upgrade_name . '_time_limit', 20);
$session = $this->get_session();
// find the blog IDs that have been processed so we can skip them
$processed_blog_ids = isset($session['processed_blog_ids']) ? $session['processed_blog_ids'] : array();
$this->error_count = isset($session['error_count']) ? $session['error_count'] : 0;
$offset = isset($session['offset']) ? $session['offset'] : false;
// get the table prefixes for all the blogs
$table_prefixes = $this->as3cf->get_all_blog_table_prefixes($processed_blog_ids);
$all_items = array();
$all_count = 0;
foreach ($table_prefixes as $blog_id => $table_prefix) {
$items = $this->get_items_to_process($table_prefix, $limit, $offset);
$count = count($items);
if (0 === $count) {
// No more items, record the blog ID to skip next time
$session['offset'] = false;
$processed_blog_ids[] = $blog_id;
} else {
$all_count += $count;
$all_items[$blog_id] = $items;
}
if ($all_count >= $all_limit) {
break;
}
$limit = $limit - $count;
}
if (0 === $all_count) {
$this->upgrade_finished();
return;
}
// loop through and update s3 meta with region
foreach ($all_items as $blog_id => $items) {
$this->as3cf->switch_to_blog($blog_id);
foreach ($items as $item) {
if ($this->error_count >= $this->error_threshold) {
$this->upgrade_error($session);
return;
}
// Do the actual upgrade to the item
$this->upgrade_item($item);
if (time() >= $finish || $this->as3cf->memory_exceeded('as3cf_update_' . $this->upgrade_name . '_memory_exceeded')) {
// Batch limits reached
$this->as3cf->restore_current_blog();
break 2;
}
}
$this->as3cf->restore_current_blog();
}
$session['offset'] = isset($item) ? $item : false;
$session['processed_blog_ids'] = $processed_blog_ids;
$session['error_count'] = $this->error_count;
$this->save_session($session);
}