2.0.3 • Published 2 years ago

@elastic.io/batching-library v2.0.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

CircleCI

batching-library

Library for batching in elastic.io. Batching allows users to combine several calls to API in one big call.

BatchClient

BatchClient represents client that works with batches. It contains logic for creating, updating statuses and storing batches.

Constructor

new BatchClient(store, config)

BatchClient constructor expects three parameters: 1. store - implementation of IStore interface. For example MongoStore. This is where batches will be stored. 2. config - instance of BatchConfig. It represent configuration and defines limitation for batches e.g: maxSize, maxItemsNumber.

Example
import { MongoStore, IStore } from '@elastic.io/batching-library';
import { BatchClient } from '@elastic.io/batching-library';
import { BatchConfig } from '@elastic.io/batching-library';
import { ConnectionOptions } from 'mongoose';

const maxSize = 1000;
const maxItemsNumber = 10;
const maxWaitTime = 1000;
const maxEmitRate = 1;
const maxRetry = 0;
const uri: string = 'url_to_mongo';
const collection = 'collection_name';
const connectionOpt: ConnectionOptions = {
  user: 'user',
  pass: 'password', 
};
const mgStore: IStore = new MongoStore(collection, uri, connectionOpt);
const config = new BatchConfig(maxSize, maxItemsNumber, maxWaitTime, maxEmitRate, maxRetry);
const client = new BatchClient(mgStore, config);

Methods

1. saveItem(item)

Save provided item to batch, returns batch with saved item (without another Batch items).

BatchItem properties
ParameterTypeRequiredDescription
idstringfalseIf not specified - uuid v1 will be generated
itemstringtrueBody of the Batch Item
Example
const batch: Batch = await client.saveItem({ id: 0, item: {}});

2. getReadyBatches()

Using provided emitter emit batches with status 'READY'

Example
const batches: Batch[] = await client.getReadyBatches();
  await Promise.all(batches.map(async (batch) => {
    try {
      
     // process batch implementation
      
      await client.updateBatchStatusById(batch.id, 'SUCCESS'); // batch was successfully processed
    } catch (e) {
      log.error('Error: %o', e);
      await client.updateBatchStatusById(batch.id, 'FAILED'); // batch processed with error
    }
  }));

Implementing your own batch storage

For reference implementation example take a look at MongoStore 1. Extend AbstractStore and override each abstract method. 2. Implementation must ensure that each operation is transactional

Environment variables

NameMandatoryDescriptionValues
LOG_OUTPUT_MODEYesLog record view formatshort long simple json bunyan
LOG_LEVELYesLog Levelfatal error warn info debug trace
MONGO_URLNoURL of mongo db used during integration testsurl to mongo db
MONGO_USERNoUsername of mongo db user, used during integration testsusername
MONGO_PASSWORDNoPassword for connecting to mongo db, used during integration testspassword
MONGO_DBNoDatabase of mongo db used during integration testsdatabase name
MONGO_DROP_DBNoIf 1 drop database each test integration test if 0 dont1 0

Environment template file

Limitations

MongoStore:

  1. Supported Mongo database versions: 4.0 and higher.

  2. Retries do not implemented.

  3. Emit rate not implemented.

  4. Library is not guarantee sequence processing of batch item

MaesterStore:

  1. Emit rate not implemented.
2.0.1-dev.2

2 years ago

2.0.1-dev.1

2 years ago

2.0.1-dev.4

2 years ago

2.0.1-dev.3

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.2-dev.3

2 years ago

2.0.2-dev.2

2 years ago

2.0.2-dev.1

2 years ago

2.0.0-dev.3

2 years ago

2.0.0-dev.2

2 years ago

2.0.3-dev.2

2 years ago

2.0.3-dev.1

2 years ago

2.0.0

3 years ago

2.0.0-dev.1

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.5-dev.1

4 years ago

0.0.5-dev.2

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.3-dev.3

4 years ago

0.0.3-dev.2

4 years ago

0.0.3-dev.1

4 years ago

0.0.2

5 years ago

0.0.1-dev.16

5 years ago

0.0.1-dev.15

5 years ago

0.0.1-dev.14

5 years ago

0.0.1-dev.13

5 years ago

0.0.1-dev.12

5 years ago

0.0.1-dev.11

5 years ago

0.0.1-dev.10

5 years ago

0.0.1-dev.9

5 years ago

0.0.1-dev.8

5 years ago

0.0.1-dev.6

5 years ago

0.0.1-dev.5

5 years ago

0.0.1-dev.4

5 years ago

0.0.1-dev.3

5 years ago

0.0.1-dev.2

5 years ago

0.0.1

5 years ago

0.0.1-dev.1

5 years ago