strapi-plugin-tasks
v0.0.36
Published
An interface for configuring scheduled tasks in Strapi.
Downloads
3
Readme
Strapi Plugin - Tasks
Installation
npm install strapi-plugin-tasks
yarn add strapi-plugin-tasks
Example
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 develop
yarn develop
Next, navigate to the Strapi Content Manager and select the Task collection type. Create a new entry, enter the required fields and ensure that the:
name
property is set to the task"key"
(eg."exampleTask"
) defined in the strapi.tasks object registerd in the Strapi register function.executeStart
property is set to a date in the past.executionPeriod
is set to the desired frequency unit.executionFrequency
is set to the desired frequency with theexecutionPeriod
property value in mind.executeFrom
andexecuteTo
property values are set to a time range within which you wish the task to execute.enabled
property 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!