test file_size with send to different shard
[cloudstore.git] / gearman / send_file.pl
index 752f7b1..4c5820f 100755 (executable)
@@ -2,21 +2,28 @@
 use warnings;
 use strict;
 
+=head1 NAME
+
+Filesystem Gearman Worker
+
+=cut
+
+
 use Data::Dump qw(dump);
-use File::Path qw(make_path remove_tree);
 
-sub home_dir {
-       my $login = shift;
-       my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) = getpwnam $login;
-       return $dir;
-}
+use lib '/srv/cloudstore/lib';
+use CloudStore::API;
+use WarnColor;
+
+my $api = CloudStore::API->new('s1');
 
 use Gearman::Worker;
 my $worker = Gearman::Worker->new;
 $worker->job_servers('127.0.0.1:4730');
 
-open(my $log, '>>', '/rsync1/s1/log/send_file.log');
-select($log); $|++;
+=head2 send_file ~u2001/from.txt#~u2003/dir/new to.txt
+
+=cut
 
 $worker->register_function( send_file => sub {
        my ($job) = @_;
@@ -26,23 +33,18 @@ $worker->register_function( send_file => sub {
        warn "# send_file [$work]\n";
 
        my ( $from, $to ) = split(/#/,$work,2);
+       
+       my $f_uid = $1 if $from =~ s{~(\w+)/}{};
+       my $t_uid = $1 if $to   =~ s{~(\w+)/}{};
 
-       my $f_dir = home_dir($1) if $from =~ s/~(\w+)//;
-       my $t_dir = home_dir($1) if $to   =~ s/~(\w+)//;
-
-       warn "send_file $f_dir $from -> $t_dir $to\n";
+       warn "send_file $f_uid $from -> $t_uid $to\n";
 
-       my $t_basedir = $t_dir . $to;
-       $t_basedir =~ s{/[^/]+$}{};
-       make_path $t_basedir unless -d $t_basedir;
-       link $f_dir . $from => $t_dir . $to;
-
-       print "send#$work#", -s $t_dir.$to, "#$!\n";
+       $api->send_file( $f_uid => $from, $t_uid => $to );
+});
 
-       die "ERROR: $!" if $!;
+=head2 rename_file ~u2001/old.txt#new.txt
 
-       return "send_file $f_dir $from -> $t_dir $to";
-});
+=cut
 
 $worker->register_function( rename_file => sub {
        my ($job) = @_;
@@ -52,55 +54,53 @@ $worker->register_function( rename_file => sub {
        warn "# rename_file [$work]\n";
 
        my ( $from, $to ) = split(/#/,$work,2);
+       my $login = $1 if $from =~ s{~(\w+)/}{};
+       $api->rename_file( $login, $from, $to );
+});
 
-       my $dir = home_dir($1) if $from =~ s/~(\w+)//;
-
-       warn "rename_file $dir $from -> $to\n";
+=head2 delete ~u2001/file_or_dir
 
-       rename $dir . $from => $dir . $to;
+=cut
 
-       print "rename#$work#", -s $dir.$to, "#$!\n";
+$worker->register_function( delete => sub {
+       my ($job) = @_;
+       my $work = $job->arg;
+       chomp $work;
+       warn "# delete [$work]\n";
 
-       die "ERROR: $!" if $!;
+       my $login = $1 if $work =~ s{~(\w+)}{};
+       $api->delete( $login, $work );
 
-       return "rename_file $dir $from -> $to";
 });
 
-$worker->register_function( delete => sub {
+=head2 file_size ~u2001/foo.txt
+
+=cut
+
+$worker->register_function( file_size => sub {
        my ($job) = @_;
        my $work = $job->arg;
        chomp $work;
-       warn "# delete [$work]\n";
+       warn "# file_size [$work]\n";
 
-       my $dir = home_dir($1) if $work =~ s/~(\w+)//;
-       my $login = $1;
-       my $full = $dir . $work;
-
-       if ( -d $full ) {
-               print "delete_tree#$work\n";
-               warn "remove_tree $full\n";
-               remove_tree $full;
-       } else {
-               print "delete_file#$work#", -s $full, "\n";
-               warn "unlink $full\n";
-               unlink $full;
-       }
-       if ( $! ) {
-               warn "ERROR: $!";
-               return "$full: $!\n";
-       } else {
-               return "$full: OK\n";
-       }
+       my $login = $1 if $work =~ s{~(\w+)}{};
+       $api->file_size( $login, $work );
+});
+
+=head2 user_usage u2001
 
+=cut
+
+$worker->register_function( user_usage => sub {
+       my ($job) = @_;
+       my $work = $job->arg;
+       chomp $work;
+       warn "# usage [$work]\n";
+       my $usage = $api->usage( $work );
+       return $usage->{_usage};
 });
 
 warn "$0 pid $$ waitng for jobs\n";
 
-if ( $ENV{SLICE} ) {
-       chroot $ENV{SLICE} || die "can't chroot $ENV{SLICE}: $!";
-} else {
-       warn "WARNING: not running under chroot SLICE=/hostname/sx\n";
-}
-
 $worker->work while 1;