schedules.js 4.23 KB
Newer Older
1
2
import db from "./db/index";
import envs from "../config/config";
3
4
import schedule from "node-schedule";
import { spawn } from "child_process";
5
import { handleOutData } from "./controllers/dataController";
6
7
8
9
10

// Data Processing Python Codes Directory - server directory에서 실행
const DATA_PROCESSING_DIR = "./src/data_processing/main.py";

// 매일 자정에 실행할 스케줄의 규칙
11
12
13
14
const rule_dataProcessing = new schedule.RecurrenceRule();
rule_dataProcessing.hour = 0;
rule_dataProcessing.minute = 0;
rule_dataProcessing.second = 0;
15
16

// 매일 자정에 실행되는 데이터 처리 스케줄
17
const dataProcessingJob = schedule.scheduleJob(rule_dataProcessing, () => {
18
19
20
21
22
23
24
25
26
27
  const today = new Date();

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

  const pyprocess = spawn("python", [
    DATA_PROCESSING_DIR,
28
29
30
31
    envs.db.host,
    envs.db.user,
    envs.db.password,
    envs.db.database,
32
33
34
35
  ]);

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

39
  pyprocess.stderr.on("data", (error) => {
40
    console.log("Error in the data processing.");
41
    console.log(error.toString());
42
43
44
45
46
47
  });

  pyprocess.on("close", () => {
    console.log("The data processing done.");
  });
});
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

// 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;
rules_weather_out_store["10m"].minute = 10;
rules_weather_out_store["20m"].minute = 20;
rules_weather_out_store["30m"].minute = 30;
rules_weather_out_store["40m"].minute = 40;
rules_weather_out_store["50m"].minute = 50;

// 임의의 사용자 데이터 등록
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();
82
83
84
85

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

  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)
    );
  }
);