npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@requence/producer

v0.0.5

Published

This package creates a task that can be processed.

Downloads

1

Readme

@requence/producer

This package creates a task that can be processed.

Basic Usage

import createProducer from '@requence/producer'

const producer = await createProducer()

Every producer instance needs a url parameter to connect to the operator. In the basic example, this parameter gets automatically retrieved from environment variables REQUENCE_URL. To be more explicit about this configuration, you can pass the connection as the first argument:

const producer = await createProducer('your operator connection url string')

Create a task template

import createProducer, { createTemplate } from '@requence/producer'

// create a template
const template = createTemplate().addService('dummy', '1')

storeTemplateSomehow(template.toJSON())

This will create a new template that only consists of one service.

Create a task

const templateJson = loadTemplateSomehow()
const task = producer.createTask(templateJson)
const result = await task.run()

Create and execute a one time task

The createTask method also accepts a builder function to streamline the process for quick one time tasks

const task = producer.createTask((builder) => builder.addService('dummy', '1'))
const result = await task.run()

Provide task input and metadata

const task = producer
  .createTask(template)
  .withMeta({ traceId: 123 })
  .withInput('some-input-value')

const result = await task.run()

Building complex task logic

In the previous example, only one service got executed. The template builder provides method to make the task infinitely complex.

Executing a service

createTemplate().addService('service-1', '1.0.0')

this will execute the service named service-1 in version 1.0.0. The version can also be specified as a version range or a version wildcard like 1.0.0, ^1.0.0, 1.0.0 - 1.5.0, 1.0.x etc. If the version is omitted, * is implied.

sequential services

createTemplate().addService('service-1', '1').addService('service-2', '1')

this will call service-1 and service-2 in sequence, so that service-2 can access the result of service-1

parallel services

createTemplate().addParallel((parallel) =>
  parallel.addService('service-1').addService('service-2'),
)

this will call service-1 and service-2 in parallel

parallel execution of sequential services

createTemplate()
  .addParallel((parallel) =>
    parallel
      .addSequence((sequence) =>
        sequence.addService('service-1').addService('service-2'),
      )
      .addService('service-3'),
  )
  .addService('service-4')

this will execute service 1 to 4 in the following order:

Parallel Sequence Order

conditional execution

createTemplate()
  .addService('service-1') // assume the response is { "done": boolean }
  .addCondition('service{service-1}.done', '===', true)
  .then((t) => t.addService('service-2'))
  .else((e) => e.addService('service-3'))

When service-1 resolves with {"done": true}, service-2 will get executed, otherwise service-3. When no else case is defined, the task would stop.

error handling

There are two ways to deal with errors in tasks:

createTemplate().addService('service-1').onFailSkip().addService('service-2')

This will allow service-1 to fail. The task will continue without the result of service-1 and moves to service-2

createTemplate()
  .addService('service-1')
  .onFail((f) => f.addService('service-2'))
  .addService('service-3')

This will execute service-2 only when service-1 fails. Then the task will continue to service-3

adding retries

In some cases a service could fail but recover automatically after a grace period.

createTemplate().addService('service-1').withRetry(3, 5_000)

This will retry to execute service-1 three times with a delay of 5 seconds inbetween.

adding service configuration

createTemplate().addService('service-1').withConfiguration('some-config')

adding aliases

When a service is used multiple times in a task, it is hard for other services to retrieve the correct result. For this case, an alias can be defined per service.

createTemplate()
  .addService('search')
  .withConfiguration({ searchFor: 'name' })
  .withAlias('name-result')
  .addService('search')
  .withConfiguration({ searchFor: 'job' })
  .withAlias('job-result')

Receiving updates

there are 2 ways to receive task specific updates.

Via callback:

producer.createTask(template).run((update) => {
  console.log('received update', update)
})

Via Async Iterator:

const resultPromise = producer.createTask(template).run()

for await (const update of resultPromise) {
  console.log('received update', update)
}