Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ var (
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
reset = string([]byte{27, 91, 48, 109})

functions =[]interface{}{makeWorkPermits,isSelfConcurrent}
functions = []interface{}{makeWorkPermits, isSelfConcurrent}
)

func makeWorkPermits(bufferCapacity int) {
if bufferCapacity <=0 {
if bufferCapacity <= 0 {
workPermits = make(chan struct{}, DEFAULT_JOB_POOL_SIZE)
} else {
workPermits = make(chan struct{}, bufferCapacity)
}
}

func isSelfConcurrent(cocnurrencyFlag int) {
if cocnurrencyFlag <=0 {
if cocnurrencyFlag <= 0 {
selfConcurrent = false
} else {
selfConcurrent = true
Expand All @@ -47,11 +47,10 @@ func isSelfConcurrent(cocnurrencyFlag int) {
func Start(v ...int) {
MainCron = cron.New()

for i,option := range v {
for i, option := range v {
functions[i].(func(int))(option)
}


MainCron.Start()

fmt.Printf("%s[JobRunner] %v Started... %s \n",
Expand Down
14 changes: 9 additions & 5 deletions jobrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ type Job struct {
running sync.Mutex
}

const UNNAMED = "(unnamed)"
const (
UNNAMED = "(unnamed)"
FUNC = "Func"
IDLE = "IDLE"
RUNNING = "RUNNING"
)

func New(job cron.Job) *Job {
name := reflect.TypeOf(job).Name()
if name == "Func" {
if name == FUNC {
name = UNNAMED
}
return &Job{
Expand All @@ -36,10 +41,10 @@ func New(job cron.Job) *Job {

func (j *Job) StatusUpdate() string {
if atomic.LoadUint32(&j.status) > 0 {
j.Status = "RUNNING"
j.Status = UNNAMED
return j.Status
}
j.Status = "IDLE"
j.Status = IDLE
return j.Status

}
Expand Down Expand Up @@ -76,5 +81,4 @@ func (j *Job) Run() {

end := time.Now()
j.Latency = end.Sub(start).String()

}
18 changes: 9 additions & 9 deletions runjob.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// A job runner for executing scheduled or ad-hoc tasks asynchronously from HTTP requests.
//
// It adds a couple of features on top of the Robfig cron package:
// 1. Protection against job panics. (They print to ERROR instead of take down the process)
// 2. (Optional) Limit on the number of jobs that may run simulatenously, to
// limit resource consumption.
// 3. (Optional) Protection against multiple instances of a single job running
// concurrently. If one execution runs into the next, the next will be queued.
// 4. Cron expressions may be defined in app.conf and are reusable across jobs.
// 5. Job status reporting. [WIP]
// 1. Protection against job panics. (They print to ERROR instead of take down the process)
// 2. (Optional) Limit on the number of jobs that may run simulatenously, to
// limit resource consumption.
// 3. (Optional) Protection against multiple instances of a single job running
// concurrently. If one execution runs into the next, the next will be queued.
// 4. Cron expressions may be defined in app.conf and are reusable across jobs.
// 5. Job status reporting. [WIP]
package jobrunner

import (
Expand All @@ -20,7 +20,8 @@ import (
// (Copying the type to this package makes it more visible)
//
// For example:
// jobrunner.Schedule("cron.frequent", jobs.Func(myFunc))
//
// jobrunner.Schedule("cron.frequent", jobs.Func(myFunc))
type Func func()

func (r Func) Run() { r() }
Expand All @@ -38,7 +39,6 @@ func Schedule(spec string, job cron.Job) error {
// The interval provided is the time between the job ending and the job being run again.
// The time that the job takes to run is not included in the interval.
func Every(duration time.Duration, job cron.Job) {

MainCron.Schedule(cron.Every(duration), New(job))
}

Expand Down
18 changes: 8 additions & 10 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ func Entries() []cron.Entry {
}

func StatusPage() []StatusData {

ents := MainCron.Entries()

Statuses := make([]StatusData, len(ents))
for k, v := range ents {
Statuses[k].Id = v.ID
Statuses[k].JobRunner = AddJob(v.Job)
Statuses[k].Next = v.Next
Statuses[k].Prev = v.Prev

Statuses := make([]StatusData, 0, len(ents))
for _, v := range ents {
Statuses = append(Statuses, StatusData{
Id: v.ID,
JobRunner: AddJob(v.Job),
Next: v.Next,
Prev: v.Prev,
})
}

// t := template.New("status_page")
Expand All @@ -42,11 +42,9 @@ func StatusPage() []StatusData {
}

func StatusJson() map[string]interface{} {

return map[string]interface{}{
"jobrunner": StatusPage(),
}

}

func AddJob(job cron.Job) *Job {
Expand Down
136 changes: 84 additions & 52 deletions views/Status.html
Original file line number Diff line number Diff line change
@@ -1,60 +1,92 @@
<html>
<head>
<style>
body {
margin: 30px 0 0 0;
font-size: 11px;
font-family: sans-serif;
color: #345;

}
h1 {
font-size: 24px;
text-align: center;
padding: 10 0 30px;
}
table {
/*max-width: 80%;*/
margin: 0 auto;
border-collapse: collapse;
border: none;
}
table td, table th {
min-width: 25px;
width: auto;
padding: 15px 20px;
border: none;
}


table tr:nth-child(odd) {
background-color: #f0f0f0;
}
table tr:nth-child(1) {
background-color: #345;
color: white;
}
th {
text-align: left;
}
:root {
--bg-color: #ffffff;
--text-color: #345;
--header-bg: #345;
--header-text: #ffffff;
--odd-row-bg: #f0f0f0;
--table-border: #ddd;
}

@media (prefers-color-scheme: dark) {
:root {
--bg-color: #0c0c0c;
--text-color: #00ff00;
--header-bg: #1a1a1a;
--header-text: #00ff41;
--odd-row-bg: #111111;
--table-border: #333;
}
}

body {
margin: 30px 0 0 0;
font-size: 11px;
font-family: sans-serif;
color: var(--text-color);
background-color: var(--bg-color);
transition: background-color 0.3s ease, color 0.3s ease;
}

h1 {
font-size: 24px;
text-align: center;
padding: 10 0 30px;
color: var(--text-color);
}

table {
margin: 0 auto;
border-collapse: collapse;
border: none;
}

table td, table th {
min-width: 25px;
width: auto;
padding: 15px 20px;
border: none;
color: var(--text-color);
transition: background-color 0.3s ease, color 0.3s ease;
}

table tr:nth-child(odd) {
background-color: var(--odd-row-bg);
}

table tr:nth-child(1) {
background-color: var(--header-bg);
color: var(--header-text);
}

table tr:nth-child(1) td,
table tr:nth-child(1) th {
color: var(--header-text);
}

th {
text-align: left;
}
</style>
</head>
<body>
<h1>JobRunner Status Report</h1>

<table>
<tr><th>ID</th><th>Name</th><th>Status</th><th>Last run</th><th>Next run</th><th>Latency</th></tr>

<h1>JobRunner Status Report</h1>

<table>
<tr><th>ID</th><th>Name</th><th>Status</th><th>Last run</th><th>Next run</th><th>Latency</th></tr>
{{range .}}

<tr>
<td>{{.Id}}</td>
<td>{{.JobRunner.Name}}</td>
<td>{{.JobRunner.Status}}</td>
<td>{{if not .Prev.IsZero}}{{.Prev.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{if not .Next.IsZero}}{{.Next.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{.JobRunner.Latency}}</td>
</tr>
{{end}}
</table>
{{range .}}
<tr>
<td>{{.Id}}</td>
<td>{{.JobRunner.Name}}</td>
<td>{{.JobRunner.Status}}</td>
<td>{{if not .Prev.IsZero}}{{.Prev.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{if not .Next.IsZero}}{{.Next.Format "2006-01-02 15:04:05"}}{{end}}</td>
<td>{{.JobRunner.Latency}}</td>
</tr>
{{end}}
</table>
</body>
</html>