Skip to content

Commit

Permalink
Added a logger (#5)
Browse files Browse the repository at this point in the history
* Import changes

* Tidied go.mod

* Removed crontab/log.go

* Reverted migrations.sql, changed up.sql
  • Loading branch information
diamondburned authored and titpetric committed Jun 22, 2019
1 parent 61da22a commit c7db70c
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 53 deletions.
2 changes: 2 additions & 0 deletions cron.scripts/api_exit123
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exit 123
4 changes: 2 additions & 2 deletions cron.scripts/api_healthcheck
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
echo $0
#!/usr/bin/env bash
echo $0
2 changes: 1 addition & 1 deletion cron.scripts/api_logtiming
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
SLEEP_TIME=$(( ( RANDOM % 10 ) + 1 ))
echo $0 " sleep for " $SLEEP_TIME
sleep $SLEEP_TIME
2 changes: 1 addition & 1 deletion cron.scripts/api_stats
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
#!/usr/bin/env bash
echo $0
sleep 1
5 changes: 3 additions & 2 deletions cron.scripts/api_weather
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/bash
echo $0
#!/usr/bin/env bash
echo $0
curl -s http://wttr.in/?0QT
2 changes: 1 addition & 1 deletion cron.scripts/ava_stats
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
#!/usr/bin/env bash
echo $0
5 changes: 3 additions & 2 deletions crontab/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"

"github.com/titpetric/factory/resputil"
"github.com/titpetric/go-web-crontab/logger"
)

