Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import Logger from "bunyan"; import { Env } from "../Env"; import { SecretString } from "lib"; export interface FetchSettingModel { id: bigint; siteId: bigint; userName: string; password: SecretString; } export interface MonthlyUsageModel { fetchSettingId: bigint; year: number; month: number; begin: string; end: string; amount: number; yen: number; } export interface UsageFetcher { fetchMonthly( logger: Logger, setting: FetchSettingModel ): Promise<MonthlyUsageModel[]>; } export interface FetchSettingRepository { findAllGasFetchSettings(): Promise<FetchSettingModel[]>; } export interface UsageRepository { saveGasMonthlyUsages(usages: MonthlyUsageModel[], now: Date): Promise<void>; } export interface FetchStatusRepository { upsertGasFetchStatusSuccess(fetchSettingId: bigint, now: Date): Promise<void>; upsertGasFetchStatusFailure(fetchSettingId: bigint, now: Date): Promise<void>; } export interface RunResult { successfulCount: number; failureCount: number; } export class UsageService { readonly env: Env; readonly fetcher: UsageFetcher; readonly fetchSettingRepo: FetchSettingRepository; readonly usageRepo: UsageRepository; readonly fetchStatusRepo: FetchStatusRepository; constructor( env: Env, fetcher: UsageFetcher, fetchSettingRepo: FetchSettingRepository, usageRepo: UsageRepository, fetchStatusRepo: FetchStatusRepository ) { this.env = env; this.fetcher = fetcher; this.fetchSettingRepo = fetchSettingRepo; this.usageRepo = usageRepo; this.fetchStatusRepo = fetchStatusRepo; } async run(parentLogger: Logger): Promise<RunResult> { const now = new Date(); const settings = await this.fetchSettingRepo.findAllGasFetchSettings(); parentLogger.info("loaded gas_fetch_settings, count %d", settings.length); const result = { successfulCount: 0, failureCount: 0, }; for (const setting of settings) { const logger = parentLogger.child({ setting_id: setting.id.toString(), user_name: setting.userName, }); logger.info( "fetch gas start by setting[%s, %s]", setting.id.toString(), setting.userName ); try { await this.fetchAndSave(logger, now, setting); result.successfulCount++; } catch (err) { logger.error({ err }, "fetch gas failed"); result.failureCount++; } finally { logger.info( "fetch gas end by setting[%s, %s]", setting.id.toString(), setting.userName ); } } return result; } private async fetchAndSave( logger: Logger, now: Date, setting: FetchSettingModel ): Promise<void> { try { const models = await this.fetcher.fetchMonthly(logger, setting); await this.usageRepo.saveGasMonthlyUsages(models, now); await this.fetchStatusRepo.upsertGasFetchStatusSuccess( setting.id, new Date() ); } catch (err) { await this.fetchStatusRepo.upsertGasFetchStatusFailure( setting.id, new Date() ); } } } |