Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WordPress 運用を支える Perl

662 views

Published on

2018/3/3に行われた YAPC::Okinawa 2018 ONNASON で発表したトークのスライドです。

Published in: Technology
  • Be the first to comment

  • Be the first to like this

WordPress 運用を支える Perl

  1. 1. 

  2. 2. 🙇
  3. 3. n 
 /wp-content/uploads/*
  4. 4. # aws s3 sync /var/www/sites/www.gaiax.co.jp/wp-content/uploads/ s3:// /wp-content/uploads/
  5. 5. #!/usr/bin/env perl use strict; use warnings; use File::ChangeNotify; my $watcher = File::ChangeNotify->instantiate_watcher( directories => ["/var/www/sites/www.gaiax.co.jp/wp-content/uploads"], ); while ( my @events = $watcher->wait_for_events() ) { # TODO: aws s3 sync system "aws", "s3", "sync", "--delete", "/var/www/sites/www.gaiax.co.jp/wp-content/", "s3://gxofficial-static/wp-content/"; } 

  6. 6.
  7. 7. PerlOutputFilterHandler
  8. 8. package ModPerl::WordPress::StaticURLRewriter; # e.g. # # in VirtualHost block directive # <Files index.php> # PerlOutputFilterHandler ModPerl::WordPress::StaticURLRewriter # </Files> # use strict; use warnings; use base qw(Apache2::Filter); use Apache2::Const -compile => qw(OK); sub handler : FilterRequestHandler { my $f = shift; # filter object ... return Apache2::Const::OK; } 1;
  9. 9. use constant BUFF_LEN => 1024; sub handler : FilterRequestHandler { my $f = shift; unless ($f->ctx) { $f->r->headers_out->unset('Content-Length'); $f->ctx(1); } my $content = ''; while ($f->read(my $buffer, BUFF_LEN)) { $content .= $buffer; } ### $content ### $f->print($content); if ( $f->seen_eos ) { my $footer = "<!-- WRITE FOOTER IF EXIST -->"; $f->print($footer); } return Apache2::Const::OK; }
  10. 10. $content $content =~ s{(?<=<img)([^>]+)}{ my $in_img = $1; $in_img =~ s{(?<=bsrc=(['"]))$PREFIX_SELF(.*?)(?=1)}{$PREFIX_S3$2}; $in_img; }eg; $content =~ s{(?<=<img)([^>]+)}{ $1 =~ s{(?<=bsrc=(['"]))$PREFIX_SELF(.*?)(?=1)}{$PREFIX_S3$2}r }eg; 

  11. 11. PerlMapToStorageHandler
  12. 12. 
 
 
 
 

  13. 13. package ModPerl::WordPress::CacheManager; # e.g. # # in VirtualHost block directive # PerlMapToStorageHandler ModPerl::WordPress::CacheManager use strict; use warnings; use Apache2::RequestRec (); use Apache2::Const -compile => qw(OK DECLINED); sub handler { my $r = shift; # request object # $r->uri() $filename # $r->filename($filename) ... return Apache2::Const::DECLINED; } 1;
  14. 14. package ModPerl::WordPress::CacheManager; use strict; use warnings; use Apache2::RequestRec (); use Apache2::RequestUtil (); # for pnotes method use Apache2::Const -compile => qw(OK DECLINED); use APR::Table (); # for notes method use File::Basename qw(dirname); use File::Path qw(make_path); use constant CACHE_LIFETIME => 3600; sub handler { my $r = shift; if ( !is_cache_enable($r) ) { return Apache2::Const::DECLINED; } manage_cache($r); return Apache2::Const::DECLINED; }
  15. 15. package ModPerl::WordPress::CacheManager; # (snip) sub is_cache_enable { my $r = shift; return $r->uri eq '/index.php'; # WordPress /index.php (entry-point) } sub manage_cache { my $r = shift; my $cache_filename = guess_cache_filename($r); $r->notes->set( mpwp_cache_filename => $cache_filename ); if ( !-f $cache_filename || time - (stat _)[9] > CACHE_LIFETIME ) { # 9=mtime $r->pnotes( mpwp_cache_generate_cb => &mpwp_cache_filename ); # ... make_path(dirname($cache_filename)); } else { $r->handler('default-handler'); $r->filename($cache_filename); } }
  16. 16. package ModPerl::WordPress::CacheManager; # (snip) sub mpwp_cache_filename { my $f = shift; my $contents_ref = shift; die if ref $contents_ref ne 'SCALAR'; my $filter_phase = !$f->ctx ? "INITIAL" : $f->seen_eos ? "SEEN_EOS" : "IN_PROGRESS"; my $cache_filename = $f->r->notes->get("mpwp_cache_filename"); if ( $filter_phase eq "INITIAL" ) { unlink $cache_filename; # remove old cache } if ( open my $fh, '>>', "$cache_filename.$$" ) { print {$fh} $$contents_ref; } if ( $filter_phase eq "SEEN_EOS" ) { rename "$cache_filename.$$" => $cache_filename; } }
  17. 17. aws s3 sync
  18. 18. aws s3 sync 🤝
  19. 19. aws s3 sync 🤝
  20. 20.
  21. 21. http://www.gaiax.co.jp/blog/natalia03/

×