通过linux-inotify刷新fikker-cdn免登录版

因为之前对fikker 不了解,所以就写了一个登录的,登录后会遇到session 超时的问题,当然程序有自动重新登录的功能,但是登录是多一个过性能肯定没有一步到位好。好了,是时候贴代码了,

如果你想后台运行 后面加 & 就可以了。。如果你是ssh 上去 ,怕退了后自动关闭,就用nohup命令运行吧,当然有能力可以用 app::daemon 模块做成后台进程。

对于目录太大的文件夹,执行起来会慢点,建议用sersync -m socket 用socket 发送更新的文件

use strict;
use EV;
use 5.010;
use Data::Dumper;
use Date::Format;
use Mojo::UserAgent;
use URI::Escape;
use AnyEvent::Inotify::Simple;

# Fikker cdn server's 
# WARNNING: do not add "/" on url end.
my $server={
	srv1=>{ url=>'http://192.168.1.99:6780',passwd=>'abc123'},
	srv2=>{ url=>'http://192.168.1.88:6780',passwd=>'abc123'},
};

# Monittor DIR
my $dir='/root/test';

#Full path of Site url
my $site_url='http://www.yiyou.org/';

# open debug =1 ,close = 0
my $debug=1;

#   ===== END CONFIG ====
my $ua = Mojo::UserAgent->new;

our $queue;

my $w = EV::timer 2, 2, sub {
   my $q=$queue;
   $queue={};
   #say "timer print";
   for my $key(keys %{$q}){
		#say  "=>",$key;
		my $url=uri_escape($site_url.$key);
		say "site url $url" if $debug;
		
		for my $srv(keys %{$server}){
			my $try=3;
			do{
				my $srv_passwd=$server->{$srv}->{passwd};
				my $get_url=$server->{$srv}->{url}."/fikker/webcache.fik?type=thiscache&cmd=cleancache&Username=admin&Password=$srv_passwd&Icase=0&Rules=2&WithCluster=1&Url=$url";
				my $res=$ua->get($get_url)->res->json;
				#print Dumper $res;
				if($res->{Return} eq  "False" ){
					warn "refresh url $url fail.";
				}else{
					$try=-1;
				}
				$try --;
				say "try $try" if $debug;
			}while($try > 0);
		}
   }
};
my $inotify = AnyEvent::Inotify::Simple->new(
    directory      => $dir,
    event_receiver => sub {
        my ($event, $file, $moved_to) = @_;
		#print Dumper $event;
		given($event) {
			when('modify'){
				$queue->{$file}=1;
				#say "modify => $file" if $debug;
			}
			when('delete'){
				$queue->{$file}=1;
				#say "modify => $file" if $debug;
			}
        };

    },
);

EV::loop;