|
1 | | -import { Schedule } from "./fake-scheduler"; |
2 | | -import { FakeSchedulerServer } from "./fake-scheduler-server"; |
3 | | - |
4 | | -import * as querystring from 'querystring'; |
| 1 | +import * as fs from 'fs'; |
5 | 2 | import * as http from 'http'; |
6 | | -import * as url from 'url'; |
7 | | -import * as fs from 'fs'; |
8 | 3 | import * as path from 'path'; |
| 4 | +import * as querystring from 'querystring'; |
| 5 | +import * as url from 'url'; |
| 6 | +import { Schedule } from './fake-scheduler'; |
| 7 | +import { FakeSchedulerServer } from './fake-scheduler-server'; |
9 | 8 |
|
10 | 9 | const port = 3000; |
11 | 10 |
|
12 | 11 | const mimeTypeResolver = (fileName: string) => { |
13 | | - const extension = path.extname(fileName).toUpperCase(); |
| 12 | + const extension = path.extname(fileName).toUpperCase(); |
| 13 | + |
| 14 | + switch (extension) { |
| 15 | + case '.HTML': |
| 16 | + return 'text/html'; |
| 17 | + case '.CSS': |
| 18 | + return 'text/css'; |
| 19 | + case '.js': |
| 20 | + return 'application/javascript'; |
| 21 | + default: |
| 22 | + return null; |
| 23 | + } |
| 24 | +}; |
14 | 25 |
|
15 | | - switch (extension) { |
16 | | - case '.HTML': return 'text/html'; |
17 | | - case '.CSS': return 'text/css'; |
18 | | - case '.js': return 'application/javascript'; |
19 | | - default: return null; |
20 | | - } |
| 26 | +const SECONDS = (x: number) => x * 1000; |
| 27 | +const MINUTES = (x: number) => SECONDS(60 * x); |
| 28 | + |
| 29 | +const options = { |
| 30 | + version: 'dev', |
| 31 | + quartzVersion: 'nodejs-emulation', |
| 32 | + dotNetVersion: 'none', |
| 33 | + timelineSpan: 3600 * 1000, |
| 34 | + schedulerName: 'DemoScheduler', |
21 | 35 | }; |
22 | 36 |
|
23 | | -const |
24 | | - SECONDS = (x: number) => x * 1000, |
25 | | - MINUTES = (x: number) => SECONDS(60 * x); |
26 | | - |
27 | | -const |
28 | | - options = { |
29 | | - version: 'dev', |
30 | | - quartzVersion: 'nodejs-emulation', |
31 | | - dotNetVersion: 'none', |
32 | | - timelineSpan: 3600 * 1000, |
33 | | - schedulerName: 'DemoScheduler', |
34 | | - }, |
| 37 | +const now = new Date().getTime(); |
35 | 38 |
|
36 | | - now = new Date().getTime(), |
37 | | - |
38 | | - schedule: Schedule = { |
39 | | - 'Maintenance': { |
40 | | - 'DB_Backup': { |
41 | | - duration: SECONDS(20), |
42 | | - triggers: { |
43 | | - 'db_trigger_1': { repeatInterval: MINUTES(1), initialDelay: SECONDS(5) }, |
44 | | - 'db_trigger_2': { repeatInterval: MINUTES(1.5) }, |
45 | | - } |
46 | | - }, |
47 | | - 'Compress_Logs': { |
48 | | - duration: MINUTES(1), |
49 | | - triggers: { |
50 | | - 'logs_trigger_1': { repeatInterval: MINUTES(3) }, |
51 | | - 'logs_trigger_2': { repeatInterval: MINUTES(4), pause: true } |
52 | | - } |
53 | | - } |
| 39 | +const schedule: Schedule = { |
| 40 | + Maintenance: { |
| 41 | + DB_Backup: { |
| 42 | + duration: SECONDS(20), |
| 43 | + triggers: { |
| 44 | + db_trigger_1: { repeatInterval: MINUTES(1), initialDelay: SECONDS(5) }, |
| 45 | + db_trigger_2: { repeatInterval: MINUTES(1.5) }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + Compress_Logs: { |
| 49 | + duration: MINUTES(1), |
| 50 | + triggers: { |
| 51 | + logs_trigger_1: { repeatInterval: MINUTES(3) }, |
| 52 | + logs_trigger_2: { repeatInterval: MINUTES(4), pause: true }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + Domain: { |
| 57 | + Email_Sender: { |
| 58 | + duration: SECONDS(10), |
| 59 | + triggers: { |
| 60 | + email_sender_trigger_1: { repeatInterval: MINUTES(2), repeatCount: 5 }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Remove_Inactive_Users: { |
| 64 | + duration: SECONDS(30), |
| 65 | + triggers: { |
| 66 | + remove_users_trigger_1: { |
| 67 | + repeatInterval: MINUTES(3), |
| 68 | + repeatCount: 5, |
| 69 | + persistAfterExecution: true, |
54 | 70 | }, |
55 | | - 'Domain': { |
56 | | - 'Email_Sender': { |
57 | | - duration: SECONDS(10), |
58 | | - triggers: { |
59 | | - 'email_sender_trigger_1': { repeatInterval: MINUTES(2), repeatCount: 5 } |
60 | | - } |
61 | | - }, |
62 | | - 'Remove_Inactive_Users': { |
63 | | - duration: SECONDS(30), |
64 | | - triggers: { |
65 | | - 'remove_users_trigger_1': { repeatInterval: MINUTES(3), repeatCount: 5, persistAfterExecution: true } |
66 | | - } |
67 | | - } |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + Reporting: { |
| 75 | + 'Daily Sales': { |
| 76 | + duration: MINUTES(7), |
| 77 | + triggers: { |
| 78 | + ds_trigger: { repeatInterval: MINUTES(60) }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + 'Services Health': { |
| 82 | + duration: MINUTES(2), |
| 83 | + triggers: { |
| 84 | + hr_trigger: { repeatInterval: MINUTES(30), startDate: now + MINUTES(1) }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + 'Resource Consumption': { |
| 88 | + duration: MINUTES(1), |
| 89 | + triggers: { |
| 90 | + rc_trigger: { |
| 91 | + repeatInterval: MINUTES(10), |
| 92 | + startDate: now + MINUTES(2), |
| 93 | + endDate: now + MINUTES(40), |
| 94 | + persistAfterExecution: true, |
68 | 95 | }, |
69 | | - 'Reporting': { |
70 | | - 'Daily Sales': { |
71 | | - duration: MINUTES(7), |
72 | | - triggers: { |
73 | | - 'ds_trigger': { repeatInterval: MINUTES(60), } |
74 | | - } |
75 | | - }, |
76 | | - 'Services Health': { |
77 | | - duration: MINUTES(2), |
78 | | - triggers: { |
79 | | - 'hr_trigger': { repeatInterval: MINUTES(30), startDate: now + MINUTES(1) } |
80 | | - } |
81 | | - }, |
82 | | - 'Resource Consumption': { |
83 | | - duration: MINUTES(1), |
84 | | - triggers: { |
85 | | - 'rc_trigger': { repeatInterval: MINUTES(10), startDate: now + MINUTES(2), endDate: now + MINUTES(40), persistAfterExecution: true } |
86 | | - } |
87 | | - } |
88 | | - } |
| 96 | + }, |
89 | 97 | }, |
| 98 | + }, |
| 99 | +}; |
90 | 100 |
|
91 | | - schedulerServer = new FakeSchedulerServer({ |
92 | | - dotNetVersion: options.dotNetVersion, |
93 | | - quartzVersion: options.quartzVersion, |
94 | | - schedule: schedule, |
95 | | - schedulerName: options.schedulerName, |
96 | | - timelineSpan: options.timelineSpan, |
97 | | - version: options.version, |
98 | | - errorEmulation: { |
99 | | - 'get_data': { probability: 1 } |
100 | | - } |
101 | | - }); |
| 101 | +const schedulerServer = new FakeSchedulerServer({ |
| 102 | + dotNetVersion: options.dotNetVersion, |
| 103 | + quartzVersion: options.quartzVersion, |
| 104 | + schedule: schedule, |
| 105 | + schedulerName: options.schedulerName, |
| 106 | + timelineSpan: options.timelineSpan, |
| 107 | + version: options.version, |
| 108 | + errorEmulation: { |
| 109 | + get_data: { probability: 1 }, |
| 110 | + }, |
| 111 | +}); |
102 | 112 |
|
103 | 113 | const requestHandler = (request: any, response: any) => { |
104 | | - const requestUrl = url.parse(request.url, true); |
| 114 | + const requestUrl = url.parse(request.url, true); |
105 | 115 |
|
106 | | - if (request.method === 'GET') { |
107 | | - console.log(request.url); |
| 116 | + if (request.method === 'GET') { |
| 117 | + console.log(request.url); |
108 | 118 |
|
109 | | - const filePath = requestUrl.query.path |
110 | | - ? 'dist/' + requestUrl.query.path |
111 | | - : 'dist/index.html'; |
| 119 | + const filePath = requestUrl.query.path ? 'dist/' + requestUrl.query.path : 'dist/index.html'; |
112 | 120 |
|
113 | | - if (fs.existsSync(filePath)) { |
114 | | - response.writeHead(200, { "Content-Type": mimeTypeResolver(filePath) }); |
115 | | - response.write(fs.readFileSync(filePath)); |
116 | | - response.end(); |
117 | | - return; |
118 | | - } |
119 | | - |
120 | | - response.writeHead(404, { "Content-Type": 'text/plain' }); |
121 | | - response.write('Not found'); |
122 | | - response.end(); |
123 | | - } else { |
124 | | - request.on( |
125 | | - 'data', |
126 | | - (data: any) => { |
127 | | - data = data.toString(); |
128 | | - var POST = querystring.parse(data); |
129 | | - |
130 | | - console.log(POST); |
131 | | - |
132 | | - const result = schedulerServer.handleRequest(POST); |
133 | | - response.writeHead(200, { "Content-Type": 'application/json' }); |
134 | | - response.write(JSON.stringify(result)); |
135 | | - response.end(); |
136 | | - }); |
| 121 | + if (fs.existsSync(filePath)) { |
| 122 | + response.writeHead(200, { 'Content-Type': mimeTypeResolver(filePath) }); |
| 123 | + response.write(fs.readFileSync(filePath)); |
| 124 | + response.end(); |
| 125 | + return; |
137 | 126 | } |
| 127 | + |
| 128 | + response.writeHead(404, { 'Content-Type': 'text/plain' }); |
| 129 | + response.write('Not found'); |
| 130 | + response.end(); |
| 131 | + } else { |
| 132 | + request.on('data', (data: any) => { |
| 133 | + data = data.toString(); |
| 134 | + const POST = querystring.parse(data); |
| 135 | + |
| 136 | + console.log(POST); |
| 137 | + |
| 138 | + const result = schedulerServer.handleRequest(POST); |
| 139 | + response.writeHead(200, { 'Content-Type': 'application/json' }); |
| 140 | + response.write(JSON.stringify(result)); |
| 141 | + response.end(); |
| 142 | + }); |
| 143 | + } |
138 | 144 | }; |
139 | 145 |
|
140 | 146 | const server = http.createServer(requestHandler); |
141 | 147 |
|
142 | 148 | server.listen( |
143 | | - port /*, |
| 149 | + port /*, |
144 | 150 | (err: any) => { |
145 | 151 | if (err) { |
146 | 152 | return console.log('something bad happened', err); |
147 | 153 | } |
148 | 154 |
|
149 | 155 | console.log(`server is listening on ${port}`); |
150 | | - }*/); |
| 156 | + }*/ |
| 157 | +); |
0 commit comments