schedules.js 4.26 KB
Newer Older
1
import envs from "../config/config";
2
3
import schedule from "node-schedule";
import { spawn } from "child_process";
4
import { handleOutData } from "./controllers/dataController";
5
6

// 매일 자정에 실행할 스케줄의 규칙
7
8
9
10
const rule_dataProcessing = new schedule.RecurrenceRule();
rule_dataProcessing.hour = 0;
rule_dataProcessing.minute = 0;
rule_dataProcessing.second = 0;
11
12

// 매일 자정에 실행되는 데이터 처리 스케줄
13
const dataProcessingJob = schedule.scheduleJob(rule_dataProcessing, () => {
14
15
16
17
18
19
20
21
  const today = new Date();

  console.log(
    `${today.getFullYear()}.${
      today.getMonth() + 1
    }.${today.getDate()} - Data Processing Start.`
  );

22
  const pyprocess = spawn("python", [envs.inner_dir.data_processing_main]);
23
24
25

  pyprocess.stdout.on("data", (data) => {
    console.log("Data processing is start.");
26
    console.log(data.toString()); // Buffer to String.
27
28
  });

29
  pyprocess.stderr.on("data", (error) => {
30
    console.log("Error in the data processing.");
31
    console.log(error.toString());
32
33
34
  });

  pyprocess.on("close", () => {
35
    console.log("The data processing is done.");
36
37
  });
});
38
39
40
41
42
43
44
45
46
47
48
49

// 10분 마다 지역 외부 날씨 저장 스케쥴
const rules_weather_out_store = {
  "00m": new schedule.RecurrenceRule(),
  "10m": new schedule.RecurrenceRule(),
  "20m": new schedule.RecurrenceRule(),
  "30m": new schedule.RecurrenceRule(),
  "40m": new schedule.RecurrenceRule(),
  "50m": new schedule.RecurrenceRule(),
};

rules_weather_out_store["00m"].minute = 0;
50
51
rules_weather_out_store["00m"].second = 0;

52
rules_weather_out_store["10m"].minute = 10;
53
54
rules_weather_out_store["10m"].second = 0;

55
rules_weather_out_store["20m"].minute = 20;
56
57
rules_weather_out_store["20m"].second = 0;

58
rules_weather_out_store["30m"].minute = 30;
59
60
rules_weather_out_store["30m"].second = 0;

61
rules_weather_out_store["40m"].minute = 40;
62
63
rules_weather_out_store["40m"].second = 0;

64
rules_weather_out_store["50m"].minute = 50;
65
rules_weather_out_store["50m"].second = 0;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

// 임의의 사용자 데이터 등록
const coordinates = [
  { lat: 37.240049, lng: 131.86931, locCode: 3743011 },
  { lat: 37.206616, lng: 127.037113, locCode: 3124053 },
  { lat: 37.666729, lng: 127.051501, locCode: 1111072 },
  { lat: 35.177681, lng: 128.805934, locCode: 3807063 },
];

// 데이터 저장 용 날짜 생성
const make_date = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month =
    now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
  const date = now.getDate() < 10 ? `0${now.getDate()}` : now.getDate();
  const hour = now.getHours() < 10 ? `0${now.getHours()}` : now.getHours();
83
84
85
86

  let minute = now.getMinutes();
  minute = minute - (minute % 10);
  minute = minute < 10 ? `0${minute}` : minute;
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

  const str_date = `${year}-${month}-${date}T${hour}:${minute}:00+09:00`;
  const collected_at = new Date(str_date);

  return collected_at;
};

// 날씨 저장 스케줄 등록
const weatherOutStoringJob_00m = schedule.scheduleJob(
  rules_weather_out_store["00m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);

const weatherOutStoringJob_10m = schedule.scheduleJob(
  rules_weather_out_store["10m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);

const weatherOutStoringJob_20m = schedule.scheduleJob(
  rules_weather_out_store["20m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);

const weatherOutStoringJob_30m = schedule.scheduleJob(
  rules_weather_out_store["30m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);

const weatherOutStoringJob_40m = schedule.scheduleJob(
  rules_weather_out_store["40m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);

const weatherOutStoringJob_50m = schedule.scheduleJob(
  rules_weather_out_store["50m"],
  () => {
    const date = make_date();
    coordinates.map(({ lat, lng, locCode }) =>
      handleOutData(locCode, date, lat, lng)
    );
  }
);