This post originally appeared on mediacurrent.com.
July 29, 2020

Here at Mediacurrent, we use Slack for, well…just about everything. From talking shop on project teams to weekly knowledge share discussions, Slack forms the backbone of communications for our distributed team. That’s why on a recent project, when we had the need to monitor daily site cron jobs, posting these updates to a Slack channel felt like a natural solution. It prevented us from needing to log into a server to determine if a job ran successfully and provided visibility to the entire project team—if a job failed, everyone on the project team would be aware. Bonus points: it was surprisingly easy.

Here’s what we did. First, we created a Slack channel specifically for project alerts. This prevented the project’s main channel from getting cluttered with messages on a daily basis. Next, we asked our Slack administrator to allow us to register an incoming Slack webhook. An incoming webhook allows you to send a message to a specific Slack channel, so when we registered the webhook, we selected our project’s alerts channel. Finally, at the beginning and end of our automated jobs we made a call to a PHP function:

function send_slack_alert($message) {
  $ch = curl_init(getenv('SLACK_WEBHOOK_URL'));
  $data = ['text' => $message];
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-type: application/json',
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

And what was the result?

If everything went as planned, we called the function with a message such as: send_slack_alert(':thumbsup: Everything is A-OK. Carry on!') and the project team received the reassuring message:

👍 Everything is A-OK. Carry on!

And if things didn’t go quite as planned, we called the function with something like: send_slack_alert(':thumbsdown: Uh-oh, something went wrong. Better check the logs!') and the project team received a call to action:

👎 Uh-oh, something went wrong. Better check the logs!

Either way, the project team was aware of whether or not a job completed successfully, and in the event of a problem, we would be the first ones to know.

One of the specific use-cases that we sent alerts for was automated archiving of content. The archiving job occurred at an off-peak time, in the wee-hours of the morning, and most days everything went as expected and we started our days off with a nice, big thumbs up. On the rare occasion that there was a problem, we were immediately notified and could intervene to analyze what caused the job to fail and step in to restart it manually. This made for a happy project team and a happy client because no one came to find days or weeks later that a critical automated behavior was failing on a regular basis, buried deep in the logs.

All in all, Slack webhooks provide an easy and lightweight solution for application monitoring. They are painless to set up and leverage the communication tools that your project team is already comfortable with. The next time you need to add visibility to a project that relies on automated behaviors, consider integrating a Slack webhook into your application monitoring stack.