-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron.go
126 lines (100 loc) · 3.45 KB
/
cron.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Mesostic Scheduler and Tasks
*/
package main
import (
"os"
"strings"
"time"
"github.com/go-co-op/gocron"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
)
// Channel for NASAapod Mesostic publishing
var nasaNewMESO = make(chan string)
// fetchCron ::: Cron emulator, currently just a single job. ffs == fetch frequency in seconds
func fetchCron(ffs uint64) {
// NASA official Astronomy Picture of the Day endpoint URL using a freely available API key
apodnow := "https://api.nasa.gov/planetary/apod?api_key=Ijb0zLeEt71HMQdy8YjqB583FK3bdh1yThVJYzpu"
apodenv := "HPSCHD_NASA_APOD_URL" // Optional ENV VAR
url := envVar(apodenv, apodnow) // NASA APOD URL to query, default if no ENV VAR
// Start a new fetch job immediately, followed every ffs seconds.
fcron := gocron.NewScheduler(time.UTC)
_, ferr := fcron.Every(ffs).Seconds().StartImmediately().Do(NASAetl, url)
if ferr != nil {
log.Error()
}
defer fcron.StartBlocking()
}
// NASAetl ::: Retrieve Astronomy Picture of the Day (APOD) metadata,
// process it through the Mesostic engine, save it in a library of ephemeral copies,
// pass the new data point (filename path) to a channel for use with displays.
func NASAetl(url string) {
hTimer := prometheus.NewTimer(hpschdNASAetlTimer)
defer hTimer.ObserveDuration()
_, _, fu := Envelope()
log.Info().
Str("fu", fu).
Msg("NASA APOD Mesostic Begin")
// the title as the spine, for now :)
date, spine, source := fetchSource(url)
// There is typically a long stretch of time from ~0000UTC to
// sometime the next morning while the APOD for the next day is being updated.
// NASA APOD API will return: 'no data available for date: YYYY-MM-DD'
//
// Trigger a new fetch for a new mesostic added to the store and quit.
if spine == "404" {
go NASAetl(fetchRandURL())
log.Warn().
Str("fu", fu).
Str("code", "404").
Msg("Remote data not available, randomized ETL triggered.")
return
}
// we don't want spaces in the spine string
trcc := strings.NewReplacer(" ", "")
spn := trcc.Replace(spine)
// convert each phrase into a line by replacing commas and periods with newlines.
trnl := strings.NewReplacer(". ", "\n", ", ", "\n")
source = trnl.Replace(source)
// get a mesostic
// this mimics the JSON API calls
// which will probably need to be revisited once this section is done
tmpFileName := fileTmp(&spn, &source)
mcMeso := make(chan string)
go mesoMain(tmpFileName, spn, mcMeso)
showR := <-mcMeso
// create new Mesostic file
mesoFile, created := apodNew(&spine, &date, &showR)
// If the mesostic file already exists, no more action is needed.
// Trigger a new fetch for a new mesostic added to the store and quit.
// TODO: This check should go *before* creating the mesostic at all.
// e.g. construct the filename and check against dirents()
if !created {
go NASAetl(fetchRandURL())
log.Warn().
Str("fu", fu).
Str("code", "204").
Msg("Local mesostic exists, randomized ETL triggered.")
return
}
// remove the tmp source file
var ferr = os.Remove(tmpFileName)
if ferr != nil {
log.Error()
}
// push filename of new Mesostic
// for now this is a non-buffered blocking operation
nasaNewMESO <- mesoFile
log.Info().
Str("spinestring", spine).
Str("filename", mesoFile).
Msg("NASA APOD Mesostic End")
log.Debug().
Str("fu", fu).
Str("fetchdate", date).
Str("spinestring", spine).
Str("filename", mesoFile).
Str("mesostic", showR).
Msg("NASA APOD Mesostic End")
}