Skip to content

Commit 4c677b8

Browse files
authored
Implement job status property in 202 responses (#53)
1 parent 6adedcd commit 4c677b8

7 files changed

Lines changed: 57 additions & 43 deletions

File tree

handlers/getAdventurer.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
3939
return
4040
}
4141

42-
if tasksQuantityExceeded := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget); tasksQuantityExceeded {
42+
if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurer(r.Header.Get("CF-Connecting-IP"), region, profileTarget); tasksQuantityExceeded {
4343
w.WriteHeader(http.StatusTooManyRequests)
4444
json.NewEncoder(w).Encode(map[string]string{
4545
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
"status": "rejected",
47+
})
48+
} else {
49+
w.WriteHeader(http.StatusAccepted)
50+
json.NewEncoder(w).Encode(map[string]string{
51+
"message": "Player profile is being fetched. Please try again later.",
52+
"status": map[bool]string{
53+
true: "started",
54+
false: "pending",
55+
}[taskAdded],
4656
})
47-
48-
return
4957
}
50-
51-
w.WriteHeader(http.StatusAccepted)
52-
json.NewEncoder(w).Encode(map[string]string{
53-
"message": "Player profile is being fetched. Please try again later.",
54-
})
5558
}

handlers/getAdventurerSearch.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
4242
return
4343
}
4444

45-
if tasksQuantityExceeded := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType); tasksQuantityExceeded {
45+
if taskAdded, tasksQuantityExceeded := scraper.EnqueueAdventurerSearch(r.Header.Get("CF-Connecting-IP"), region, query, searchType); tasksQuantityExceeded {
4646
w.WriteHeader(http.StatusTooManyRequests)
4747
json.NewEncoder(w).Encode(map[string]string{
4848
"message": "You have exceeded the maximum number of concurrent tasks.",
49+
"status": "rejected",
50+
})
51+
} else {
52+
w.WriteHeader(http.StatusAccepted)
53+
json.NewEncoder(w).Encode(map[string]string{
54+
"message": "Player search is being fetched. Please try again later.",
55+
"status": map[bool]string{
56+
true: "started",
57+
false: "pending",
58+
}[taskAdded],
4959
})
50-
51-
return
5260
}
53-
54-
w.WriteHeader(http.StatusAccepted)
55-
json.NewEncoder(w).Encode(map[string]string{
56-
"message": "Player search is being fetched. Please try again later.",
57-
})
5861
}

handlers/getGuild.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
3939
return
4040
}
4141

42-
if tasksQuantityExceeded := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
42+
if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuild(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
4343
w.WriteHeader(http.StatusTooManyRequests)
4444
json.NewEncoder(w).Encode(map[string]string{
4545
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
"status": "rejected",
47+
})
48+
} else {
49+
w.WriteHeader(http.StatusAccepted)
50+
json.NewEncoder(w).Encode(map[string]string{
51+
"message": "Guild profile is being fetched. Please try again later.",
52+
"status": map[bool]string{
53+
true: "started",
54+
false: "pending",
55+
}[taskAdded],
4656
})
47-
48-
return
4957
}
50-
51-
w.WriteHeader(http.StatusAccepted)
52-
json.NewEncoder(w).Encode(map[string]string{
53-
"message": "Guild profile is being fetched. Please try again later.",
54-
})
5558
}

handlers/getGuildSearch.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
3939
return
4040
}
4141

42-
if tasksQuantityExceeded := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
42+
if taskAdded, tasksQuantityExceeded := scraper.EnqueueGuildSearch(r.Header.Get("CF-Connecting-IP"), region, name); tasksQuantityExceeded {
4343
w.WriteHeader(http.StatusTooManyRequests)
4444
json.NewEncoder(w).Encode(map[string]string{
4545
"message": "You have exceeded the maximum number of concurrent tasks.",
46+
"status": "rejected",
47+
})
48+
} else {
49+
w.WriteHeader(http.StatusAccepted)
50+
json.NewEncoder(w).Encode(map[string]string{
51+
"message": "Guild search is being fetched. Please try again later.",
52+
"status": map[bool]string{
53+
true: "started",
54+
false: "pending",
55+
}[taskAdded],
4656
})
47-
48-
return
4957
}
50-
51-
w.WriteHeader(http.StatusAccepted)
52-
json.NewEncoder(w).Encode(map[string]string{
53-
"message": "Guild search is being fetched. Please try again later.",
54-
})
5558
}

