line

Calculate and send real-time alerts

This example shows a workflow for a delivery or car service with real-time data calculations and a vehicle tracking process as well as triggering notifications (emails or text messages) to the customer based on different conditions.

Visual of Workflow

This flowchart shows a visual representation of the workflow tasks.
line

Workflow Steps

  • Run a simple algorithm that calculates the duration and estimated time of arrival (ETA)
  • As long as the remaining duration is more than 1 hour, continue calculating the ETA and remaining duration at every interval (half of the time until arrival)
  • Once the remaining duration is less than one hour, send a message to the user saying that the vehicle will arrive in 1 hour
  • As long as the duration is more than our target time (2 minutes here), recalculate the remaining duration and ETA and compare it with the previous one
  • If the ETA has changed by more than 20 minutes, then send an alert to the user letting them know that there is a delay and include the new ETA
  • If the ETA has not changed by more than 20 minutes, send a notification to the user 2 minutes before arrival

Workflow Code

This workflow is the code that orchestrates tasks (through the Zenaton workflow engine) and executes them on your servers.

const { workflow, duration } = require("zenaton");

module.exports = workflow("NotifyEtaWorkflow", {
  *handle(tripId, user) {
    const twilio_sms = this.connector('twilio_sms', 'your-connector-id');

    this.tripId = tripId;
    this.user = user;

    const BEFORE = 3600;
    const PRECISION = 120;
    const UPDATE = 1200;
    let duration;
    let eta;
    let eta2;

    [duration, eta] = this.getTimeToArrival();

    while (duration > BEFORE + PRECISION) {
      yield this.wait.for(duration.seconds((duration - BEFORE) / 2));

      [duration, eta] = this.getTimeToArrival();
    }

    yield this.run.task("InformUserOfEtaTask", this.user, eta);

    while (duration > PRECISION) {
      yield this.wait.for(duration.seconds(UPDATE));

      [duration, eta2] = this.getTimeToArrival();
      if (Math.abs(eta2 - eta) >= UPDATE) {
        eta = eta2;

        twilio_sms.post('/Messages.json', {body: {To: this.user.phone, Body: '...'}})
      }
    }
  },
  *getTimeToArrival() {
    return yield this.run.task("CalculateTimeToArrivalTask", this.tripId);
  },
});

Workflow Executions

line