Spaces:
Running
Running
| /** | |
| * Copyright (c) 2017~2020, OBCon Inc. | |
| * All rights reserved. | |
| */ | |
| /** | |
| * @file | |
| * @copyright 2017~2020, OBCon Inc. | |
| * @author gye hyun james kim [pnuskgh@gmail.com] | |
| */ | |
| class Batch { | |
| constructor() { | |
| } | |
| //--- callback(code: int, message: string) | |
| //--- code : 코드 (0. 정상) | |
| //--- message : 메시지 | |
| batchJob(options = {}, callback) { | |
| logger.info('include/batch.js :: batchJob'); | |
| callback(0, 'ok'); | |
| } | |
| //--- 모든 Promise를 차례대로 실행 한다. | |
| //--- 정상인 경우, 다음의 then 함수를 호출 한다. | |
| //--- 오류인 경우, 다음의 catch 함수를 호출 한다. 오류 이후의 promise는 실행되지 않는다. | |
| batchJob_stepBystep(options = {}, callback) { //--- 차례대로 실행 | |
| let myPromise = new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('11111111111'); | |
| resolve({ code: 0, message: 'ok 1' }); | |
| }, 3000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 1' }); | |
| } | |
| }); | |
| myPromise = myPromise.then(function(value) { | |
| return new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('22222222222'); | |
| logger.info(`code: ${value.code}, message: ${value.message}`); | |
| resolve({ code: 0, message: 'ok 2' }); | |
| }, 2000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 2' }); | |
| } | |
| }); | |
| }); | |
| myPromise = myPromise.then(function(value) { | |
| return new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('333333'); | |
| logger.info(`code: ${value.code}, message: ${value.message}`); | |
| resolve({ code: 0, message: 'ok 3' }); | |
| }, 1000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 3' }); | |
| } | |
| }); | |
| }); | |
| myPromise = myPromise.then(function(value) { | |
| logger.info(`code: ${value.code}, message: ${value.message}`); | |
| callback(value.code, value.message); | |
| }); | |
| myPromise = myPromise.catch(function(err) { | |
| logger.info(`code: ${err.code}, message: ${err.message}`); | |
| callback(err.code, err.message); | |
| }); | |
| } | |
| //--- 모든 Promise를 실행 한다. | |
| //--- 정상인 경우, then 함수를 호출 한다. values에는 모든 promise의 결과를 배열로 반환 한다. | |
| //--- 오류인 경우, 해당 오류의 반환값을 가지고 catch 함수를 호출 한다. 단, 이 경우에도 모든 Promise가 실행 된다. | |
| batchJob_all(options = {}, callback) { | |
| let promises = []; | |
| promises.push(new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('11111111111'); | |
| resolve({ code: 0, message: 'ok 1' }); | |
| }, 2000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 1' }); | |
| } | |
| })); | |
| promises.push(new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('2222222222'); | |
| reject({ code: 1, message: 'error 2' }); | |
| // resolve({ code: 0, message: 'ok 2' }); | |
| }, 1000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 2' }); | |
| } | |
| })); | |
| promises.push(new Promise(function(resolve, reject) { | |
| try { | |
| setTimeout(function() { | |
| logger.info('33333333333'); | |
| resolve({ code: 0, message: 'ok 3' }); | |
| }, 1000); | |
| } catch (e) { | |
| utils.obj.loggerError(e); | |
| reject({ code: 1, message: 'error 3' }); | |
| } | |
| })); | |
| Promise.all(promises) | |
| .then(function(values) { //--- values : { code: 0, message: 'ok' }의 배열 | |
| for (let idx = 0; idx < values.length; idx++) { | |
| let value = values[idx]; | |
| logger.info(`code: ${value.code}, message: ${value.message}`); | |
| } | |
| callback(0, '데이터 정리 완료'); | |
| }) | |
| .catch(function(err) { | |
| logger.info(`code: ${err.code}, message: ${err.message}`); | |
| callback(err.code, err.message); | |
| }); | |
| } | |
| } | |
| module.exports = Batch; | |