summaryrefslogtreecommitdiff
path: root/cmd/getandpurgequeue/main.go
blob: e5466d36987fe774ac8ab1490229942abb640728 (plain)
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
// Copyright 2019 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

// getandpurgequeue gets and deletes all messages from a queue. This can
// be useful for debugging queue issues.
package main

import (
	"flag"
	"fmt"
	"log"

	"rescribe.xyz/bookpipeline"
)

const usage = `Usage: getandpurgequeue qname

getandpurgequeue gets and deletes all messages from a queue.

This can be useful for debugging queue issues.

Valid queue names:
- preprocess
- wipeonly
- ocrpage
- analyse
- test
`

type QueuePipeliner interface {
	Init() error
	LogAndPurgeQueue(url string) error
	PreQueueId() string
	WipeQueueId() string
	OCRPageQueueId() string
	AnalyseQueueId() string
	TestQueueId() string
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() != 1 {
		flag.Usage()
		return
	}

	var conn QueuePipeliner
	conn = &bookpipeline.AwsConn{Region: "eu-west-2"}

	err := conn.Init()
	if err != nil {
		log.Fatalln("Error setting up cloud connection:", err)
	}

	qdetails := []struct {
		id, name string
	}{
		{conn.PreQueueId(), "preprocess"},
		{conn.WipeQueueId(), "wipeonly"},
		{conn.OCRPageQueueId(), "ocrpage"},
		{conn.AnalyseQueueId(), "analyse"},
		{conn.TestQueueId(), "test"},
	}

	qname := flag.Arg(0)

	var qid string
	for i, n := range qdetails {
		if n.name == qname {
			qid = qdetails[i].id
			break
		}
	}
	if qid == "" {
		log.Fatalln("Error, no queue named", qname)
	}

	err = conn.LogAndPurgeQueue(qid)
	if err != nil {
		log.Fatalln("Error getting and purging queue", qname, ":", err)
	}
}