github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/lib/batch-manager.ts (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import * as database from '../clients/database';
    16  import { BatchProcessor } from './batch-processor';
    17  import { IDBBatch } from './interfaces';
    18  import * as utils from './utils';
    19  
    20  const log = utils.getLogger('lib/batch-manager.ts');
    21  
    22  /**
    23   * Lifecycle manager for BatchProcessor instances, within a single type, across multiple authors
    24   */
    25  export class BatchManager {
    26  
    27    processors: {[author: string]: BatchProcessor} = {};
    28  
    29    constructor(
    30      private type: string,
    31      private processBatchCallback: (batch: IDBBatch) => Promise<void>,
    32    ) { }
    33  
    34    public async init() {
    35      // Query all incomplete batches for our type, in creation order
    36      const inflightBatches = await database.retrieveBatches({
    37        type: this.type,
    38        completed: null,
    39      }, 0, 0, { created: 1 });
    40      const byAuthor: {[author: string]: IDBBatch[]} = {};
    41      for (const inflight of inflightBatches) {
    42        const forAuthor = byAuthor[inflight.author] = byAuthor[inflight.author] || [];
    43        forAuthor.push(inflight);
    44      }
    45      // Init a set of processors for each distinct authors.
    46      // Note these will be reaped if no work comes in while we're processing the backlog
    47      for (const [author, forAuthor] of Object.entries(byAuthor)) {
    48        await this.getProcessor(author).init(forAuthor);
    49      }
    50    }
    51  
    52    protected processorCompleteCallback(author: string) {
    53      log.trace(`${this.type} batch manager: Reaping processor for ${author}`);
    54      delete this.processors[author];
    55    }
    56  
    57    public getProcessor(author: string) {
    58      if (!this.processors[author]) {
    59        log.trace(`${this.type} batch manager: Creating processor for ${author}`);
    60        this.processors[author] = new BatchProcessor(
    61          author,
    62          this.type,
    63          this.processBatchCallback,
    64          this.processorCompleteCallback.bind(this),
    65        );
    66      }
    67      return this.processors[author];
    68    }
    69  
    70  }