How to use the QStash EdgeFunction Scheduler template through Azion
Preview
The QStash EdgeFunction Scheduler template is designed to set up and manage a custom edge function, which receives a user-configured schedule and dispatches it through QStash, a message queue and task scheduler for serverless runtimes.
Requirements
Section titled RequirementsBefore using this template, you need to:
- Create an Upstash account.
- In the Upstash Console, get your Current Signing Key, Next Signing Key, and QStash Token within the QStash tab.
- Generate your GitHub personal token.
Getting the template
Section titled Getting the templateTo start using the QStash EdgeFunction Scheduler template, proceed as follows:
- Access Real-Time Manager (RTM).
- If you don’t have an account yet, create a new one by visiting the sign-up page.
- On the homepage, select the Start with a template option.
- Find the QStash EdgeFunction Scheduler card and select it.
- Click the Settings tab to open the configuration section.
Setting up the template
Section titled Setting up the templateIn the configuration form, you must provide the information to configure your Azion application. Fill in the presented fields.
Fields identified with an asterisk are mandatory.
- Application Name *: the name of your edge application. It’s used to identify and manage your application within Azion’s platform.
- Use a unique and easy-to-remember name. If the name has already been used, the platform returns an error message.
- QStash Current Signing Key *: a string to sign all messages sent to the destination.
- Go to your Upstash Console and copy the
QSTASH_CURRENT_SIGNING_KEY
from the Qstash tab.
- Go to your Upstash Console and copy the
- QStash Next Signing Key *: a string used to sign messages after you’ve rotated your signing keys.
- Go to your Upstash Console and copy the
QSTASH_NEXT_SIGNING_KEY
from the Qstash tab.
- Go to your Upstash Console and copy the
- GitHub Personal Token *: your GitHub personal token.
After completing all the information, click the Next button, located in the bottom-right corner. This will start the deployment process.
Deploying the template
Section titled Deploying the templateDuring the deployment, you’ll be able to follow the process through a window showing off the logs. When it’s complete, the deployment page appears, confirming the edge application has been successfully created.
This page shows you the following sections:
- Access your Edge Application section includes the link to visit and explore your edge application.
- Build Summary contains the application’s name and function.
- The Deployment details tab can be opened to access the logs related to the deployment.
- What do you want to do next? provides recommendations regarding advanced options to enhance your edge application: Customize Domain, Manage Edge Application, View Edge Application Metrics, View Edge Application Logs, and the Back to Home button.
Key configurations
Section titled Key configurationsWhen deployed, this template creates an edge function built with the arguments provided by the user. It also creates a new GitHub repository that includes an automation to enable continuous deployment. Additionally, the template creates and instantiates an edge application and its domain to ease the interaction with the edge function.
Moreover, to guarantee the optimal performance of this template, the following Azion product must be activated in your account:
You must activate this product separately via RTM. To do so:
- Access RTM.
- On the upper-left corner, select the three horizontal lines to open the Products menu.
- In the BUILD section, select Edge Application.
- You’ll be redirected to the Edge Application page.
- It lists all the edge applications you’ve created.
- Find the edge application related to your template and select it.
- In the Main Settings tab, find the Edge Application Modules section and enable the switch for the product you want to enable.
Scheduling tasks
Section titled Scheduling tasksAfter the deployment is finished and propagated, you’re able to start scheduling tasks.
To do so, you have two options: via terminal or via Upstash console.
Via terminal
Section titled Via terminal- Get your application domain.
- In the Upstash Console, copy the
QSTASH_TOKEN
from the QStash tab. - In your development environment, open the terminal.
- Make an HTTP POST request to the specified URL to schedule your messages.
Use the following exemples for your request, according to the interval required:
curl --request POST "https://qstash.upstash.io/v1/publish/{add your domain here}" \ -H "Authorization: Bearer {add your QStash token here}" \ -H "Content-Type: application/json" \ -d "{ \"hello\": \"world\"}"
Every minute
Section titled Every minutecurl --request POST "https://qstash.upstash.io/v1/publish/{add your domain here}" \ -H "Authorization: Bearer {add your QStash token here}" \ -H "Content-Type: application/json" \ -H "Upstash-Cron: * * * * *" \ -d "{ \"hello\": \"world\"}"
Every 10 minutes
Section titled Every 10 minutescurl --request POST "https://qstash.upstash.io/v1/publish/{add your domain here}" \ -H "Authorization: Bearer {add your QStash token here}" \ -H "Content-Type: application/json" \ -H "Upstash-Cron: */10 * * * *" \ -d "{ \"hello\": \"world\"}"
Every hour
Section titled Every hourcurl --request POST "https://qstash.upstash.io/v1/publish/{add your domain here}" \ -H "Authorization: Bearer {add your QStash token here}" \ -H "Content-Type: application/json" \ -H "Upstash-Cron: 0 * * * *" \ -d "{ \"hello\": \"world\"}"
Where:
Flag | Description |
---|---|
--request POST | Specifies the method of request sent. In this case, a HTTP POST method to post or schedule a HTTP message. |
-H "Authorization" | Provides authorization information through a token for authentication. You must add your QStash token here. |
-H "Content-Type" | Specifies the content type of the request body. In this case, JSON format. |
-H "Upstash-Cron" | Schedules a cron-like job in Upstash, allowing you to automate tasks at specific intervals. |
-d | Provides the data for the request body, including the JSON payload. The backslashes (\ ) before the double quotes (" ) are used to ensure that the quotes are treated as part of the data within the JSON payload. |
Done. Your task is scheduled and will be executed according to the defined scheduling.
Your scheduled tasks are visible in the Upstash Console, within the QStash tab, for your review and monitoring.
Via Upstash console
Section titled Via Upstash console- Copy your Azion application domain.
- Go to the Upstash Console and open the QStash tab.
- Complete the form with the requested information.
- In the type field, select Scheduled and choose the delay.
- Alternatively, select Once to send an immediate and unique message.
- Click the Schedule button.
Done. Your task is scheduled and visible in the Scheduled Jobs section for your review and monitoring.
Managing the template
Section titled Managing the templateConsidering that this initial setup may not be optimal for your specific edge application, all settings can be customized at any time in Azion Real-Time Manager (RTM). Once the template is deployed, you also have full control over customizing your Upstash account and QStash configurations.
To manage and edit your edge application’s settings, follow these steps:
- Access RTM.
- On the upper-left corner, select the three horizontal lines to open the Products menu.
- In the BUILD section, select Edge Application.
- You’ll be redirected to the Edge Application page.
- It lists all the edge applications you’ve created.
- Find the edge application relate to the Qstash Scheduler template and select it.
- The list is organized alphabetically. You can also use the search bar located in the upper-left corner of the list; currently, it filters only by Application Name.
After selecting the edge application you’ll work on, you’ll be directed to a page containing all the settings you can configure.
Continuous deployment
Section titled Continuous deploymentOnce the template is deployed, you can edit and update your args and code, as well as implement a continuous deployment workflow. However, you’ll first need to declare secrets on your project’s GitHub repository to complete the second build with the changes. When the second build is completed, you’ll be able to manage your project with a continuous deployment workflow and edit the args as desired.
To do so, open your QStash EdgeFunction Scheduler repository on GitHub. Then, go to Settings > Secrets and variables > Action to add your variables, following these instructions:
- Add the Azion personal token to the secrets:
- Read how to generate an Azion personal token in the documentation.
AZION_PERSONAL_TOKEN=<value>
- Add the environments for use in the action workflow in the main.yml file, included in the .github/workflows folder of your repository:
- name: edge-... id: azion_edge ... with: .... azionPersonalToken: ${{ secrets.AZION_PERSONAL_TOKEN }} ....
- Add the Upstash Signing Keys to the secrets, being:
- QSTASH_CURRENT_SIGNING_KEY: a string to sign all messages sent to the destination.
- Go to your Upstash Console and copy the
QSTASH_CURRENT_SIGNING_KEY
in the QStash tab. - QSTASH_NEXT_SIGNING_KEY: a string used to sign messages after you’ve rotated your signing keys.
- Go to your Upstash Console and copy the
QSTASH_NEXT_SIGNING_KEY
from the QStash tab.
- Go to your Upstash Console and copy the
QSTASH_CURRENT_SIGNING_KEY=<value>QSTASH_NEXT_SIGNING_KEY=<value>
- Add the environments for use in the action workflow in the main.yml file, included in the .github/workflows folder of your repository:
- name: Create args file run: | ... "QSTASH_CURRENT_SIGNING_KEY": "${{ secrets.QSTASH_CURRENT_SIGNING_KEY }}", "QSTASH_NEXT_SIGNING_KEY": "${{ secrets.QSTASH_NEXT_SIGNING_KEY }}" ...
- Open a pull request to merge the changes to the main branch and start the automatic deployment.
Now, your project is ready to work with a continuous deployment workflow, updating instantly any changes in the application or the repository.
Adding a custom domain
Section titled Adding a custom domainThe edge application created during the deployment has an assigned Azion domain to make it accessible through the browser. The domain has the following format: xxxxxxxxxx.map.azionedge.net/
. However, you can add a custom domain for users to access your edge application through it.
Contributors