chanakya has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
I have a bunch of scripts which monitor incoming email, process them and load parsed data into Mysql.
I'm demonizing the scripts, in order to process the incoming emails at a faster rate. I have implemented
Proc::Daemon to achieve this. All is well till now.
I'm planning to add modes (like normal mode or daemon mode) to the script, so that I can execute
scripts in daemon mode via the cron, and use the normal mode if I want to troubleshoot or manually
load some emails.
In my code below I have added the daemon mode via the command line options.
Here I need your suggestion on how can i simplify the code and also remove duplicated code. The code shown is a sample script, and the code for some of the scripts is long.
Here's my two questions and seeking suggestions to improve the code
* How can avoid code duplication
* How can I add start/stop commands for the script. thank you for your time
I have a bunch of scripts which monitor incoming email, process them and load parsed data into Mysql.
I'm demonizing the scripts, in order to process the incoming emails at a faster rate. I have implemented
Proc::Daemon to achieve this. All is well till now.
I'm planning to add modes (like normal mode or daemon mode) to the script, so that I can execute
scripts in daemon mode via the cron, and use the normal mode if I want to troubleshoot or manually
load some emails.
In my code below I have added the daemon mode via the command line options.
Here I need your suggestion on how can i simplify the code and also remove duplicated code. The code shown is a sample script, and the code for some of the scripts is long.
The above code works perfectly in the daemon mode and normal mode.#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Proc::Daemon; use Log::Log4perl qw(get_logger :easy :levels); Use MysqlUtils; use vars qw($opt_daemon, $opt_mailid); GetOptions( "daemon|d" => \$opt_daemon, "mailid|a=s" => \$opt_mailid, "help|h" => \&usage, ) || usage(); if(!$opt_mailid){ $opt_mailid = shift || usage(); } chomp($opt_mailid); &usage if( (!defined($opt_mailid)) && (@ARGV==0) ); if(scalar(@ARGV) > 0){ if ( (!defined ($opt_mailid)) || (@ARGV > 0) ) { usage(); die("Invalid Argument passed"); } } if($opt_daemon) { ##Inits print STDOUT "Daemon mode \n"; Proc::Daemon::Init; Log::Log4perl->init_once("$ENV{CONF}" . "/log4perl.conf"); my @mail_ids = qw(200909101 200909102 200909103); while (1) { foreach my $opt_mailid (@mail_ids) { my ($log) = Log::Log4perl::get_logger('main'); my $data = get_data('mail_id' => $opt_mailid, 'file_path' => " +/spool/emails"); ##connect db MysqlUtils::connect(); eval { MysqlUtils::insert_record($data); }; if($@) { $log->error("unable to insert data"); } } } } else { print STDOUT "Normal mode\n"; my ($log) = Log::Log4perl::get_logger('main'); my $data = get_data('mail_id' => $opt_mailid, 'file_path' => "/spo +ol/emails"); ##connect db MysqlUtils::connect(); eval { MysqlUtils::insert_record($data); }; if($@) { $log->error("unable to insert data"); } }
Here's my two questions and seeking suggestions to improve the code
* How can avoid code duplication
* How can I add start/stop commands for the script. thank you for your time
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Executing script in normal/daemon mode and adding start/stop capability
by almut (Canon) on Oct 07, 2009 at 12:35 UTC | |
Re: Executing script in normal/daemon mode and adding start/stop capability
by ELISHEVA (Prior) on Oct 07, 2009 at 11:12 UTC | |
Re: Executing script in normal/daemon mode and adding start/stop capability
by afoken (Chancellor) on Oct 07, 2009 at 22:23 UTC | |
Re: Executing script in normal/daemon mode and adding start/stop capability
by chanakya (Friar) on Oct 08, 2009 at 06:47 UTC |
Back to
Seekers of Perl Wisdom