Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #56 from proshunsuke/v14
Browse files Browse the repository at this point in the history
V14
  • Loading branch information
proshunsuke authored Oct 17, 2021
2 parents 5358394 + 781d286 commit e873d27
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SITE_NAME=keyakizaka

setup:
$(MAKE) yarn/install

Expand Down Expand Up @@ -26,7 +28,7 @@ run:
yarn clasp run execute

run/local:
node -e 'require("./dist/index.js");global.setSchedule();'
SITE_NAME=$(SITE_NAME) node -e 'require("./dist/index.js");global.setSchedule();'

open:
yarn clasp open
Expand Down
1 change: 1 addition & 0 deletions src/lib/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class Trigger {
* @returns {string | null}
*/
static getTargetSiteNameProperty(): string | null {
if (process.env.SITE_NAME) return process.env.SITE_NAME;
if (process.env.ENV !== 'production') return null;
const properties = PropertiesService.getScriptProperties();
return properties.getProperty(TARGET_SITE_NAME_KEY);
Expand Down
9 changes: 7 additions & 2 deletions src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export default class Schedule {
new KeyakiSiteSchedule(),
new SakuraSiteSchedule(),
];
for await (const siteSchedule of siteScheduleList) {
await siteSchedule.setSiteSchedule(startDate);
for (let i = 0; i < siteScheduleList.length; i++) {
const executed = await siteScheduleList[i].setSiteSchedule(
startDate,
siteScheduleList[i + 1]
);
// スケジュール登録をした場合は即終了。しなかった場合は既に登録済みなので次のサイトのスケジュール登録に進む
if (executed) return;
}
}
}
38 changes: 34 additions & 4 deletions src/sites/siteSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@ import Trigger, { TERMINATION_MINUTES } from '../lib/trigger';
import { SiteCalendarInterface } from '../calendarInterface';

export interface SiteScheduleInterface {
setSiteSchedule(startDate: dayjs.Dayjs): Promise<void>;
setSiteSchedule(
startDate: dayjs.Dayjs,
nextSiteSchedule?: SiteScheduleInterface
): Promise<boolean>;

siteCalendarUrl(): string;

siteCalendarIds(): SiteCalendarInterface[];

siteName(): string;
}

export default abstract class SiteSchedule implements SiteScheduleInterface {
async setSiteSchedule(startDate: dayjs.Dayjs): Promise<void> {
if (!this.doesExecute()) return;
/**
* スケジュール登録をした場合はtrue, しなかった場合はfalseを返す
*
* @param {dayjs.Dayjs} startDate
* @param nextSiteSchedule
* @returns {Promise<boolean>}
*/
async setSiteSchedule(
startDate: dayjs.Dayjs,
nextSiteSchedule?: SiteScheduleInterface
): Promise<boolean> {
if (!this.doesExecute()) return false;
console.info(`${this.siteName()}の予定を更新します`);
const beginningOfNexYearMonth = dayjs().startOf('month').add(1, 'year');
let targetBeginningOfMonth = SiteSchedule.getTargetBeginningOfMonth();
Expand All @@ -32,12 +48,26 @@ export default abstract class SiteSchedule implements SiteScheduleInterface {
'YYYY-MM-DD'
)}, 次実行するサイト: ${this.siteName()}`
);
return;
return true;
}
}

if (nextSiteSchedule) {
const thisMonth = dayjs().startOf('month');
Trigger.setTrigger(thisMonth, nextSiteSchedule.siteName());
console.info(
`${this.siteName()}の全てのスケジュール作成が完了したので次のトリガーをセットして終了します。次実行開始する月: ${thisMonth.format(
'YYYY-MM-DD'
)}, 次実行するサイト: ${nextSiteSchedule.siteName()}`
);
return true;
}

Trigger.deleteTargetDateProperty();
Trigger.deleteTargetSiteNameProperty();
Trigger.deleteTriggers();

return true;
}

/**
Expand Down
23 changes: 14 additions & 9 deletions tests/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('setSchedule', (): void => {
jest.spyOn(console, 'info').mockImplementation();
jest.resetAllMocks();
});
it('setScheduleが24回呼ばれ各サイト1年分の予定が作成されること', async () => {
it('サイトの1年分のスケジュールが登録された場合は次のスケジュール登録は実行されずsetScheduleは12回実行されること', async () => {
await Schedule.setSchedule(dayjs());
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(24);
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(12);
});
it('propertiesに日付がセットされていた場合にその日付からスケジュール登録が始まること', async () => {
Trigger.getTargetDateProperty = jest.fn().mockReturnValueOnce('2020-01-01');
await Schedule.setSchedule(dayjs());
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(23);
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(11);
});
it('propertiesにサイト名がセットされていた場合にそのサイトからスケジュール登録が始まること', async () => {
Trigger.getTargetSiteNameProperty = jest
Expand All @@ -35,18 +35,23 @@ describe('setSchedule', (): void => {
.mockReturnValue(true);
Trigger.setTrigger = jest.fn().mockReturnThis();
await Schedule.setSchedule(dayjs());
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(3);
expect(Trigger.setTrigger).toBeCalledTimes(2);
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(2);
expect(Trigger.setTrigger).toBeCalledTimes(1);
});
it('指定時間内に全て実行出来たらdeleteTargetDatePropertyとdeleteTriggersが呼ばれること', async () => {
it('次にスケジュール登録するサイトが存在しない場合にdeleteTargetDatePropertyとdeleteTriggersが呼ばれること', async () => {
Trigger.hasExceededTerminationMinutes = jest.fn().mockReturnValue(false);
Trigger.setTrigger = jest.fn().mockReturnThis();
Trigger.deleteTargetDateProperty = jest.fn().mockReturnThis();
Trigger.deleteTargetSiteNameProperty = jest.fn().mockReturnThis();
Trigger.deleteTriggers = jest.fn().mockReturnThis();
Trigger.getTargetSiteNameProperty = jest
.fn()
.mockReturnValueOnce('sakurazaka');
await Schedule.setSchedule(dayjs());
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(24);
expect(OneMonthSchedule.setSchedule).toBeCalledTimes(12);
expect(Trigger.setTrigger).not.toBeCalled();
expect(Trigger.deleteTargetDateProperty).toBeCalledTimes(2);
expect(Trigger.deleteTriggers).toBeCalledTimes(2);
expect(Trigger.deleteTargetDateProperty).toBeCalledTimes(1);
expect(Trigger.deleteTargetSiteNameProperty).toBeCalledTimes(1);
expect(Trigger.deleteTriggers).toBeCalledTimes(1);
});
});

0 comments on commit e873d27

Please sign in to comment.