Changeset 983


Ignore:
Timestamp:
05/17/12 09:58:27 (12 years ago)
Author:
nanardon
Message:
  • add task module to validate automated request
Location:
LATMOS-Accounts
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts/MANIFEST

    r964 r983  
    3838bin/la-sync-list 
    3939bin/la-sync-manager 
     40bin/la-sync-process 
    4041bin/la-warn-expire 
    4142bin/la-sql-edit-form 
     
    9798lib/LATMOS/Accounts/SynchAccess.pm 
    9899lib/LATMOS/Accounts/Synchro.pm 
     100lib/LATMOS/Accounts/Task/Automatedrequest.pm 
    99101lib/LATMOS/Accounts/Task/Basessynchro.pm 
    100102lib/LATMOS/Accounts/Task/Buildlistes.pm 
  • LATMOS-Accounts/bin/la-sql-upgrade.in

    r981 r983  
    180180            delete from accreq_attributes_list 
    181181            } 
     182        ], 
     183    }, 
     184    { 
     185        ver => 7, 
     186        sql => [ 
     187            q{ 
     188            ALTER TABLE request ADD COLUMN automated boolean NOT NULL DEFAULT false; 
     189            }, 
     190            q{ 
     191            ALTER TABLE request ADD COLUMN objrev bigint; 
     192            }, 
    182193        ], 
    183194    }, 
  • LATMOS-Accounts/bin/la-sync-manager

    r915 r983  
    1717    'test'         => \my $test, 
    1818    'wait=i'       => \my $wait, 
    19     'r|run=s'      => \my $run, 
    2019) or pod2usage(); 
    2120 
     
    2827    exit(1); 
    2928}; 
    30  
    31 if ($run) { 
    32     my $res = $syncm->process_module($run); 
    33     exit($res ? 0 : 1); 
    34 } 
    3529 
    3630la_set_log( 
  • LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm

    r959 r983  
    423423} 
    424424 
     425sub list_auto_pending_requests { 
     426    my ($self) = @_; 
     427 
     428    my $sth = $self->db->prepare(q{ 
     429        select id from request 
     430        where done is null 
     431            and apply < now() 
     432            and automated = true 
     433        order by apply 
     434    }); 
     435    $sth->execute; 
     436    my @ids; 
     437    while (my $res = $sth->fetchrow_hashref) { 
     438        push(@ids, $res->{id}); 
     439    } 
     440 
     441    @ids 
     442} 
     443 
    4254441; 
    426445 
  • LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/DataRequest.pm

    r971 r983  
    135135} 
    136136 
     137sub objrev { 
     138    my ($self) = @_; 
     139 
     140    return ($self->_infos || {})->{objrev}; 
     141} 
     142 
    137143sub register { 
    138     my ($self, $user, $apply, %param) = @_; 
    139  
    140     if (!$self->is_for_new_object() 
    141         && !$self->oobject) { 
    142         la_log(LA_ERR, "An object type %s is required", $self->otype); 
    143         return; 
     144    my ($self, $options, %param) = @_; 
     145 
     146    my $user = $options->{user}; 
     147    my $apply = $options->{apply}; 
     148    my $auto = $options->{auto}; 
     149 
     150    my $rev; 
     151 
     152    if (!$self->is_for_new_object()) { 
     153        if ($self->oobject) { 
     154            $rev = $self->oobject->get_attributes('rev'); 
     155        } else { 
     156            la_log(LA_ERR, "An object type %s is required", $self->otype); 
     157            return; 
     158        } 
    144159    } 
    145160 
    146161    my $sth_req = $self->base->db->prepare(q{ 
    147         INSERT into request (name, object, "user", apply) 
    148         values (?,?,?,?) 
     162        INSERT into request (name, object, "user", apply, automated, objrev) 
     163        values (?,?,?,?,?,?) 
    149164        }); 
    150165    $apply ||= scalar(localtime); 
    151     $sth_req->execute($self->accreq->id, $self->object_name, $user, $apply) or return; 
     166    $sth_req->execute($self->accreq->id, $self->object_name, $user, $apply, 
     167        $auto ? 1 : 0, $rev) or return; 
    152168 
    153169    my $newid = $self->base->db->last_insert_id( 
     
    336352} 
    337353 
     354sub unset_auto { 
     355    my ($self) = @_; 
     356 
     357    my $sth = $self->accreq->db->prepare(q{ 
     358        update request  set automated = false where id = ? 
     359    }); 
     360    return $sth->execute($self->ar_id); 
     361} 
     362 
     363sub auto_apply_to_object { 
     364    my ($self, $comment) = @_; 
     365 
     366    if (!$self->is_for_new_object) { 
     367        if (my $obj = $self->oobject) { 
     368            if ($obj->get_attributes('rev') != $self->objrev) { 
     369                $self->base->log(LA_WARN, 
     370                    'Object %s has been modified, set request as non 
     371                    automated'); 
     372 
     373                $self->unset_auto; 
     374                return 1; # The automatic action "succeed" 
     375            } 
     376        } else { 
     377            $self->base->log('Cannot apply to non existing object %s', 
     378                $self->object_name); 
     379            return; 
     380        } 
     381    } 
     382    $self->apply_to_object($comment); 
     383} 
     384 
    338385sub apply_to_object { 
    339386    my ($self, $comment, %attrs) = @_; 
     
    341388    my %newvalues = $self->_prepare_attrs(%attrs); 
    342389 
    343     my $obj = $self->oobject or return; 
    344  
    345     if (defined($obj->set_c_fields(%newvalues)) && 
    346         $self->_register_applied($comment)) { 
    347         $self->base->log( 
    348             LA_INFO, 
    349             'DataRequest id=%d applied to object %s/%s', 
    350             $self->ar_id, 
     390    if ($self->is_for_new_object) { 
     391        if ($self->base->create_object( 
    351392            $self->otype, 
    352             $obj->id, 
    353         ); 
    354         $self->base->commit; 
    355         return 1; 
    356     } else { 
    357         $self->base->log( 
    358             LA_ERR, 
    359             'Error applying DataRequest id=%d to object %s/%s: %s', 
    360             $self->ar_id, 
    361             $self->otype, 
    362             $obj->id, 
    363             $self->base->db->errstr, 
    364         ); 
    365         $self->base->rollback; 
    366         return; 
     393            $attrs{_name}, 
     394            %attrs 
     395        )) { 
     396            $self->_register_applied($comment); 
     397            return 1; 
     398        } else { 
     399            $self->base->log( 
     400                LA_ERR, 
     401                'Error applying DataRequest id=%d to create object %s/%s: %s', 
     402                $self->ar_id, 
     403                $self->otype, 
     404                $attrs{_name}, 
     405                $self->base->db->errstr, 
     406            ); 
     407            return; 
     408        } 
     409    } else { 
     410        my $obj = $self->oobject or return; 
     411 
     412        if (defined($obj->set_c_fields(%newvalues)) && 
     413            $self->_register_applied($comment)) { 
     414            $self->base->log( 
     415                LA_INFO, 
     416                'DataRequest id=%d applied to object %s/%s', 
     417                $self->ar_id, 
     418                $self->otype, 
     419                $obj->id, 
     420            ); 
     421            return 1; 
     422        } else { 
     423            $self->base->log( 
     424                LA_ERR, 
     425                'Error applying DataRequest id=%d to object %s/%s: %s', 
     426                $self->ar_id, 
     427                $self->otype, 
     428                $obj->id, 
     429                $self->base->db->errstr, 
     430            ); 
     431            $self->base->rollback; 
     432            return; 
     433        } 
    367434    } 
    368435} 
  • LATMOS-Accounts/lib/LATMOS/Accounts/Task.pm

    r861 r983  
    2626    my ($self, $baserev, $syncm) = @_; 
    2727 
     28    $baserev ||= -1; 
    2829    la_log LA_DEBUG, "Comparing db: %d <=> %d", $syncm->dbrev, $baserev; 
    2930    if ($syncm->dbrev > $baserev) { 
Note: See TracChangeset for help on using the changeset viewer.