예제 #1
0
 static function sync($task)
 {
     aws_s3::log("Amazon S3 Re-sync task started..");
     batch::start();
     $items = ORM::factory("item")->find_all();
     $task->set("total_count", count($items));
     $task->set("completed", 0);
     if (!module::get_var("aws_s3", "synced", false)) {
         aws_s3::log("Emptying contents of bucket");
         $task->status = "Emptying contents of bucket";
         $task->save();
         require_once MODPATH . "aws_s3/lib/s3.php";
         $s3 = new S3(module::get_var("aws_s3", "access_key"), module::get_var("aws_s3", "secret_key"));
         $bucket = module::get_var("aws_s3", "bucket_name");
         $resource = aws_s3::get_resource_url("");
         $stuff = array_reverse(S3::getBucket($bucket, $resource));
         $i = 0;
         foreach ($stuff as $uri => $item) {
             $i++;
             aws_s3::log("Removing " . $uri . " from S3");
             S3::deleteObject($bucket, $uri);
             $task->percent_complete = round(20 * ($i / count($stuff)));
             $task->save();
         }
     }
     $task->percent_complete = 20;
     aws_s3::log("Commencing upload tasks");
     $task->state = "Commencing upload...";
     $task->save();
     $completed = $task->get("completed", 0);
     $items = ORM::factory("item")->find_all();
     foreach ($items as $item) {
         try {
             if ($item->id > 1) {
                 aws_s3::upload_item($item, aws_s3::get_upload_flags());
             }
         } catch (Exception $err) {
         }
         $completed++;
         $task->set("completed", $completed);
         $task->percent_complete = round(80 * ($completed / $task->get("total_count"))) + 20;
         $task->status = $completed . " of " . $task->get("total_count") . " uploaded.";
         $task->save();
     }
     $task->percent_complete = 100;
     $task->state = "success";
     $task->done = true;
     aws_s3::log("Sync task completed successfully");
     $task->status = "Sync task completed successfully";
     module::set_var("aws_s3", "synced", true);
     site_status::clear("aws_s3_not_synced");
     batch::stop();
     $task->save();
 }