ARTICLE AD BOX
Welcome to Day 5 of 12 Days of DigitalOcean! Yesterday, you group up your Birthday Reminder Service to tally connected DigitalOcean Functions, meaning it’s now serverless and cloud-ready. 🎉 Today, you will beryllium taking it a measurement further by automating it to tally connected its ain schedule—no manual input required.
By nan extremity of this guide, your activity (or immoderate different usability you’re moving on) will tally automatically astatine a group clip each day. That intends nary overmuch remembering to trigger it yourself—it conscionable works.
Why Automate?
Deploying your usability to nan unreality was a ample triumph yesterday, but having to tally it manually defeats nan intent of automation. Today’s attraction is connected scheduling nan activity to tally automatically—like clockwork—so you tin group it and hide it. DigitalOcean Functions has built-in scheduling utilizing Triggers for this nonstop purpose, and you personification 2 ways to group it up.
🚀 What You’ll Learn
By nan extremity of today, you’ll cognize really to:
- Set up a regular trigger to tally your usability automatically.
- Learn 2 ways to do this—through nan DigitalOcean Control Panel aliases pinch nan doctl CLI and project.yml.
- Test nan setup to make judge it works.
🛠 What You’ll Need
Before getting started, guarantee you personification nan following:
- A usability already deployed connected DigitalOcean (e.g., nan Birthday Reminder Service from Day 4: Building a Birthday Reminder Service) to automate pinch triggers.
- If utilizing nan doctl CLI, you’ll petition nan project.yml grounds for your function. You tin cheque retired Day 4 for instructions connected installing and mounting up nan doctl CLI, and creating nan project.yml file.
🧑🍳 Recipe for Day 5: Automating Functions pinch Triggers
Step 1: Set Up a Trigger for Your Function
DigitalOcean gives you 2 options for mounting up triggers:
- Option 1: Use nan Control Panel for speedy and easy setup.
- Option 2: Use nan doctl CLI for overmuch robust and programmatic control.
Warning: Remember that triggers created successful nan Control Panel are mislaid if you redeploy nan usability utilizing nan CLI. If you’ll beryllium redeploying often, usage Option 2 to adhd triggers consecutive to nan project.yml file.
Option 1: Using nan Control Panel to Create a Trigger
The easiest measurement to group up a trigger is done nan DigitalOcean Control Panel. It’s quick, requires nary setup, and is cleanable for elemental one-off configurations.
- Navigate to nan main Functions page successful nan DigitalOcean dashboard.
- Find your usability (e.g., reminders/birthdays) and click nan Triggers tab.
- Click Create Trigger, adhd a Name for your trigger. This tin beryllium point descriptive, for illustration daily-birthday-trigger. The punishment must incorporated only alphanumeric characters, dashes, and periods.
- Add a cron expression to group nan schedule. For example, 0 9 * * * intends nan usability will tally each clip astatine 9:00 AM.
Note: If you not judge really cron works? Check retired crontab.guru for a useful guide.
- Save nan trigger.
Once you’ve created your trigger, it’s a bully thought to proceedings it to guarantee everything useful arsenic expected. Jump to nan Test nan Automation conception beneath to study how.
Warning: While nan dashboard is elemental and effective, immoderate triggers you create coming will beryllium overwritten aliases deleted if you redeploy nan usability utilizing nan CLI. For predominant updates aliases programmatic deployments, skip to Option 2.
Option 2: Using nan doctl CLI pinch project.yml
You tin too create triggers by adding them consecutive to your project.yml file. This method is overmuch reliable for predominant redeployments because it ensures your triggers are ever included successful nan function’s configuration. Unlike nan Control Panel action described earlier, this onslaught prevents triggers from being overwritten aliases mislaid during redeployments.
Here’s really you tin group it up:
-
Open your project.yml grounds from Day 4: Deploying Birthday Notifications pinch DigitalOcean Functions. It mightiness look point for illustration this:
packages: - name: reminders shared: false environment: DO_DB_NAME: "${DB_NAME}" DO_DB_USER: "${DB_USER}" DO_DB_PASSWORD: "${DB_PASSWORD}" DO_DB_HOST: "${DB_HOST}" DO_DB_PORT: "${DB_PORT}" TWILIO_ACCOUNT_SID: "${TWILIO_ACCOUNT_SID}" TWILIO_AUTH_TOKEN: "${TWILIO_AUTH_TOKEN}" TWILIO_PHONE_FROM: "${TWILIO_PHONE_FROM}" TWILIO_PHONE_TO: "${TWILIO_PHONE_TO}" functions: - name: birthdays runtime: python:default -
Add a triggers conception to your usability configuration to specify nan schedule:
triggers: - name: daily-birthday-trigger sourceType: scheduler sourceDetails: cron: "0 9 * * *" -
Final updated project.yml file:
packages: - name: reminders shared: false environment: DO_DB_NAME: "${DB_NAME}" DO_DB_USER: "${DB_USER}" DO_DB_PASSWORD: "${DB_PASSWORD}" DO_DB_HOST: "${DB_HOST}" DO_DB_PORT: "${DB_PORT}" TWILIO_ACCOUNT_SID: "${TWILIO_ACCOUNT_SID}" TWILIO_AUTH_TOKEN: "${TWILIO_AUTH_TOKEN}" TWILIO_PHONE_FROM: "${TWILIO_PHONE_FROM}" TWILIO_PHONE_TO: "${TWILIO_PHONE_TO}" functions: - name: birthdays runtime: python:default triggers: - name: daily-birthday-trigger sourceType: scheduler sourceDetails: cron: "0 9 * * *"This tells DigitalOcean to tally nan birthdays usability each clip astatine 9:00 AM.
-
triggers section: Defines nan name, type, and schedule for nan trigger. You tin punishment your trigger point descriptive, for illustration daily-birthday-trigger.
-
cron: Specifies nan clip nan usability will tally regular successful UTC. The cron look 0 9 * * * for example, schedules nan usability to tally each clip astatine 9:00 AM.
-
-
Save nan grounds and deploy it by moving nan pursuing bid from nan directory containing nan my-birthday-reminder-service folder:
doctl serverless deploy my-birthday-reminder-serviceImportant: Make judge to tally nan bid from nan genitor directory, not incorrect nan my-birthday-reminder-servicefolder. Running it from nan incorrect location tin consequence successful an correction for illustration this:
❯ doctl serverless deploy my-birthday-reminder-service Error: ENOENT: nary specified file aliases directory, lstat
-
Once nan deployment is successful, you’ll spot a confirmation relationship akin to this:
Deployed functions - reminders/birthdays Deployed triggers: - daily-birthday-trigger -
Go to your dashboard to verify that nan trigger has been created nether nan Functions section.
Pro Tip: To proceedings your trigger, temporarily group nan cron look to a less minutes from now (e.g., 28 9 * * * if it’s 9:25 AM). After confirming that it works, update it backmost to your intended schedule and redeploy.
Step 2: Test nan Automation
Let’s proceedings your triggers to make judge they’re working. Instead of waiting for them to occurrence connected their accustomed schedule, you tin temporarily group them to tally a less minutes from now. Here’s really to do it:
-
Set nan cron expression for your trigger to a less minutes from nan existent clip (in UTC). For example, if it’s 9:25 AM UTC, group nan cron look to 28 9 * * * truthful nan usability runs astatine 9:28 AM UTC.
Note: Not judge really to personification your conception clip to UTC? Tools for illustration Time Zone Converter tin help.
-
Save nan updated trigger (if utilizing nan Control Panel) aliases redeploy your updated project.yml grounds (if utilizing nan CLI):
doctl serverless deploy my-birthday-reminder-service -
Wait for nan trigger to execute, past cheque nan activation logs to corroborate nan usability ran successfully:
doctl serverless activations logs --function reminders/birthdays --last
Note: Activation logs grounds specifications astir erstwhile your usability was executed, including whether it ran successfully aliases encountered errors. They’re a adjuvant measurement to verify that your trigger fired astatine nan scheduled time.
This bid will return nan specifications of nan astir caller activation of your function. For example, you mightiness spot point for illustration this:
=== 59e8e4f482874d79a8e4f48287dd79ef occurrence 12/23 07:46:33 reminders/birthdays:0.0.6 2024-12-23T07:46:33.323205805Z stdout: Message sent for Charlie Brown. Message SID: SM85f5caeb3ec09239e0d8bdaaba2b158b
This confirms that nan trigger fired and nan usability was activated successfully! 🎉
Note: Once you’ve verified everything is working, update nan cron look to your intended schedule (e.g., 0 9 * * * for 9:00 AM UTC daily) and prevention aliases redeploy.
A Quick Note connected Logs
While nan activations logs bid is simply a awesome measurement to cheque caller executions, sometimes you’ll petition overmuch elaborate logs to debug aliases analyse issues pinch your function. DigitalOcean too provides options to guardant these logs to outer logging services, making monitoring and troubleshooting your exertion easier complete time.
On Day 6, you will study to position logs directly, construe them effectively, and group up log forwarding to outer services for illustration Logtail aliases Papertrail. These devices will thief you easy enactment connected apical of your function’s performance.
🎁 Wrap-Up
Here’s what you accomplished today:
- You automated your Birthday Reminder Service (or immoderate different function) to tally daily.
- You learned 2 ways to group up triggers—through nan Control Panel and pinch nan doctl CLI.
- You tested your setup to guarantee it useful arsenic expected.
Here are nan erstwhile tutorials from this series:
- Day 1: Setting Up a PostgreSQL Database for Birthday Reminders
- Day 2: Connecting to Your PostgreSQL Database pinch Python
- Day 3: Checking Birthdays and Sending SMS Notifications
- Day 4: Deploying Birthday Notifications to Functions
Up Next: Now that your activity runs independently, nan adjacent measurement is monitoring it effectively. In nan adjacent tutorial, you will study really to position your usability logs and guardant them to outer services to simplify hunt and troubleshooting. See you then!