July 2012 Archives

An Excuse to Get Outside (Team Building via RC Helicopter)

Last month I was lucky enough to attend YAPC::NA in Madison, WI. Among the many spectacular talks, impressive people, and events there was a raffle that took place for conference attendees. With all proceeds going to the Perl Foundation, I contributed $100 and threw my raffle tickets in to two buckets: lunch with Larry Wall and some ThinkGeek swag.

Unfortunately, I didn't get to dine with the great one, but at one of the evening events I did win the ThinkGeek grand prize: a remote controlled outdoor helicopter (picture at right).

Now, at the time, I was slightly underwhelmed. My first thought was, "Wow! I won! Wait .. I won? How am I going to get this thing home?" My second thought was, "What the heck am I going to do with a remote controlled helicopter!?"

Thoughts aside, the helicopter was a great ice breaker for meeting new people and I had some great conversations at the event. By the time the conference had ended, I decided to check my carry-on bag and continued to draw attention with my helicopter as I traveled from Madison, WI to Irvine, CA.

When I returned home, I explained the highlights of YAPC to my co-workers and told them of my grand prize winnings. Their first reaction was, "You won a remote controlled helicopter!? ... WHY IT IS NOT HERE!?"

So, the next day I brought the helicopter in to the office and over the last several weeks we have taken 15-20 minute breaks to get outdoors and take turns flying it. Not only does it make me feel like a kid again, but it gives us a great excuse to get out of the office, enjoy the sunny southern California days, and have some fun. We even started assigning achievements to people that flew it: If you were able to cut the leaves off a tree without crashing you were awarded the Paul Bunyan achievement, or if you demonstrated excellent control/maneuverability you were given the Sensei achievement.

Sadly, however, last week it had its last flight. It had been slowly deteriorating over the last few trips and now no longer flies after losing some critical pieces.

To remedy this, we have now started a fund so we can all pitch in and buy another, better helicopter, since we all had so much fun. What are some activities you do with your co-workers, either inside the office or out?

Preventing Collisions with Perl cron jobs

Imagine you are a brilliant developer who just created a Perl script that takes form submissions from your website and imports them to your ticketing system. Well done! Now you want to set this script to run periodically so that as new requests come in they are automatically submitted to your ticketing system. Working in a Linux environment, you quickly add a line to the crontab to ensure this script runs every 5 minutes.

Perfect! Your done.

Time passes …

One day you start receiving complaints that duplicate tickets are being submitted to your ticketing system. After some investigation, you discover that the number of forms being submitted to your website is more than your script can process before another instance of your script is being called again. This results in your script processing the same form multiple times!

[09:00:00 Instance 1] Searching for all cases … found 8!
[09:01:03 Instance 1] Processing case A
[09:02:12 Instance 1] Processing case B
[09:02:55 Instance 1] Processing case C
[09:03:20 Instance 1] Processing case D
[09:04:32 Instance 1] Processing case E
[09:05:00 Instance 2] Searching for all cases … found 3!
[09:05:02 Instance 2] Processing case F
[09:05:12 Instance 1] Processing case F
[09:06:10 Instance 2] Processing case G
[09:06:28 Instance 1] Processing case G
[09:07:42 Instance 2] Processing case H
[09:07:50 Instance 1] Processing case H

One way to do this, is to prevent your script from running again until the previous instance has finished. Using, the Proc::ProcessTable module, you can retrieve a listing of running processes to see if your script is already running, and if so, exit to let your other instance finish.

use Proc::ProcessTable;
my $count = 0;
my $table = Proc::ProcessTable->new;
for my $process ( @{ $table->table } ) {
  next unless $process->{cmndline};
  if ($process->{cmndline} =~ /$0/) {
    $count++;
    exit if $count > 1;
  }
}

In the above code, we create an instance of the Proc::ProcessTable, and by calling the table method, we can iterate through the list of running processes and check to see if the script is running. If the total number of instances is greater than one, the script simply exits.

Checking for the exists of the script, is accomplished by using the special Perl variable $0 which is populated with the name of the file being executed. So, for example, if your script was called “process_website_forms.pl” this is what the $0 would contain.

Simply place this block of code at the start of your script, and there will be no more redundant cases submitted to your ticketing system.

About Jonathan Lloyd

user-pic I blog about Perl.