0.0.1 • Published 1 year ago

@cascadeos/dynamic-worker v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Dynamic Worker

i did too much meowing

The Dynamic worker is a layer over the top of AutumnDB that adds a queue and job processing. To perform writes to the underlying data store, you create a job as an action-request contract and the worker will process it. The Dynamic worker also provides:

  • a framework for building integrations with 3rd party services
  • support for formula fields using Dynscript
  • automatic paper trails for updates, creation and authorship
  • support for hook functionality using triggered actions
  • support for time delayed and recurring actions

Multiple worker instances can be safely run in parralel, as the system utilises postgraphile to enqueue and dequeue jobs.

Usage

Below is an example how to use this library:

import { foobarPlugin } from '@cascadeos/dynamic-plugin-foobar';
import { PluginManager, Worker } from '@cascadeos/dynamic-worker';
import * as autumndb from 'autumndb';
import { v4 as uuidv4 } from 'uuid';

const bootstrap = async () => {
    // Define common log context
    const logContext = {
        id: `SERVER-${uuidv4()}`,
    };

    // Set up plugins
    const pluginManager = new PluginManager([
        foobarPlugin(),
    ]);
    const integrations = pluginManager.getSyncIntegrations(logContext);
    const actionLibrary = pluginManager.getActions(logContext);

    // Set up cache
    const cache = new autumndb.Cache(environment.redis);
    await cache.connect();

    // Set up database
    const { kernel, pool } = await autumndb.Kernel.withPostgres(
        logContext,
        cache,
        environment.database.options,
    );

    // Create and new worker instance
    const worker = new Worker(
        kernel,
        kernel.adminSession()!,
        actionLibrary,
        pool,
    );

    // Set up a sync instance using integrations from plugins
    const sync = new Sync({
        integrations,
    });

    // Initialize worker instance
    await worker.initialize(context, sync, async (actionRequest) => {
        console.log('actionRequest:', JSON.stringify(actionRequest, null, 4));
    });

    // Check that the worker instance is functioning
    console.log('Worker ID:', worker.getId());

    // Close everything down cleanly
    await worker.consumer.cancel();
    await kernel.disconnect(logContext);
    await cache.disconnect();
}