handlers/getStatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var initTime = time.Now()
15-
var version = "1.14.1"
15+
var version = "1.15.1"
1616

1717
func getStatus(w http.ResponseWriter, r *http.Request) {
1818
json.NewEncoder(w).Encode(map[string]interface{}{

scraper/scraper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ func InitScraper() {
139139
})
140140
}
141141

142-
func createTask(clientIP, region, taskType string, query map[string]string) (tasksQuantityExceeded bool) {
142+
func createTask(clientIP, region, taskType string, query map[string]string) (taskAdded, tasksQuantityExceeded bool) {
143143
crc32 := crc32.NewIEEE()
144144
crc32.Write([]byte(strings.Join(append(slices.Sorted(maps.Values(query)), region, taskType), "")))
145145
hashString := strconv.Itoa(int(crc32.Sum32()))
146146

147147
if taskQueue.CountQueuedTasksForClient(clientIP) >= viper.GetInt("maxtasksperclient") {
148-
return true
148+
return false, true
149149
}
150150

151151
url := fmt.Sprintf(
@@ -170,17 +170,17 @@ func createTask(clientIP, region, taskType string, query map[string]string) (tas
170170
"taskType": taskType,
171171
})
172172

173-
taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
174-
return false
173+
added := taskQueue.AddTask(clientIP, hashString, utils.BuildRequest(url, query))
174+
return added, false
175175
}
176176

177-
func EnqueueAdventurer(clientIP, region, profileTarget string) (tasksQuantityExceeded bool) {
177+
func EnqueueAdventurer(clientIP, region, profileTarget string) (taskAdded, tasksQuantityExceeded bool) {
178178
return createTask(clientIP, region, "player", map[string]string{
179179
"profileTarget": profileTarget,
180180
})
181181
}
182182

183-
func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (tasksQuantityExceeded bool) {
183+
func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (taskAdded, tasksQuantityExceeded bool) {
184184
return createTask(clientIP, region, "playerSearch", map[string]string{
185185
"Page": "1",
186186
"region": region,
@@ -189,14 +189,14 @@ func EnqueueAdventurerSearch(clientIP, region, query, searchType string) (tasksQ
189189
})
190190
}
191191

192-
func EnqueueGuild(clientIP, region, name string) (tasksQuantityExceeded bool) {
192+
func EnqueueGuild(clientIP, region, name string) (taskAdded, tasksQuantityExceeded bool) {
193193
return createTask(clientIP, region, "guild", map[string]string{
194194
"guildName": name,
195195
"region": region,
196196
})
197197
}
198198

199-
func EnqueueGuildSearch(clientIP, region, query string) (tasksQuantityExceeded bool) {
199+
func EnqueueGuildSearch(clientIP, region, query string) (taskAdded, tasksQuantityExceeded bool) {
200200
return createTask(clientIP, region, "guildSearch", map[string]string{
201201
"page": "1",
202202
"region": region,

scraper/taskQueue.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewTaskQueue(bufferSize int) *TaskQueue {
3232
return queue
3333
}
3434

35-
func (q *TaskQueue) AddTask(clientIP, hash, url string) {
35+
func (q *TaskQueue) AddTask(clientIP, hash, url string) (added bool) {
3636
fullURL := utils.BuildRequest(url, map[string]string{
3737
"taskClient": clientIP,
3838
"taskHash": hash,
@@ -41,7 +41,7 @@ func (q *TaskQueue) AddTask(clientIP, hash, url string) {
4141
q.mutex.Lock()
4242
if duplicate := slices.Contains(q.hashes, hash); duplicate {
4343
q.mutex.Unlock()
44-
return
44+
return false
4545
}
4646
q.clientIPs[clientIP]++
4747
q.hashes = append(q.hashes, hash)
@@ -52,6 +52,8 @@ func (q *TaskQueue) AddTask(clientIP, hash, url string) {
5252
Hash: hash,
5353
URL: fullURL,
5454
}
55+
56+
return true
5557
}
5658

5759
func (q *TaskQueue) run() {

0 commit comments

Comments
 (0)