-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_job.js
113 lines (98 loc) · 3.48 KB
/
run_job.js
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
const DriveConnector = require('./DriveConnector')
const CamConnector = require('./CamConnector')
const LocalConnector = require('./LocalConnector')
const Config = require('./Config')
const Tracer = require('./Tracer')
const CodeUpdater = require('./CodeUpdater')
const tracer = new Tracer()
const localConnector = new LocalConnector(tracer)
const driveConnector = new DriveConnector(tracer, localConnector)
const codeUpdater = new CodeUpdater(tracer)
const camConnector = new CamConnector(tracer)
const schedule = require('node-schedule');
const fs = require('fs')
const moment = require('moment')
const DAYS_MAPPING = {"l": 1, "m": 2, "x": 3, "j": 4, "v":5, "s":6, "d": 0}
const CONFIG_RELOAD_TIME = 24*60*60*1000
let job = null
let dailyJob = null
let codeUpdateJob = null
let previousConfig = null
function cancelJobs() {
if (job) job.cancel()
if (dailyJob) dailyJob.cancel()
}
function cronFromConfig(config) {
const frequency = config.frequency
const cronFrequency = parseInt(frequency) > 0 ? `*/${frequency}` : 0
let hours = '*'
let daysOfWeek = '*'
if (config.startHour && config.endHour)
hours = `${config.startHour}-${config.endHour}`
if (config.daysOfWeek) {
daysOfWeek = config.daysOfWeek.split(',').map(d => DAYS_MAPPING[d]).join(',')
}
return `0 ${cronFrequency} ${hours} * * ${daysOfWeek}`
}
async function processPhoto(config) {
tracer.log('Processing photo')
const localDir = await localConnector.createDailyDir(config)
const pictureCameraPath = await camConnector.takePhoto()
const rawEnabled = config.rawEnabled
await camConnector.downloadFile(pictureCameraPath, rawEnabled, localDir)
await camConnector.deleteFile(pictureCameraPath)
await driveConnector.uploadFiles(localConnector.photos_dir)
return
}
async function processDailyPhoto(config) {
const localDir = await localConnector.createDailyDir(config)
const pictureCameraPath = await camConnector.takePhoto()
const filePath = await camConnector.downloadFile(pictureCameraPath, false, localDir)
await camConnector.deleteFile(pictureCameraPath)
await driveConnector.uploadDailyFile(filePath, config)
}
async function loadConfig() {
try {
tracer.log('Loading config')
const config = await Config.getConfig(driveConnector)
tracer.log('Config loaded')
if (!job || !previousConfig || config.stringify() !== previousConfig.stringify()) {
console.log('Updating job with cron ', cronFromConfig(config), ' at ', new Date())
if (job) job.cancel()
if (dailyJob) dailyJob.cancel()
job = schedule.scheduleJob(cronFromConfig(config), () => {
tracer.log(`Executing job at ${new Date()}`)
try {
processPhoto(config)
} catch(err) {
tracer.error('Error processing photo')
tracer.error(err)
}
});
if (config.dailyEnabled) {
dailyJob = schedule.scheduleJob('26 0 * * 0-6', () => {
tracer.log(`Executing daily job at ${new Date()}`)
try {
processDailyPhoto(config)
} catch(err) {
tracer.error('Error processing daily photo')
tracer.error(err)
}
})
}
previousConfig = new Config(config._config)
}
if (config.updateCode) {
await codeUpdater.updateCode()
}
} catch(err) {
tracer.error(err)
}
}
configJob = schedule.scheduleJob('*/5 * * * *', () => {
loadConfig().catch(err => trace.error(err))
});
loadConfig().catch(err => trace.error(err))
module.exports.stop = () => {
cancelJobs()
}