The Reshuffle Class

The Reshuffle Class provides common services to Reshuffle connectors and event handlers. It also serves as the event bus, routing events emitted from the connector and invoking the event handlers.

Reshuffle class common services

Common Datastore

Many times you need to store information as part of running your integration business logic. The Reshuffle Class provides access to a common datastore.

Methods:

This method sets the datastore:

setPersistentStore(persistentStore)

This method returns the datastore:

getPersistentStore(persistentStore)

Note: event handlers have a utility method attached to the Event they receive as a parameter, to access the datastore: app.getPersistentStore()

Example:

Here is an example of how to use the datastore:

const { Reshuffle, CronConnector, SQLStoreAdapter } = require('../..')
const { Pool } = require('pg')
const app = new Reshuffle()

const connector = new CronConnector(app)

// see https://node-postgres.com/features/connecting on how to configure the pool
const pool = new Pool()
const persistentStore = new SQLStoreAdapter(pool, 'reshuffledb')
app.setPersistentStore(persistentStore)

connector.on({ expression: '*/5 * * * * *' }, async (event, app) => {
  let store = app.getPersistentStore()
  // single server setup
  let times = (await store.get('scripts/times-said-hello')) || 0
  console.log(`Hello World! ${times} times.`)
  times++
  await store.set('scripts/times-said-hello', times)

  // for destributed setup with many reshuffle servers set() should be replaced with update()
  let safe_count_times =
    (await store.get('scripts/times-said-hello-safe_count_times')) || 0
  console.log(`Hello World! safe count = ${safe_count_times} times.`)
  await store.update(
    'scripts/times-said-hello-safe_count_times',
    (safe_count_times) => {
      return safe_count_times === undefined ? 1 : safe_count_times + 1
    }
  )
})

app.start()

More information about Reshuffle's datastore can be found here

Common Logging

The Reshuffle class can facilitate standard logging across Connectors and event handlers.

Example:

Here is how you would use the logging in a event handle:

const { Reshuffle, CronConnector } = require('reshuffle')
const app = new Reshuffle()

const connector = new CronConnector(app)

const logger = app.getLogger()

// eslint-disable-next-line no-unused-vars
connector.on({ expression: '*/5 * * * * *' }, (event, app) => {
  logger.info('an info')
  logger.warn('a warning')
  logger.error('an error')
})

app.start()

HTTP routing

Many connectors need to listen for HTTP calls. For example, a Slack connector needs to listen to incoming HTTP events emitted by the Slack events API. Reshuffle helps you register to these calls and removes the need from the Connector to run it's own HTTP server.

Example:

At any time your controller can call this method to register a delegate (most commonly itself) as a handler to that path.

app.registerHTTPDelegate('/foo/bar', delegate)

Event Handling - handler execution

The Reshuffle class works as an event bus, aggregating all the events coming from different services via the connectors, and executing the event handlers.

Example:

Here is how a connector emits an event to the Reshuffle event bus:

this.app?.handleEvent(event.id, event)

The event ID is the identifier that maps to the right event handler, the event object is a {"key":"value"} to be passed to the event handler.

Starting and stopping all the connectors

The start(port) and the stop() method serve to control the initialization and finalization of all the connectors in the system. You should call app.start() to get connectors to work. See the examples folder

Happy coding!