0.0.36 • Published 7 months ago

strapi-plugin-tasks v0.0.36

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

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 the executionPeriod property value in mind.
  • executeFrom and executeTo 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!
0.0.35

7 months ago

0.0.36

7 months ago

0.0.34

9 months ago

0.0.33

9 months ago

0.0.32

9 months ago

0.0.31

9 months ago

0.0.30

9 months ago

0.0.29

10 months ago

0.0.28

11 months ago

0.0.26

11 months ago

0.0.25

11 months ago

0.0.24

11 months ago

0.0.23

11 months ago

0.0.22

11 months ago

0.0.21

11 months ago

0.0.20

11 months ago

0.0.18

11 months ago

0.0.17

11 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

11 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago

0.0.0

11 months ago