Cron Jobs
Learn how to set up and use Cron Jobs in MkSaaS
MkSaaS supports integrating Cron Jobs, which is a free and open-source cron job scheduler, for scheduled tasks, allowing you to automate your website's tasks, like distributing credits to users.
You have to setup cron jobs only if your product meets one of the following conditions:
- The product has a free plan, and free users can receive credits every month.
- The product has a lifetime plan, and lifetime users can receive credits every month.
- The product has a yearly subscription plan, and subscription users can receive credits every month.
Credits
Learn more about Credits
Setup
- Create a new account in Cron Job.
- Go to Cron Job Dashboard >
Cronjobs
, clickCREATE CRONJOB
. - Set
Title
andURL
inCron Job Common
section, and setExecution schedule
to run every day at00:00
.
The URL is in the format of https://{your-domain}/api/distribute-credits
.
- Set
Username
andPassword
inCron Job Advance
section, and setTimeout
to30
seconds.
The username and password are the basic authentication credentials for the cron job to access the API, please keep them secret.
- After creating the cron job, set the
Username
andPassword
in the.env
file.
CRON_JOBS_USERNAME=your_username
CRON_JOBS_PASSWORD=your_password
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Cron Job System Structure
src/app/api/distribute-credits/route.ts
: The cron job API route for distributing credits to users.
Usage
There is only one cron job for now, for distributing credits to users. It should be run every day, so you should set the right Execution schedule
when creating the cron job.
When the cron job is triggered, it will make a request to the /api/distribute-credits
route, with the Username
and Password
as basic authentication credentials, and distribute credits to users.
You can test the cron job by clicking the TEST RUN
button in the cron job page, and check the response in the Response
tab in the image below. You can also view the execution history of the cron job.
Reference
Next Steps
Now that you have enabled the cron job, you may want to explore these related integrations: