I have a Perl script that is making a call to the AWS command line to upload a file to S3. It uses Log4Perl to log information about the upload. When the upload is small, a few seconds, the logging works fine -- but when the file is bigger and takes a few minutes then nothing gets logged. The file gets uploaded fine, but the log file is not created.
This is how I'm doing it:
#get start time
my #dt = localtime;
my $TransferStartDate = strftime("%Y-%m-%d", #dt);
my $TransferStartTime = strftime("%H:%M:%S", #dt);
my $aws_command = "/path/to/aws/bin/aws s3 mv $local_file s3://" . $s3_bucket;
my $aws_status = system( $aws_command);
#get stop time
#dt = localtime;
my $TransferEndDate = strftime("%Y-%m-%d", #dt);
my $TransferEndTime = strftime("%H:%M:%S", #dt);
my $log_conf = "
log4perl.rootLogger = $log_level, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = /path/to/logs/aws_s3_log-$TransferEndDate.txt
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
";
Log::Log4perl::init(\$log_conf);
my $logger = Log::Log4perl->get_logger();
$logger->info( "Transfer command = $aws_command" );
$logger->info( "Transfer start = $TransferStartDate $TransferStartTime" );
$logger->info( "Transfer end = $TransferEndDate $TransferEndTime" );
#check return code
if ( $aws_status == 0 )
{
$logger->info( "Transfer completed successfully");
}
elsif ( $aws_status == 1 )
{
$errstring = "s3 transfer failed status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 2 )
{
$errstring = "Syntax error on AWS S3 command status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 130 )
{
$errstring = "transfer interrupted by Ctrl-C or other SIGINT status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 255 )
{
$errstring = "Command failed status = $aws_status";
$logger->error( $errstring );
}
elsif ( $aws_status == 512 )
{
$errstring = "s3 command formatted incorrectly status = $aws_status";
$logger->error( $errstring );
}
else
{
$errstring = "Error with AWS s3 command status = $aws_status";
$logger->error( $errstring );
}
Any ideas what I am doing wrong? Using system to make the call should cause the Perl script to stop until the upload completes, right?
Since you're logging to file, i would add a $|=1; in the very beginning of the script.
Also i think you should instantiate logger earlier, before launching aws_command, and launch it using ``:
$|=1;
my #dt = localtime;
my $TransferStartDate = strftime("%Y-%m-%d", #dt);
my $TransferStartTime = strftime("%H:%M:%S", #dt);
my $log_conf = "
log4perl.rootLogger = $log_level, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = /path/to/logs/aws_s3_log-$TransferStartDate.txt
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
";
Log::Log4perl::init(\$log_conf);
my $logger = Log::Log4perl->get_logger();
my $aws_command = "/path/to/aws/bin/aws s3 mv $local_file s3://" . $s3_bucket;
my $aws_output = `$aws_command`;
my $aws_status = $?;
#get stop time
#dt = localtime;
my $TransferEndDate = strftime("%Y-%m-%d", #dt);
my $TransferEndTime = strftime("%H:%M:%S", #dt);
$logger->info( "Transfer command = $aws_command" );
$logger->info( "Transfer result = $aws_result" );
Not sure it'll solve your issue, but might help you narrow your diagnostics.
Related
I'm experiencing early termination of the AkkaSystem after first record being read ,entering into the Dead letter without executing the task for all the records
I have 10records in my file , 2 of the records has the matching filename to be pushed to s3.
what could be going wrong here. Please suggest
Sample file record:
xxxxx,ABC,2019-05-10 00:11:00
yyyyyy,XYZ,2019-05-10 00:41:00
import akka.actor.{Actor, ActorSystem, Props}
import scala.io.Source
import scala.sys.process._
class HelloActor extends Actor {
def receive: Receive = {
case line: String => {
// print("Ahshan"+line)
val row = line.split ( "," )
val stdout = new StringBuilder
val stderr = new StringBuilder
val status = Seq ( "/bin/sh", "-c", "ls /Users/ahshan.md/Downloads/".concat ( row ( 2 ).substring ( 0, 10 ) ).concat ( "/*" ).concat ( row ( 0 ) ).concat ( "*" ) ) ! ProcessLogger ( stdout append _, stderr append _ )
// println(status)
// println("stdout: " + stdout)
// println("stderr: " + stderr)
if (status == 0) {
println ( "/bin/sh", "-c", "aws s3 cp ".concat ( stdout.mkString ).concat ( " " ).concat ( "s3://ahshan/".concat ( row ( 1 ) ).concat ( "/" ).concat ( row ( 0 ) ).concat ( ".email" ) ) )
}
else {
// println ( "File Not Found: " + row ( 0 ), row ( 1 ), row ( 2 ).substring ( 0, 10 ) )
println ( "stderr: " + stderr )
}
}
case "finished" => println ( "Hello Ahshan" )
case _ => println ( "Exiting" )
}
}
object AkkaHelloWorld extends App {
// an actor needs an ActorSystem
val system = ActorSystem ( "HelloSystem" )
// create and start the actor
val helloActor = system.actorOf ( Props [HelloActor], name = "helloActor" )
try {
val filename = "/Users/ahshan.md/Downloads/test.txt"
for (line <- Source.fromFile ( filename ).getLines) {
helloActor ! line
}
}
finally {
system.terminate ()
}
}
The ActorSystem and the actors run concurrently with your main thread, sending a message to an actor is async and the sending thread immediately continues, it does not wait for the actor to process the message.
This means that in your code the main thread fires off each line to the actor as fast as it can and then terminates the actor system. In this example it could make sense to move the termination logic into the actor and let the actor terminate the system when it has completed.
That will only work as long as your application is a single actor though, as soon as you add another actor you will have to revisit how you terminate the system.
As an additional note using the process API in Scala like that will block until the process has completed, calling blocking code can have bad consequences, read this section of the docs for more details: https://doc.akka.io/docs/akka/current/typed/dispatchers.html#blocking-needs-careful-management
My script is working so far to open a remote FTP connection, change directory, and download a file. My last two steps would be to delete the remove file once it's fully downloaded and then close the connection. ACF documentation (and cfdocs) seems to have very little information on this. Here's what I have so far:
ftpConnection = ftpService.open(
action = 'open',
connection = variables.ftpConnectionName,
server = variables.ftpServerName,
username = '***************',
password = '***************',
secure='true');
if( ftpConnection.getPrefix().succeeded ){
fileList = ftpService.listdir(directory = variables.ftpPath, connection= variables.ftpConnectionName, name='pendingFiles', stopOnError='true').getResult();
if( fileList.recordCount ){
changeFtpConnectionDir = ftpService.changeDir(
connection = variables.ftpConnectionName,
directory = variables.ftpPath);
getFtpConnection = ftpService.getFile(
connection = variables.ftpConnectionName,
remoteFile = fileList.name,
localFile = local.localPath & fileList.name,
failIfExists = false,
timeout = 3000
);
deleteRemoteFile = ftpService.remove(
connection = variables.ftpConnectionName,
remoteFile = fileList.name
};
closeFtp = ftpService.close(
connection = variables.ftpConnectionName
);
};
};
Error is thrown on the remoteFile = fileList.name. Since I already changed directory I don't think I need to put the full path here.
I put the entire script up since there doesn't seem to be many resources out there about using the newer ftpServer() functions.
D'oh - my issue was a typo:
deleteRemoteFile = ftpService.remove(
connection = variables.ftpConnectionName,
remoteFile = fileList.name
);// had } instead of )
I'll still leave this up as a resource for ftpService()
ะก++, Embarcadero RAD Studio XE2
I need connect to a ::6100 with TIdTCPClient through http-proxy. So I wrote this code:
m_pClient = new TIdTCPClient( NULL );
m_pClient->Host = m_sServerAddress.c_str();
m_pClient->Port = StrToInt( m_sServerPort.c_str() );
m_pClient->ConnectTimeout = 5000;
m_pClient->ReadTimeout = 5000;
if ( m_bUseProxy == true )
{
m_pIdIOHandlerStack = new TIdIOHandlerStack( NULL );
m_pIdIOHandlerStack->TransparentProxy = new TIdConnectThroughHttpProxy( m_pIdIOHandlerStack );
m_pIdIOHandlerStack->TransparentProxy->Host = m_sProxyHost;
m_pIdIOHandlerStack->TransparentProxy->Port = m_iProxyPort;
m_pIdIOHandlerStack->TransparentProxy->Enabled = True;
m_pClient->IOHandler = m_pIdIOHandlerStack;
}
else
{
m_pClient->IOHandler = NULL;
}
<other code>
m_pClient->Connect();
I got an exeption "403 forbidden" on "Connect"
proxy: 5.196.0.118::3128
I can connect to this server without proxy or ping it.
I used this proxy server successfully with my browser, but I can't use it for my code.
How can I resolve this problem?
Meaning, they don't have to be distributed. I'm thinking about using memcached or redis for that. Probably the latter one. What I'm concerned about is "we've got to free some memory, so we'll delete this key/value before it expired" thing. But I'm open to other suggestions as well.
tl;dr Use ready-made solution, suggested by developers.
So, I decided not to use memcached for the purpose. Since it's a caching server. I don't see a way to ensure that it doesn't delete my keys because it's out of memory. With, redis that's not an issue as long as maxmemory-policy = noeviction.
There are 3 links I want to share with you. They are basically 3 ways, that I now know, to solve the issue. As long as you have redis >= 2.6.0 that is.
redis >= 2.6.12
If you've got redis >= 2.6.12, you're lucky and can simply use setnx command with its new options ex and nx:
$redis->set($name, <whatever>, array('nx', 'ex' => $ttl));
But we can't just delete the lock in the end, if we are to allow for critical section taking longer then we expected (>= ttl). Consider the following situation:
C1 acquires the lock
lock expires
C2 acquires the lock
C1 deletes C2's lock
For that not to happen we are going to store current timestamp as a value of the lock. Then, knowing that Lua scripts are atomic (see Atomicity of scripts):
$redis->eval('
if redis.call("get", KEYS[1]) == KEYS[2] then
redis.call("del", KEYS[1])
end
', array($name, $now));
However, is it possible for two clients to have equal now values? For that all the above actions should happen within one second and ttl must be equal to 0.
Resulting code:
function get_redis() {
static $redis;
if ( ! $redis) {
$redis = new Redis;
$redis->connect('127.0.0.1');
}
return $redis;
}
function acquire_lock($name, $ttl) {
if ( ! $ttl)
return FALSE;
$redis = get_redis();
$now = time();
$r = $redis->set($name, $now, array('nx', 'ex' => $ttl));
if ( ! $r)
return FALSE;
$lock = new RedisLock($redis, $name, $now);
register_shutdown_function(function() use ($lock) {
$r = $lock->release();
# if ( ! $r) {
# Here we can log the fact that lock has expired too early
# }
});
return $lock;
}
class RedisLock {
var $redis;
var $name;
var $now;
var $released;
function __construct($redis, $name, $now) {
$this->redis = get_redis();
$this->name = $name;
$this->now = $now;
}
function release() {
if ($this->released)
return TRUE;
$r = $this->redis->eval('
if redis.call("get", KEYS[1]) == KEYS[2] then
redis.call("del", KEYS[1])
return 1
else
return 0
end
', array($this->name, $this->now));
if ($r)
$this->released = TRUE;
return $r;
}
}
$l1 = acquire_lock('l1', 4);
var_dump($l1 ? date('H:i:s', $l1->expires_at) : FALSE);
sleep(2);
$l2 = acquire_lock('l1', 4);
var_dump($l2 ? date('H:i:s', $l2->expires_at) : FALSE); # FALSE
sleep(4);
$l3 = acquire_lock('l1', 4);
var_dump($l3 ? date('H:i:s', $l3->expires_at) : FALSE);
expire
The other solution I found here. You simply make the value expire with expire command:
$redis->eval('
local r = redis.call("setnx", ARGV[1], ARGV[2])
if r == 1 then
redis.call("expire", ARGV[1], ARGV[3])
end
', array($name, $now, $ttl));
So, only acquire_lock function changes:
function acquire_lock($name, $ttl) {
if ( ! $ttl)
return FALSE;
$redis = get_redis();
$now = time();
$r = $redis->eval('
local r = redis.call("setnx", ARGV[1], ARGV[2])
if r == 1 then
redis.call("expire", ARGV[1], ARGV[3])
end
return r
', array($name, $now, $ttl));
if ( ! $r)
return FALSE;
$lock = new RedisLock($redis, $name, $now);
register_shutdown_function(function() use ($lock) {
$r = $lock->release();
# if ( ! $r) {
# Here we can log that lock as expired too early
# }
});
return $lock;
}
getset
And the last one is described again in documentation. Marked with "left for historical reasons" note.
This time we store timestamp of the moment when the lock is to expire. We store it with setnx command. If it succeeds, we've acquired the lock. Otherwise, either someone else's holding the lock, or the lock has expired. Be it the latter, we use getset to set new value and if the old value hasn't changed, we've acquired the lock:
$r = $redis->setnx($name, $expires_at);
if ( ! $r) {
$cur_expires_at = $redis->get($name);
if ($cur_expires_at > time())
return FALSE;
$cur_expires_at_2 = $redis->getset($name, $expires_at);
if ($cur_expires_at_2 != $cur_expires_at)
return FALSE;
}
What makes me uncomfortable here is that we seem to have changed someone else's expires_at value, don't we?
On a side note, you can check which redis is it that you're using this way:
function get_redis_version() {
static $redis_version;
if ( ! $redis_version) {
$redis = get_redis();
$info = $redis->info();
$redis_version = $info['redis_version'];
}
return $redis_version;
}
if (version_compare(get_redis_version(), '2.6.12') >= 0) {
...
}
Some debugging functions:
function redis_var_dump($keys) {
foreach (get_redis()->keys($keys) as $key) {
$ttl = get_redis()->ttl($key);
printf("%s: %s%s%s", $key, get_redis()->get($key),
$ttl >= 0 ? sprintf(" (ttl: %s)", $ttl) : '',
nl());
}
}
function nl() {
return PHP_SAPI == 'cli' ? "\n" : '<br>';
}
Experts... I'm creating another question in continuation with my earlier query.... this question is different from earlier request so I thought better to create new thread instead of confusing experts answers.
Below code connects each alias in tnsfile to the database...
1. Is there anyway I can limit each database connection only once and don't allow different alias connecting to the same database again?
I tried using hash but no able to fix this..
use strict;
use warnings;
if /^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/
{
{
$hashref->{$1}="";
}
}
Below regex can select each SID value from file but not able to combine with if...
(/\(CONNECT_DATA\s+=\s+\(SID\s+=\s+(\w+\d+?)(\s+)?\)/)
Sample file (tnsfile.txt)
DB1.UK, DB2.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB1.EU, DB2.CH =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB3.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB3.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB4.UK.US, DB4.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db4))
)
Expected $hashref value:
DB1.UK
DB3.UK
DB4.UK.US
Are you caching your database handles? It sounds like you are.
If so, you just need to create a new hash that relates SID to database handles. Then you only optionally create a new handle if that SID hasn't already been loaded.
In psuedocode:
my %dbh_by_sid;
while (<DATA>) {
my $sid = ...;
my $dbh = $dbh_by_sid{$sid} ||= DBI->connect(...)
or die "DB connect failed: $DBI::errstr";
# ...
}
This way if that particular SID has already been connected to, it will reuse the same handle.
This is far from a refined solution, and it breaks if the format of your tns file changes dramatically, but you could try something like this:
use strict;
my (%tns, %sid, $tns);
open IN, 'tnsfile.txt' or die;
while (<IN>) {
if (/^([\w.]+)/) {
($tns) = $1;
}
if (/SID *= *([\w.]+)/) {
$tns{$tns} = $1 unless ($sid{$1}++)
}
}
close IN;
print join "\n", keys %tns;