User Tools

Site Tools


DB agent code sample

#--------------------
#--- CONTROLLER 1 ---
#--------------------
 
package DBagent::Controller;
 
use utf8;
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Util qw(md5_sum dumper quote encode url_unescape);
use Mojo::JSON qw(encode_json decode_json false true);
use File::Basename;
use Filesys::Df;
use File::stat;
use POSIX;
 
...
 
sub db_dump {
    my $self = shift;
 
    my $name = $self->req->param('name');
    my $store = $self->req->param('store');
    my $login = $self->req->param('login');
    my $password = $self->req->param('password');
    my $cb = $self->req->param('cb');
    my $job_id = $self->req->param('job_id');
    my $magic = $self->req->param('magic');
 
    return $self->render(json => { success => 0 }) unless $name;
 
    return $self->render(json => { success => 0 }) unless $store;
    return $self->render(json => { success => 0 }) unless $login;
    return $self->render(json => { success => 0 }) unless $password;
 
    return $self->render(json => { success => 0 }) unless $cb;
    return $self->render(json => { success => 0 }) unless $job_id;
    return $self->render(json => { success => 0 }) unless $magic;
 
 
    my $sub = Mojo::IOLoop::Subprocess->new;
 
    $sub->run(
        sub {
            my $sub = shift;
            my $app = $self->app;
            my $dir = $app->app->config('datadir');
 
            $app->log->info("--- The dump begins name=$name job=$job_id");
            my $file = $app->agent->db_dump($name, $dir);
 
            unless ($file) {
                $app->log->info("--- The dump unsuccessful name=$name job_id=$job_id");
                return undef;
            }
            $app->log->info("--- The dump is done name=$name job_id=$job_id file=$file");
 
            # ---Upload the dump ---
            $app->log->info("--- The upload begins file=$file job_id=$job_id store=$store");
 
            my $store = aStoreI->new($store, $login, $password);
            unless ($store->alive) {
                unlink $file;
                $app->log->info("--- The upload unsuccessful because store is dead store=$store job_id=$job_id");
                return undef;
            }
            my $res = $store->data_put($file);
 
            my $size = stat($file)->size;
            my $res_size = $res->{list}->[0]->{size} || 0;
            my $res_file = $res->{list}->[0]->{size} || '';
 
            $app->log->info("--- The upload size is $res_size res_file=$res_file job_id=$job_id");
 
            unless ($res_size == $size) {
                $app->log->info("--- The upload unsuccessful name=$name job_id=$job_id file=$file");
                unlink $file;
                $store->data_delete($file);
                return undef;
            }
            $app->log->info("--- The upload is done name=$name job_id=$job_id file=$file");
            unlink $file;
            $name;
        },
        sub {
            my ($sub, $err, @results) = @_;
        }
    );
    $self->render(json => { success => 1 });
}
 
sub db_restore {
    my $self = shift;
 
    my $store = $self->req->param('store');
    my $login = $self->req->param('login');
    my $password = $self->req->param('password');
    my $file = $self->req->param('file');
    my $new_name = $self->req->param('new_name');
 
    my $sub = Mojo::IOLoop::Subprocess->new;
 
    $sub->run(
        sub {
            my $sub = shift;
            my $app = $self->app;
 
            my $cb = $self->req->param('cb');
            my $job_id = $self->req->param('job_id');
            my $magic = $self->req->param('magic');
 
            return $self->render(json => { success => 0 }) unless $store;
            return $self->render(json => { success => 0 }) unless $login;
            return $self->render(json => { success => 0 }) unless $password;
            return $self->render(json => { success => 0 }) unless $file;
            return $self->render(json => { success => 0 }) unless $new_name;
 
            return $self->render(json => { success => 0 }) unless $cb;
            return $self->render(json => { success => 0 }) unless $job_id;
            return $self->render(json => { success => 0 }) unless $magic;
 
            $self->app->log->info("---The download begins file=$file job=$job_id");
 
            my $st = aStoreI->new($store, $login, $password);
            unless ($st->alive) {
                $self->app->log->info("---The download unsuccessful because store is dead store=$store job_id=$job_id");
                return undef;
            }
 
            my $res = $st->data_get($file, $app->config('datadir'));
            $app->log->info("---The download done store=$store job_id=$job_id file=$res");
 
            $app->log->info("---The restore begins job_id=$job_id new_name=$new_name file=$res");
            my $restore = $app->agent->db_restore($res, $new_name);
            $app->log->info("---The restore done job_id=$job_id new_name=$new_name file=$res");
 
        },
        sub {
            my ($sub, $err, @results) = @_;
        }
    );
    $self->render(json => { success => 1 });
}
 
1;

First PagePrevious PageBack to overviewNext PageLast Page