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
// currentTubeJobsActionsRow render a section include kick, pause and unpause
24
// job button by given server and tube.
25
func currentTubeJobsActionsRow(server string, tube string) string {
26 10
	var err error
27 10
	var bstkConn *beanstalk.Conn
28 10
	var buf, pauseTimeLeft strings.Builder
29 10
	var pause = strconv.Itoa(selfConf.TubePauseSeconds)
30 10
	if pause == "-1" {
31 10
		pause = "3600"
32
	}
33 10
	if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
34 10
		return ``
35
	}
36 10
	tubeStats := &beanstalk.Tube{
37 10
		Conn: bstkConn,
38 10
		Name: tube,
39
	}
40 10
	statsMap, _ := tubeStats.Stats()
41 10
	if statsMap["pause-time-left"] == "0" {
42 10
		pauseTimeLeft.WriteString(`<a class="btn btn-default btn-sm" href="?server=`)
43 10
		pauseTimeLeft.WriteString(server)
44 10
		pauseTimeLeft.WriteString(`&tube=`)
45 10
		pauseTimeLeft.WriteString(url.QueryEscape(tube))
46 10
		pauseTimeLeft.WriteString(`&action=pause&count=-1" title="Temporarily prevent jobs being reserved from the given tube. Pause for: `)
47 10
		pauseTimeLeft.WriteString(pause)
48 10
		pauseTimeLeft.WriteString(` seconds"><i class="glyphicon glyphicon-pause"></i> Pause tube</a>`)
49 10
	} else {
50 10
		pauseTimeLeft.WriteString(`<a class="btn btn-default btn-sm" href="?server=`)
51 10
		pauseTimeLeft.WriteString(server)
52 10
		pauseTimeLeft.WriteString(`&tube=`)
53 10
		pauseTimeLeft.WriteString(url.QueryEscape(tube))
54 10
		pauseTimeLeft.WriteString(`&action=pause&count=0" title="Pause seconds left: `)
55 10
		pauseTimeLeft.WriteString(statsMap["pause-time-left"])
56 10
		pauseTimeLeft.WriteString(`"><i class="glyphicon glyphicon-play"></i> Unpause tube</a>`)
57
	}
58 10
	bstkConn.Close()
59 10
	buf.WriteString(`<section id="actionsRow"><b>Actions:</b> &nbsp;<a class="btn btn-default btn-sm" href="?server=`)
60 10
	buf.WriteString(server)
61 10
	buf.WriteString(`&tube=`)
62 10
	buf.WriteString(url.QueryEscape(tube))
63 10
	buf.WriteString(`&action=kick&count=1"><i class="glyphicon glyphicon-forward"></i> Kick 1 job</a> <form method="GET"><div class="btn-group" role="group"><button type="submit" class="btn btn-default btn-sm" style="margin-right: -2px;"><i class="glyphicon glyphicon-fast-forward"></i> Kick more </button><input type="hidden" name="server" value="`)
64 10
	buf.WriteString(server)
65 10
	buf.WriteString(`"><input type="hidden" name="tube" value="`)
66 10
	buf.WriteString(url.QueryEscape(tube))
67 10
	buf.WriteString(`"><input type="hidden" name="action" value="kick"><input type="number" value="10" name="count" min="0" step="1" size="4" class="form-control input-sm" style="padding: 5px 2px 5px 12px; text-align: center;"></div></form> `)
68 10
	buf.WriteString(pauseTimeLeft.String())
69 10
	buf.WriteString(` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <div class="btn-group"><a data-toggle="modal" class="btn btn-success btn-sm" href="#" id="addJob"><i class="glyphicon glyphicon-plus-sign glyphicon-white"></i> Add job</a><button class="btn btn-success btn-sm dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu">`)
70 10
	buf.WriteString(currentTubeJobsActionsRowSample(server, tube))
71 10
	buf.WriteString(`</ul></div></section>`)
72 10
	return buf.String()
73
}
74

75
// currentTubeJobsActionsRowSample render a dropdown sample list by given server
76
// and tube.
77
func currentTubeJobsActionsRowSample(server string, tube string) string {
78 10
	sample := strings.Builder{}
79
	for _, v := range sampleJobs.Tubes {
80 9
		if v.Name != tube {
81 9
			continue
82
		}
83 9
		if len(v.Keys) == 0 {
84 0
			continue
85
		}
86
		for _, k := range v.Keys {
87
			for _, j := range sampleJobs.Jobs {
88 9
				if j.Key != k {
89 9
					continue
90
				}
91 9
				sample.WriteString(`<li><a href="?server=`)
92 9
				sample.WriteString(server)
93 9
				sample.WriteString(`&tube=`)
94 9
				sample.WriteString(url.QueryEscape(tube))
95 9
				sample.WriteString(`&action=loadSample&key=`)
96 9
				sample.WriteString(j.Key)
97 9
				sample.WriteString(`">`)
98 9
				sample.WriteString(html.EscapeString(j.Name))
99 9
				sample.WriteString(`</a></li>`)
100
			}
101
		}
102
	}
103 10
	if sample.String() == "" {
104 10
		return `<li><a href="javascript:void(0);">There are no sample jobs</a></li>`
105
	}
106 9
	sample.WriteString(`<li class="divider"></li><li><a href="./sample?action=manageSamples">Manage samples</a></li>`)
107 9
	return sample.String()
108
}

Read our documentation on viewing source code .

Loading