Basics
Features
Best Practices
Addon Development
Related Docs
Dark Mode

#scheduleStarted Event

When a Schedule your Addon has created starts, the Schedule System will fire a scheduleStarted Event. You should respond as soon as you recieve the Event (this is done automatically when using the static-cms-addon Helper Package).

After receiving and acknowleding this Event, you can run whatever functionality you wish based on the Schedule's eventName and body which are sent within the Event. Once your custom functionality completes successfully or fails, you must make a call to MercuryCMS indicating whether the function succeeded or failed. If you fail to make the status call within the Schedule's configured timeout period, the Schedule System will mark it as a failure and ignore any further calls about that Run.

Here's an example using static-cms-addon:

import cms from 'static-cms-addon'
import importStoreItems from './importStoreItems.js' // hypothetical user-defined import script

let app = cms.createServer()

app.on('scheduleStarted:importStoreItems', async e => {
  try {
    let log = await importStoreItems({category: e.body.category})
    e.log(log)
    await e.success()
  } catch (err) {
    e.log(err)
    await e.error()
  }
})

app.listenToEvents('/api/events')
app.listen(cms.getPort())

This is the same functionality but using express without static-cms-addon:

import axios from 'axios'
import express from 'express'
import formidable from 'express-formidable'
import importStoreItems from './importStoreItems.js' // hypothetical user-defined import script

let cmsPort = process.argv[process.argv.indexOf('--cms-port') + 1]
let addonId = process.argv[process.argv.indexOf('--addon-id') + 1]
let addonPort = process.argv[process.argv.indexOf('--ports') + 1].split(',')[0]

let app = express()
app.use(formidable())

app.post('/api/events', async (req, res) => {

  // handle scheduleStarted event
  if (req.fields.event.type == 'scheduleStarted') {
    res.send() // respond immediately
    if (req.fields.event.eventName == 'importStoreItems') {
      try {
        let log = await importStoreItems({category: req.fields.event.body.category})
        await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/schedule/${req.fields.event.scheduleId}/complete`, {runId: req.fields.event.runId, log})
      } catch (err) {
        await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/schedule/${req.fields.event.scheduleId}/complete`, {runId: req.fields.event.runId, status: 'error', log: err})
      }
    }
    return
  }

  // return a simple 200 status code if the event is unrecognized
  res.send()

})

app.listen(addonPort)

In the above examples, the Addon has created multiple Schedules with the same "importStoreItems" eventName but different body values.

There are a few special rules for scheduleStarted Events when using static-cms-addon:

  • You do not need to call e.send() as this is automatically done for you before your Event Handler is run
  • The provided Event object contains a special e.log() function that works like console.log() to add lines to a log that will be automatically sent when you call e.success() or e.error()
  • The provided Event object contains special e.success() and e.error() functions used to make the status call to MercuryCMS
  • You can either use app.on('scheduleStarted', handler) to listen to all eventNames or use app.on('scheduleStarted:importStoreItems', handler) to listen to that specific eventName

In the express example, remember that you must include the scheduleId in the route and runId in the response body to /api/addon/<addonId>/schedule/<scheduleId>/complete.