LogoMkSaaS Docs

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

  1. Create a new account in Cron Job.
  2. Go to Cron Job Dashboard > Cronjobs, click CREATE CRONJOB.
  3. Set Title and URL in Cron Job Common section, and set Execution schedule to run every day at 00:00.

The URL is in the format of https://{your-domain}/api/distribute-credits.

Create Cron Job

  1. Set Username and Password in Cron Job Advance section, and set Timeout to 30 seconds.

The username and password are the basic authentication credentials for the cron job to access the API, please keep them secret.

Cron Job Advance

  1. After creating the cron job, set the Username and Password in the .env file.
.env
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

route.ts
  • 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.

Test Cron Job


Reference

Next Steps

Now that you have enabled the cron job, you may want to explore these related integrations: