1
// Copyright 2016 - 2020 The aurora Authors. All rights reserved. Use of this
2
// source code is governed by a MIT license that can be found in the LICENSE
3
// file.
4
//
5
// The aurora is a web-based beanstalkd queue server console written in Go
6
// and works on macOS, Linux and Windows machines. Main idea behind using Go
7
// for backend development is to utilize ability of the compiler to produce
8
// zero-dependency binaries for multiple platforms. aurora was created as an
9
// attempt to build very simple and portable application to work with local or
10
// remote beanstalkd server.
11

12
package main
13

14
import (
15
	"html"
16
	"net/url"
17
	"strconv"
18
	"strings"
19

20
	"github.com/xuri/aurora/beanstalk"
21
)
22

23
// currentTubeJobsShowcase return a section include three stats of job, call
24
// currentTubeJobsShowcaseSections function and get that return value based on
25
// the given server and tube config.
26
func currentTubeJobsShowcase(server string, tube string) string {
27 10
	var buf strings.Builder
28 10
	buf.WriteString(`<section class="jobsShowcase">`)
29 10
	buf.WriteString(currentTubeJobsShowcaseSections(server, tube))
30 10
	buf.WriteString(`</section>`)
31 10
	return buf.String()
32
}
33

34
// currentTubeJobsShowcaseSections constructs a tube job in ready, delayed and
35
// buried stats table based on the given server and tube config.
36
func currentTubeJobsShowcaseSections(server string, tube string) string {
37 10
	stats := []string{"ready", "delayed", "buried"}
38 10
	var err error
39 10
	var buf, s, j, b, m, r strings.Builder
40 10
	var bstkConn *beanstalk.Conn
41 10
	if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
42 10
		return `<hr><div class="pull-left"><h3>Next job in "ready" state</h3></div><div class="clearfix"></div><i>empty</i><hr><div class="pull-left"><h3>Next job in "delayed" state</h3></div><div class="clearfix"></div><i>empty</i><hr><div class="pull-left"><h3>Next job in "buried" state</h3></div><div class="clearfix"></div><i>empty</i>`
43
	}
44 10
	tubeStats := &beanstalk.Tube{
45 10
		Conn: bstkConn,
46 10
		Name: tube,
47
	}
48 10
	peek := []func() (id uint64, body []byte, err error){tubeStats.PeekReady, tubeStats.PeekDelayed, tubeStats.PeekBuried}
49
	for k, stat := range stats {
50 10
		s.Reset()
51 10
		j.Reset()
52 10
		b.Reset()
53 10
		m.Reset()
54 10
		r.Reset()
55 10
		tubes, _ := bstkConn.ListTubes()
56 10
		jobID, jobBody, err := peek[k]()
57 10
		if err != nil {
58 10
			buf.WriteString(`<hr><div class="pull-left"><h3>Next job in "`)
59 10
			buf.WriteString(stat)
60 10
			buf.WriteString(`" state</h3></div><div class="clearfix"></div><i>empty</i>`)
61 10
			continue
62
		}
63 9
		statsJob, err := bstkConn.StatsJob(jobID)
64 9
		if err != nil {
65 0
			continue
66
		}
67
		for _, v := range jobStatsOrder {
68 9
			s.WriteString(`<tr><td>`)
69 9
			s.WriteString(v)
70 9
			s.WriteString(`</td><td>`)
71 9
			s.WriteString(statsJob[v])
72 9
			s.WriteString(`</td></tr>`)
73
		}
74
		for _, v := range tubes {
75 9
			m.WriteString(`<li><a href="?server=`)
76 9
			m.WriteString(server)
77 9
			m.WriteString(`&tube=`)
78 9
			m.WriteString(url.QueryEscape(tube))
79 9
			m.WriteString(`&action=moveJobsTo&destTube=`)
80 9
			m.WriteString(url.QueryEscape(v))
81 9
			m.WriteString(`&state=`)
82 9
			m.WriteString(stat)
83 9
			m.WriteString(`">`)
84 9
			m.WriteString(html.EscapeString(v))
85 9
			m.WriteString(`</a></li>`)
86
		}
87 9
		if jobBody != nil {
88 9
			b.WriteString(`<div class="pull-right"><div style="margin-bottom: 3px;"><a class="btn btn-sm btn-info addSample" data-jobid="`)
89 9
			b.WriteString(strconv.Itoa(int(jobID)))
90 9
			b.WriteString(`" href="?server=`)
91 9
			b.WriteString(server)
92 9
			b.WriteString(`&tube=`)
93 9
			b.WriteString(url.QueryEscape(tube))
94 9
			b.WriteString(`&action=addSample"><i class="glyphicon glyphicon-plus glyphicon-white"></i> Add to samples</a> <div class="btn-group"> <button class="btn btn-info btn-sm dropdown-toggle" data-toggle="dropdown"> <i class="glyphicon glyphicon-arrow-right glyphicon-white"></i> Move all `)
95 9
			b.WriteString(stat)
96 9
			b.WriteString(` to </button><ul class="dropdown-menu"><li><input class="moveJobsNewTubeName input-medium" type="text" data-href="?server=`)
97 9
			b.WriteString(server)
98 9
			b.WriteString(`&tube=`)
99 9
			b.WriteString(url.QueryEscape(tube))
100 9
			b.WriteString(`&action=moveJobsTo&state=`)
101 9
			b.WriteString(stat)
102 9
			b.WriteString(`&destTube=" placeholder="New tube name"/></li>`)
103 9
			b.WriteString(m.String())
104 9
			b.WriteString(`<li class="divider"></li><li><a href="?server=`)
105 9
			b.WriteString(server)
106 9
			b.WriteString(`&tube=`)
107 9
			b.WriteString(url.QueryEscape(tube))
108 9
			b.WriteString(`&action=moveJobsTo&destState=buried&state=`)
109 9
			b.WriteString(stat)
110 9
			b.WriteString(`">Buried</a></li></ul></div> <a class="btn btn-sm btn-danger" href="?server=`)
111 9
			b.WriteString(server)
112 9
			b.WriteString(`&tube=`)
113 9
			b.WriteString(url.QueryEscape(tube))
114 9
			b.WriteString(`&state=`)
115 9
			b.WriteString(stat)
116 9
			b.WriteString(`&action=deleteAll&count=1" onclick="return confirm('This process might hang a while on tubes with lots of jobs. Are you sure you want to continue?');"><i class="glyphicon glyphicon-trash glyphicon-white"></i> Delete all `)
117 9
			b.WriteString(stat)
118 9
			b.WriteString(` jobs</a> <a class="btn btn-sm btn-danger" href="?server=`)
119 9
			b.WriteString(server)
120 9
			b.WriteString(`&tube=`)
121 9
			b.WriteString(url.QueryEscape(tube))
122 9
			b.WriteString(`&state=`)
123 9
			b.WriteString(stat)
124 9
			b.WriteString(`&action=deleteJob&jobid=`)
125 9
			b.WriteString(strconv.Itoa(int(jobID)))
126 9
			b.WriteString(`"><i class="glyphicon glyphicon-remove glyphicon-white"></i> Delete</a></div></div>`)
127
		}
128 9
		if jobBody != nil {
129 9
			j.WriteString(preformat(jobBody))
130
		}
131 9
		if jobBody != nil {
132 9
			r.WriteString(`<hr><div class="pull-left"><h3>Next job in "`)
133 9
			r.WriteString(stat)
134 9
			r.WriteString(`" state</h3></div><div class="clearfix"></div><div class="row show-grid"><div class="col-sm-3"><table class="table"><thead><tr><th>Stats:</th><th>&nbsp;</th></tr></thead><tbody>`)
135 9
			r.WriteString(s.String())
136 9
			r.WriteString(`</tbody></table></div><div class="col-sm-9"><div class="clearfix"><div class="pull-left"><b>Job data:</b></div>`)
137 9
			r.WriteString(b.String())
138 9
			r.WriteString(`</div><pre><code>`)
139 9
			r.WriteString(j.String())
140 9
			r.WriteString(`</code></pre></div></div>`)
141 9
		} else {
142 0
			r.WriteString(`<hr><div class="pull-left"><h3>Next job in "`)
143 0
			r.WriteString(stat)
144 0
			r.WriteString(`" state</h3></div><div class="clearfix"></div><i>empty</i>`)
145
		}
146 9
		buf.WriteString(r.String())
147
	}
148 10
	bstkConn.Close()
149 10
	return buf.String()
150
}

Read our documentation on viewing source code .

Loading