/tinyletter

The Programs of the Week a Woman Was Nominated for President

This Week’s Program: July 25 - July 29

There’s a lot to be both excited and anxious about this week. For those feeling overwhelmed with emotion, my advice is to do what I do: don’t feel. Write code.

Here’s the code that I wrote while suppressing the urge to write a think piece on Facebook…

d8c28f84a32c83041e4167d5ea4358ea9f616079

cron is a fun and infuriating piece of software. Here’s what I want to do: Every day at 9am NYC time the program should run and make a song. Inside of my CloudFormation template I do the JSON plate spinning required to ensure that my crontab is loaded onto the system and that it pulls the correct variables out of the stack. Here’s what it looks like:

FORECAST_API_KEY={KEY}
0 13 * * *  cd /srv/sonic_sketches &&
↪ /usr/local/bin/lein trampoline run {BUCKET} >/dev/null 2>&1

I’ll break this down: At the top, you can set environment settings that will be passed as variables into the running script. I do this for my Forecast API credentials that last week I passed as a CloudFormation parameter.

0 13 * * * is the crontab scheduling syntax. This says “run at 13:00 every day.” The server’s time zone is set to UTC. NYC, currently in Daylight Savings, is in the EDT time zone, -0400 away from UTC.

I use cd and not pushd and the full path to lein, because the cron job is run in a very limited shell environment. No bash here. That’s why I also use the full syntax to explicitly map standard error to standard out (2>&1) versus the handy bash shorthand.

6b5a9f0f35c73a6ee7a606f272f76cfdd3d3ca84

I want to always run the program at 9:00 am in NYC, and not have to worry about Daylight Savings. To do that I set up two cron jobs: one for 13:00 and one for 14:00. I write a test with the date command. date +%z outputs just the time zone offset from UTC. The date command reads the TZ environment variable for time zone information. Depending on if this is “-0400” or “-0500” the cronjob will continue to execute the rest of the program.

Later I have to futz with this because crontabs are very sensitive to the percent symbol (%) and because I execute date within a subshell, the TZ var is not exported to it. This is resolved by 87c5b32fba062506b268296d1df39a32d900a895.

44d1a2da067e56e0760eb2a0b1d536bdfdf7a73e

I reach into the bag of Java 8 tricks to do some better DateTime handling. With this, I grab the day of week and the ISO8601 representation of the date. This is to make the filename in s3 easier to understand.

c11a66e11a81ce8af3c1eb80559922d2e5001956

I update .gitignore to ignore a local Leiningen profile. I’m going to put secrets here.

232ff32fd206253b756477b2b74b61f037422256

The secrets I will put there are OAuth tokens for Twitter.

I bring in Twitter4J, the Java client for the Twitter API.

Next week, I’m going to automatically publish generated songs to Twitter.

#botALLY
– Mark