strapi-plugin-tasks v0.0.36
Strapi Plugin - Tasks
Installation
npm install strapi-plugin-tasksyarn add strapi-plugin-tasksExample
Code
To register an "exampleTask" task, you need to register the task's code.
First, import the CronTasks type from the strapi-plugin-tasks package and extended the Strapi interface definition:
// ./src/index.ts
import { Strapi } from '@strapi/strapi'
import { CronTasks } from 'strapi-plugin-tasks/server/models'
declare module '@strapi/strapi' {
interface Strapi {
tasks: CronTasks
}
}Next, you need to register your tasks in the exported Strapi register function:
// ./src/index.ts
register({ strapi }: { strapi: Strapi }) {
strapi.tasks = {
// the "key" (eg. "exampleTask") needs to match the name
// property of the targeted task in the Strapi Content Manager.
"exampleTask": async () => {
// Enter example task code here.
console.log('"exampleTask" task executed!')
return
}
}
}The complete file would then look like this:
// ./src/index.ts
import { Strapi } from '@strapi/strapi'
import { CronTasks } from 'strapi-plugin-tasks/server/models'
declare module '@strapi/strapi' {
interface Strapi {
tasks: CronTasks
}
}
export default {
/**
* An asynchronous register function that runs before
* your application is initialized.
*
* This gives you an opportunity to extend code.
*/
register({ strapi }: { strapi: Strapi }) {
strapi.tasks = {
// the [key] (eg. "exampleTask") needs to match the name property of the targeted task in the Strapi Content Types plugin.
"exampleTask": async () => {
// Enter example task code here.
console.log('"exampleTask" task executed!')
return
}
}
},
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
async bootstrap({ strapi }: { strapi: Strapi }) {},
}Strapi CMS
Lastly, you need to create a Task in the Strapi CMS.
First, launch your Strapi project in develop mode:
npm start developyarn developNext, navigate to the Strapi Content Manager and select the Task collection type. Create a new entry, enter the required fields and ensure that the:
nameproperty is set to the task"key"(eg."exampleTask") defined in the strapi.tasks object registerd in the Strapi register function.executeStartproperty is set to a date in the past.executionPeriodis set to the desired frequency unit.executionFrequencyis set to the desired frequency with theexecutionPeriodproperty value in mind.executeFromandexecuteToproperty values are set to a time range within which you wish the task to execute.enabledproperty is set to true.
Finally, navigate to the Tasks Settings in Strapi Settings and ensure that the tasks engine is enabled.
Execution
Once the configuration steps have been followed above the below should be logged to your Strapi applications console when the task successfully executes:
"exampleTask" task executed!2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago