Friday, March 6, 2015

Case Study: Run Cron Jobs in the Cloud

SUMMARY:
  • Import any external library or module
  • Run scripts on a regular schedule or via HTTP request


BACKGROUND:
JJGames.com ships video games around the world. A script runs on a regular basis to check each order for potential credit card fraud.

PROBLEM:
The person who codes the scripts is different than the server admin. This caused delays in code changes being pushed to production.

ANSWER:
JJGames migrated the code to hookscript where it could be updated remotely.

STACK:
Written in Perl. Uses an API from MaxMind and data stored in an PostgreSQL database.

Perl on hookscript has some common CPAN modules built in, including DBI. Accessing the database was all cut/paste from existing code.

# all perl code needs this on hookscript
use Hookscript;

# CPAN modules pre-installed on hookscript
use DBI;
use DBI qw(:sql_types);

# accessing db is easy
my $data_source = "datasource_info";
my $username    = "username";
my $password    = "password";
my $dbh         = DBI->connect( $data_source, $username, $password);
        

MaxMind offers a module on CPAN to make it easier to access their API.
Using App::fatten, JJGames combined their script code and all its module dependencies into a single file. They uploaded that file to hookscript for execution.

# this is automatically generated with app::fatten
BEGIN {
my %fatpacked;

$fatpacked{"Business::MaxMind::CreditCardFraudDetection.pm"} = ...
  package BusinessMaxMindCreditCardFraudDetection;
...
...

unshift @INC, bless \%fatpacked, $class;
  }
# end of fatpack code

# adding module is standard after fatpack
use Business::MaxMind::CreditCardFraudDetection;

my $ccfs = Business::MaxMind::CreditCardFraudDetection->new(
    isSecure => 1,
    debug    => 0,
    timeout  => 10
);
        

Migrating the code took about 15 minutes.

Now, changes are made on hookscript and instantly pushed live with no need to wait for the server admin.