type API struct {
Expand Down Expand Up @@ -51,8 +52,8 @@ func (API) New(opts *APIOptions) (*API, error) {
},
Logs: func(w http.ResponseWriter, r *http.Request) {
response := &struct {
Job *JobItem `json:"job"`
Logs []Log `json:"logs"`
Job *JobItem `json:"job"`
Logs []logger.LogEntry `json:"logs"`
}{}

request := func() (err error) {
Expand Down
5 changes: 3 additions & 2 deletions crontab/crontab_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crontab

import (
"github.com/pkg/errors"
"github.com/titpetric/go-web-crontab/logger"
)

type CrontabJobs struct {
Expand Down Expand Up @@ -50,8 +51,8 @@ func (c *CrontabJobs) Get(id string) (*JobItem, error) {
return nil, errors.New("No matching job: " + id)
}

func (c *CrontabJobs) Logs(id string) ([]Log, error) {
logs := []Log{}
func (c *CrontabJobs) Logs(id string) ([]logger.LogEntry, error) {
logs := []logger.LogEntry{}
err := c.cron.db.Select(&logs, "select name, description from logs order by name asc")
return logs, err
}
Expand Down
46 changes: 30 additions & 16 deletions crontab/job.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package crontab

import (
"os"
"strings"
"time"

"os/exec"

"github.com/pkg/errors"
"github.com/titpetric/factory"
"github.com/titpetric/go-web-crontab/logger"
)

type JobItem struct {
Expand Down Expand Up @@ -37,35 +36,50 @@ func (job *JobItem) Run(cron *Crontab) error {
if !job.CanRun() {
return nil
}

defer job.Done()

command := strings.Split(job.Command, " ")
// Make a new logger. This takes in the stdout and stderr, log them into
// both the application's std{out,err} and, when Finish() is called,
// finalizes everything and write it to the database.
var jobLog = logger.NewLog(job.Name)

jobLog := Log{
Name: job.Name,
Stamp: time.Now(),
}
command := strings.Split(job.Command, " ")

cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdout = jobLog.Stdout()
cmd.Stderr = jobLog.Stderr()

if err := cmd.Start(); err != nil {
// Log when a task fails
if _, err := jobLog.Finish(cron.db, err); err != nil {
return errors.Wrap(err, "Couldn't run job "+job.Name+" and save to db")
}

return errors.Wrap(err, "Can't run command")
}

done := make(chan error, 1)
var done = make(chan error, 1)
go func() {
done <- cmd.Wait()
}()

select {
case <-job.cancel:
return errors.Wrap(cmd.Process.Kill(), "Killing process "+job.Name)
case err := <-done:
if err != nil {
return errors.Wrap(err, "Unexpected error when running "+job.Name)
if err := cmd.Process.Kill(); err != nil {
if _, dberr := jobLog.Finish(cron.db, err); dberr != nil {
return errors.Wrap(dberr, "Couldn't stop job "+job.Name+" and save to db")
}

return errors.Wrap(err, "Couldn't stop job "+job.Name)
}
case cmdError := <-done:
if _, err := jobLog.Finish(cron.db, cmdError); err != nil {
return errors.Wrap(err, "Couldn't finish job "+job.Name+" and save to db")
}
jobLog.Duration = time.Since(jobLog.Stamp)
return errors.Wrap(jobLog.save(cron.db), "Can't save "+job.Name+" run to db")

return errors.Wrap(cmdError, "Couldn't finish job "+job.Name)
}

return nil
}
19 changes: 0 additions & 19 deletions crontab/log.go

This file was deleted.

2 changes: 1 addition & 1 deletion db/mysql/static.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/schema/mysql/20190619213353.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `logs` ADD `output` JSON NOT NULL AFTER `name`, ADD `exit_code` INT NOT NULL AFTER `output`;
4 changes: 2 additions & 2 deletions db/schema/new.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
touch mysql/$(date +%Y%m%d%H%M%S).up.sql
#!/bin/sh
touch mysql/$(date +%Y%m%d%H%M%S).up.sql
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ go 1.12

require (
github.com/SentimensRG/sigctx v0.0.0-20171003180858-c19b774db63b
github.com/apex/log v1.1.0
github.com/fatih/color v1.7.0 // indirect
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-chi/cors v1.0.0
github.com/goware/statik v0.2.0
github.com/jmoiron/sqlx v1.2.0
github.com/joho/godotenv v1.3.0
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/namsral/flag v1.7.4-pre
github.com/pkg/errors v0.8.1
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
github.com/stretchr/testify v1.3.0 // indirect
github.com/titpetric/factory v0.0.0-20190411141538-ce6800c0c52f
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
google.golang.org/appengine v1.1.0 // indirect
)
22 changes: 18 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
github.com/SentimensRG/sigctx v0.0.0-20171003180858-c19b774db63b h1:L8UMHZvKunbxWEqnarh7TDm/EwDu02ryCS4noVcH//M=
github.com/SentimensRG/sigctx v0.0.0-20171003180858-c19b774db63b/go.mod h1:F+s/TOqT6eVQiBSyc6l7zrGa4BzWiyqexJZYd1ns6yw=
github.com/apex/log v1.1.0 h1:J5rld6WVFi6NxA6m8GJ1LJqu3+GiTFIt3mYv27gdQWI=
github.com/apex/log v1.1.0/go.mod h1:yA770aXIDQrhVOIGurT/pVdfCpSq1GQV/auzMN5fzvY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/cors v1.0.0 h1:e6x8k7uWbUwYs+aXDoiUzeQFT6l0cygBYyNhD7/1Tg0=
github.com/go-chi/cors v1.0.0/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/goware/statik v0.2.0 h1:2dKnJIawSr/qbd4TdSgRtNc6mdVZrTOR56aSiL47460=
github.com/goware/statik v0.2.0/go.mod h1:Fktf+coYRC3SB2RfBB++LAG6ojA/VzuDp0Jfd064ICs=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 h1:x7xEyJDP7Hv3LVgvWhzioQqbC/KtuUhTigKlH/8ehhE=
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/sony/sonyflake v0.0.0-20181109022403-6d5bd6181009 h1:3wBL/e/qjpSYaXacpbIV+Bsj/nwQ4UO1llG/av54zzw=
github.com/sony/sonyflake v0.0.0-20181109022403-6d5bd6181009/go.mod h1:dVvZuWJd174umvm5g8CmZD6S2GWwHKtpK/0ZPHswuNo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/titpetric/factory v0.0.0-20190411141538-ce6800c0c52f h1:urYfAVydn8x5DJhG1BeGkj9fS9vGlF26BNNVwXQS1D4=
github.com/titpetric/factory v0.0.0-20190411141538-ce6800c0c52f/go.mod h1:VFd2XRrQZoX9cOxpeZezKpOlXDwU/dbRejKLjwP+xY8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
40 changes: 40 additions & 0 deletions logger/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logger

import (
"sync"
)

// JSONBuffer is a byte buffer
type JSONBuffer struct {
v []byte
mu sync.Mutex
}

// NewJSONBuffer creates a new JSONBuffer
func NewJSONBuffer() *JSONBuffer {
return &JSONBuffer{
v: []byte{},
}
}

// Write writes a new line
func (j *JSONBuffer) Write(b []byte) (int, error) {
// Trim the new line
if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}

j.mu.Lock()
j.v = append(j.v, b...)
j.v = append(j.v, ',')
j.mu.Unlock()

return len(b), nil
}

func (j *JSONBuffer) String() string {
j.mu.Lock()
defer j.mu.Unlock()

return "[" + string(j.v[:len(j.v)-1]) + "]"
}
23 changes: 23 additions & 0 deletions logger/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package logger

// These two structs are dummy structs that implement Write() to satisfy
// io.Writer.
type logOut struct {
log *Log
}

type logErr struct {
log *Log
}

func newDummyLogs(l *Log) (*logOut, *logErr) {
return &logOut{l}, &logErr{l}
}

func (l *logOut) Write(p []byte) (int, error) {
return l.log.stdout(p)
}

func (l *logErr) Write(p []byte) (int, error) {
return l.log.stderr(p)
}
Loading

0 comments on commit c7db70c

Please sign in to comment.