Is it possible and/or common for app developers to write custom Sentinel Transitions?

The documentation for Sentinel Transitions looks pretty complete and makes me think that it’s possible for projects to write custom transitions beyond the existing available transitions. That said, it’s not clear to me if the documentation is meant as a guide for core developers or if anybody can have their own custom transitions in their projects without being on a branch/fork.

Is it possible and/or common for projects to have custom Sentinel Transitions?

And if so, do you think it would be possible/simple to have one that writes a read:message:<uuid> doc to the medic-user-xxx-meta DB whenever a specific type of data_record hits the medic DB? (Trying to find a solution for this, but also just curious).

1 Like

I don’t think it’s possible today to actually write a custom transition and have it run inside the sentinel process. But this “concept” is something that happens. What I see people doing is creating a stand-alone process hosted outside the CHT stack which follows the same pattern as a sentinel transition: monitors CouchDB’s changes feed and then “does something” custom.

That is the concept behind pollster for MSF-Goma. Or scripts/contacts for Covid MoH KE. The CARES integration with OpenMRS is a bit different, but has a similar result and it was originally implemented like a transition (using the changes feed).

Yes - you could write a doc in medic-user-xxx-meta when a data_record changes. I believe the scripts/contacts for Covid MoH KE would be a decent starting point for a JavaScript developer trying to write this.

I am not the author of this script, but here is some of the relevant section:

const level = require('level');
const PouchDB = require('pouchdb-core');

PouchDB.plugin(require('pouchdb-adapter-http'));
PouchDB.plugin(require('pouchdb-mapreduce'));

const cache = level('cache');

const updater = async () => {
  let DELAY_FACTOR = 1;
  const seqNumber = await getSeqNumber(cache);

  console.info(`Processing from Sequence Number: ${seqNumber.substring(0, 61)}`);

  const result = await getChangesAndLastSeq(couchdb, seqNumber);
  const docs = await getDocsFromChangeSet(couchdb, result.changeSet);

  ... something like docs.filter(is data_record).forEach(make your doc)

  await updateSeqNumber(cache, result.seqNumber);

  if (seqNumber === result.seqNumber) {
    DELAY_FACTOR = 30;
  } else {
    DELAY_FACTOR = 1;
  }

  return new Promise(() => setTimeout(updater, DELAY_FACTOR * SLEEP_DELAY));
};

(async () => {
  updater();
})();
3 Likes

Great, thanks for all the examples!