[rt-users] Custom Scrip Taking Too Much Time

Varun varun.vyas at elitecore.com
Fri Nov 20 03:38:39 EST 2009


Hello All

 

I have RT 3.6.3 installed with fastcgi implemented. I have one problem with
my one of custom scrip which we have placed on closure of ticket for 3 of
our queues. This scrip inserts username and password of the requestor into
our one table called Feedback master and then sends a mail to that user for
feedback from the user side. In short it sends a feedback form to the user
on closing of ticket and inserts that username and password in our database
for our future reference. But due to this, delay in closing of ticket has
increased tremendously and it takes 3-4 minutes for a ticket to gets closed
(which includes finding the email address of requestor from custom fields
and generating a random password for that user and inserting that details in
our table and then sending a feedback form to that user). I have attached my
scrip. Please any one has any idea how I can optimize this scrip and
decrease the time to close ticket below 1 min. 

 

1) Scrip For Finding Preferred Email Address Of User And Inserting it into
Database:

 

Script Condition :

 

# User defined condition for invoking feedback related scrips.

# This condition ensures the ticket is closed.

 

 if(($self->TransactionObj->Field eq 'Status') and

    ($self->TransactionObj->NewValue() eq 'closed)) {

        return(1);

   }else {

        return(undef);

   }

 

Script Action :

 

use lib qw(/opt/rt3/lib);

use List::Util qw(first);

use POSIX;

use Data::Dumper;

use RT::FeedbackUser;

 

         my $queueObj=$self->TicketObj->QueueObj;

 

## add the valid queue ids here

         my @validQueueIds=(21,301,601);

         my $qid=$queueObj->Id;

         my $match=first{/$qid/}@validQueueIds ;

 

        if(defined($match)){

#       PROCEED FURTHER TO CREATE USER

            my $fbUserObj=RT::FeedbackUser->new($queueObj->CurrentUser);

                my @allRequestors=();

            my $newusername;

            my $newpwd;

            my $tickId=$self->TicketObj->Id;

                my $disabled=0;

                        my $dtCreated=strftime('%Y-%m-%d
%H:%M:%S',localtime);

                my $dtModified=strftime('%Y-%m-%d %H:%M:%S',localtime);

                my $cfObj=RT::CustomField->new($RT::SystemUser);

                    my
$cfvalues=RT::ObjectCustomFieldValues->new($RT::SystemUser);

#       CHECK for PREFERRED EMAIL ADDRESS

                my $preferredAddr='';

 

                        $cfObj->Load('Preferred Email');

                        $RT::Logger->debug("[CreateFBUser/Commit]: CF id=
".$cfObj->Id.",ticket id=".$tickId);

                        $cfvalues->UnLimit();

                               while(my $cf=$cfvalues->Next)

                                    {

                                      if( $cf->ObjectId == $tickId &&
$cf->CustomField == $cfObj->Id)                    

                                              {

                                

                                                $preferredAddr=
$cf->Content;

 
$RT::Logger->debug("[CreateFBUser/Commit]: preferred addr=".$preferredAddr);

                                             }

                                    }

                                                            

               ####### remove leading spaces ########

                $preferredAddr=~ s/^\s+// ;

                #######  remove trailing spaces #######

                $preferredAddr=~ s/\s+$//;

 

                

               

                   if(defined($preferredAddr) && $preferredAddr ne '' &&
$preferredAddr =~
/^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ){

#       THERE IS AN ADDITIONAL PREFERRED EMAIL ADDRESS SO CREATE USER FOR
THAT

                        $newpwd=generatePwd();

 
$RT::Logger->debug("[CreateFBUser/Commit]:Preferred
Username:".$preferredAddr.", Password=".$newpwd);

                       my($res,$msg)=  $fbUserObj->Create(

                        UserName => $preferredAddr,

                        Password => $newpwd,

                        TicketId => $tickId,

                        Disabled => $disabled,

                        DateCreated => $dtCreated,

                        DateModified => $dtModified,

                        );

 
$RT::Logger->debug("[CreateFBUser/Commit]: result status =".$res.",
ResultMessage =".$msg);

                }else{

                        $RT::Logger->debug("[CreateFBUser/Commit]: Creating
normal users");

 

 
push(@allRequestors,$self->TicketObj->Requestors->MemberEmailAddresses);

 

                  foreach(@allRequestors){

                                    $newusername=$_;

                         $newpwd='';

                        unless($newusername=~ m/organization.com/){

 

                                $newpwd=generatePwd();

 

 
$RT::Logger->debug("[CreateFBUser/Commit]:Username:".$newusername.",
Password=".$newpwd);

                                    my($res,$msg)=  $fbUserObj->Create(

                                UserName => $newusername,

                                Password => $newpwd,

                                TicketId => $tickId,

                                Disabled => $disabled,

                                DateCreated => $dtCreated,

                                DateModified => $dtModified,

                                );

 
$RT::Logger->debug("[CreateFBUser/Commit]: result status =".$res.",
ResultMessage =".$msg);

                        } # end of unless

                   } # end of foreach

                }  # end of else

 

 }else{

        $RT::Logger->debug("[CreateFBUser/Commit]:Ticket not matched to
given queues so no feedback user created ");

 }

 

sub generatePwd {

# GENERATE A PASSWORD

        my $newpwd;

        my $pwdLen=6;

            my @charset = (('A'..'Z'), ('a'..'z'), (0..9));

        my $range = $#charset + 1;

        my $randomHr=int(rand($pwdLen));

        my $randomMin=int(rand($pwdLen));

 

 

      for (0..$pwdLen-1) {

              $newpwd.= $charset[int(rand($range))];

               # Add timestamp to new password to ensure it remains unique

              $newpwd.=($_== $randomHr)?strftime('%H',localtime):'';

              $newpwd.=($_==$randomMin)?strftime('%M',localtime):'';

     }

     return $newpwd;

}

 

 

2) Scrip For Sending Mail:

 

use lib qw(/opt/rt3/lib);

use List::Util qw(first);

require MIME::Lite;

require MIME::Lite::HTML;

use RT::FeedbackUser;

use RT::FeedbackUsers;

 

my $queueObj=$self->TicketObj->QueueObj;

my $user=RT::FeedbackUser->new($RT::SystemUser);

my @allrequestors=$self->TicketObj->Requestors->MemberEmailAddresses;

 my $cfObj=RT::CustomField->new($RT::SystemUser);

 my $cfvalues=RT::ObjectCustomFieldValues->new($RT::SystemUser);

 my $preferredAddr='';

 my $accessCode;

 

my @validQueueIds=(21,301,601);

my $qid=$queueObj->Id;

my $match=first{/$qid/}@validQueueIds ;

if(!defined($match)){

            return;

}

 

# CHECK for PREFERRED EMAIL ADDRESS

 

 $cfObj->Load('Preferred Email');

 

 $cfvalues->UnLimit();

 while(my $cf=$cfvalues->Next){

 

    if($cf->ObjectId == $self->TicketObj->Id && $cf->CustomField ==
$cfObj->Id){

                  $preferredAddr=$cf->Content;

 

                  ####### remove leading spaces ########

                  $preferredAddr=~ s/^\s+// ;

                  #######  remove trailing spaces #######

                  $preferredAddr=~ s/\s+$//;

 

                  $RT::Logger->debug("[SendLiteMail]: preferred
addr=".$preferredAddr);

        }

 }

 

 

 if(defined($preferredAddr) && $preferredAddr ne '' && $preferredAddr =~
/^\w+(\-\w+)*(\.\w+(\-\w+)*)*@\w+(\-\w+)*(\.\w+(\-\w+)*)+$/ ){

 

        $user->LoadByUserNameAndTicket( UserName => $preferredAddr ,
TicketId => $self->TicketObj->Id , Disabled => 0 );

 

            $RT::Logger->debug("[SendLiteMail]: Loaded preferred
feedbackuser,Username=".$user->UserName.", Password=".$user->Password);

        $RT::Logger->debug("[SendLiteMail]: About to send integrated mail to
preferred addr ");

        $accessCode= $user->Password;

        my $mailHTML = new MIME::Lite::HTML(To => $user->UserName,

                              From =>'admin at rt.com',

                              Subject => 'Please send us feedback for
ticket:'.$user->TicketId);

 

        my $MIMEmail
=$mailHTML->parse("http://mycompanyname.com/index.jsp?activationcode=$access
Code ");

 

         $MIMEmail->send;

            $RT::Logger->debug("[SendLiteMail]: mail sent ");

}else{

 

 foreach(@allrequestors){

            my $name=$_;

            if($name=~ m/organization.com/){

                        # DONT LOAD THIS USER AS IT WONT EXIST IN THE DB

                        $RT::Logger->debug("[SendLiteMail]: Not mailing to
internal user");

            }else{

#                      $RT::Logger->debug("[SendLiteMail]:
Requestor=".$name.",ticket=".$user->TicketId);

 

#                      $user->LoadByUserNameAndTicket( UserName => $name ,
TicketId => $user->TicketId, Disabled => 0 );

            $RT::Logger->debug("[SendLiteMail]:
Requestor=".$name.",ticket=".$self->TicketObj->Id);

 

            $user->LoadByUserNameAndTicket( UserName => $name , TicketId =>
$self->TicketObj->Id, Disabled => 0 );

 

                        $RT::Logger->debug("[SendLiteMail]: Loaded
feedbackuser, Username=".$user->UserName.", Password=".$user->Password);

            $RT::Logger->debug("[SendLiteMail]: About to send integrated
mail");

 

                        $accessCode= $user->Password;

 

                        my $mailHTML = new MIME::Lite::HTML(To =>
$user->UserName,

                          From =>' admin at rt.com ',

                              Subject => 'Please send us feedback for
ticket:'.$user->TicketId);

 

            my $MIMEmail
=$mailHTML->parse("http://mycompanyname.com/index.jsp?activationcode=$access
Code ");

 

         $MIMEmail->send;

 

                        $RT::Logger->debug("[SendLiteMail]: mail sent ");

            }

 }

}# end of if

 

 

 

 

Please any help is highly appreciated. Waiting a positive reply.

 

Thanks & Regards

Varun Vyas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20091120/10057914/attachment.htm>


More information about the rt-users mailing list