Merge branch 'attr'
[cloudstore.git] / gearman / send_file.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 =head1 NAME
6
7 Filesystem Gearman Worker
8
9 =cut
10
11
12 use Data::Dump qw(dump);
13
14 use lib '/srv/cloudstore/lib';
15 use CloudStore::API;
16
17 my $api = CloudStore::API->new('s1');
18
19 use Gearman::Worker;
20 my $worker = Gearman::Worker->new;
21 $worker->job_servers('127.0.0.1:4730');
22
23 open(my $log, '>>', '/rsync1/s1/log/send_file.log');
24 select($log); $|++;
25
26 =head2 send_file ~u2001/from.txt#~u2003/dir/new to.txt
27
28 =cut
29
30 $worker->register_function( send_file => sub {
31         my ($job) = @_;
32
33         my $work = $job->arg;
34         chomp $work;
35         warn "# send_file [$work]\n";
36
37         my ( $from, $to ) = split(/#/,$work,2);
38         
39         my $f_uid = $1 if $from =~ s{~(\w+)/}{};
40         my $t_uid = $1 if $to   =~ s{~(\w+)/}{};
41
42         warn "send_file $f_uid $from -> $t_uid $to\n";
43
44         $api->send_file( $f_uid => $from, $t_uid => $to );
45 });
46
47 =head2 rename_file ~u2001/old.txt#new.txt
48
49 =cut
50
51 $worker->register_function( rename_file => sub {
52         my ($job) = @_;
53
54         my $work = $job->arg;
55         chomp $work;
56         warn "# rename_file [$work]\n";
57
58         my ( $from, $to ) = split(/#/,$work,2);
59         my $login = $1 if $from =~ s{~(\w+)/}{};
60         $api->rename_file( $login, $from, $to );
61 });
62
63 =head2 delete ~u2001/file_or_dir
64
65 =cut
66
67 $worker->register_function( delete => sub {
68         my ($job) = @_;
69         my $work = $job->arg;
70         chomp $work;
71         warn "# delete [$work]\n";
72
73         my $login = $1 if $work =~ s{~(\w+)}{};
74         $api->delete( $login, $work );
75
76 });
77
78 =head2 file_size ~u2001/foo.txt
79
80 =cut
81
82 $worker->register_function( file_size => sub {
83         my ($job) = @_;
84         my $work = $job->arg;
85         chomp $work;
86         warn "# file_size [$work]\n";
87
88         my $login = $1 if $work =~ s{~(\w+)}{};
89         $api->file_size( $login, $work );
90 });
91
92 =head2 user_usage u2001
93
94 =cut
95
96 $worker->register_function( user_usage => sub {
97         my ($job) = @_;
98         my $work = $job->arg;
99         chomp $work;
100         warn "# usage [$work]\n";
101         my $usage = $api->usage( $work );
102         return $usage->{_usage};
103 });
104
105 warn "$0 pid $$ waitng for jobs\n";
106
107 if ( $ENV{SLICE} ) {
108         chroot $ENV{SLICE} || die "can't chroot $ENV{SLICE}: $!";
109 } else {
110         warn "WARNING: not running under chroot SLICE=/hostname/sx\n";
111 }
112
113 $worker->work while 1;
114