UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

77 lines (76 loc) 3.59 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransactionProcessRepo = void 0; const createDebug = require("debug"); const moment = require("moment"); const factory = require("../factory"); const concurrentLock_1 = require("./concurrentLock"); const debug = createDebug('chevre-domain:repo:transactionProcess'); const DEFAULT_LOCK_EXPIRES = 120; /** * 取引プロセスリポジトリ */ class TransactionProcessRepo { // private readonly redisClient: RedisClientType; constructor(redisClient, options) { this.concurrentLockRepo = new concurrentLock_1.ConcurrentLockRepo({ redisClient }); // this.redisClient = redisClient; this.options = options; } static CREATE_REDIS_KEY(params) { return `chvrapi:lockTxn:${params.id}`; } lock(params) { return __awaiter(this, void 0, void 0, function* () { const key = TransactionProcessRepo.CREATE_REDIS_KEY(params); const ttl = (typeof this.options.lockExpiresInSeconds === 'number') ? this.options.lockExpiresInSeconds : DEFAULT_LOCK_EXPIRES; const expires = moment() .add(ttl, 'seconds') .toDate(); // reimplement using concurrentLockRepo(2025-05-27~) try { yield this.concurrentLockRepo.lock({ project: { id: params.project.id, typeOf: factory.organizationType.Project }, about: { identifier: key, typeOf: 'Thing' }, expires, audience: { identifier: '1', typeOf: 'Audience' } }); debug('locked,', params.id); } catch (error) { throw new factory.errors.AlreadyInUse(params.typeOf, [], 'Another transaction process in progress'); } // const results = await this.redisClient.multi() // .setNX(key, '1') // .expire(key, ttl) // .exec(); // debug('locked,', params.id, results); // if (Array.isArray(results) && (results[0] === 1 || (<any>results)[0] === true)) { // return; // } else { // throw new factory.errors.AlreadyInUse(params.typeOf, [], 'Another transaction process in progress'); // } }); } unlock(params) { return __awaiter(this, void 0, void 0, function* () { const key = TransactionProcessRepo.CREATE_REDIS_KEY(params); // reimplement using concurrentLockRepo(2025-05-27~) yield this.concurrentLockRepo.unlock({ about: { identifier: key, typeOf: 'Thing' }, audience: { identifier: '1', typeOf: 'Audience' } }); // await this.redisClient.del([key]); }); } } exports.TransactionProcessRepo = TransactionProcessRepo;