Skip to content

Commit dfcd8d8

Browse files
authored
worker|cmd: Use XDG for Cache and data (#86)
1 parent 8a9879a commit dfcd8d8

File tree

8 files changed

+62
-26
lines changed

8 files changed

+62
-26
lines changed

cmd/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"github.com/urfave/cli/v3"
55
)
66

7+
var (
8+
AppName = "qid"
9+
)
10+
711
var (
812
Cmd = &cli.Command{
9-
Name: "qid",
13+
Name: AppName,
1014
Usage: "QID is a small CI/CD build on top of a Queue(Pub/Sub) system",
1115
Commands: []*cli.Command{
1216
serverCmd,

cmd/server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"os/signal"
9+
"path/filepath"
910
"strings"
1011
"syscall"
1112

@@ -27,6 +28,8 @@ import (
2728

2829
"gocloud.dev/pubsub/mempubsub"
2930
"gocloud.dev/pubsub/natspubsub"
31+
32+
"github.com/adrg/xdg"
3033
)
3134

3235
var (
@@ -45,7 +48,6 @@ var (
4548
&cli.StringFlag{Name: "db-password", Usage: "Database Password"},
4649
&cli.StringFlag{Name: "db-name", Usage: "Database Name"},
4750
&cli.BoolFlag{Name: "run-migrations", Value: true, Usage: "Flag to know if migrations should be ran"},
48-
&cli.StringFlag{Name: "db-file", Usage: "Flag to know where the DB File is"},
4951

5052
&cli.BoolFlag{Name: "run-worker", Value: true, Usage: "Runs a worker with QID server"},
5153
&cli.IntFlag{Name: "concurrency", Value: 1, Usage: "Number of workers to start in one instance"},
@@ -102,14 +104,17 @@ var (
102104
return fmt.Errorf("failed to open: %v", err)
103105
}
104106
defer topic.Shutdown(ctx)
105-
107+
dbFile, err := xdg.DataFile(filepath.Join(AppName, AppName+".db"))
108+
if err != nil {
109+
return fmt.Errorf("failed to create dbFile: %v", err)
110+
}
106111
logger.Log("msg", "DB connection starting ...", "db-system", cfg.DBSystem)
107112
db, err := mysql.New(cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPassword, mysql.Options{
108113
DBName: cfg.DBName,
109114
MultiStatements: true,
110115
ClientFoundRows: true,
111116
System: cfg.DBSystem,
112-
DBFile: cfg.DBFile,
117+
DBFile: dbFile,
113118
})
114119
if err != nil {
115120
panic(err)

docs/documentation.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Job that depends on that Resource that changed. If there is then a new job/s wil
1616
Then when it finishes it checks if another job depends on it and queues for that job/s to be triggered.
1717

1818
Workers execute everything on the host machine for now (either on the server if local or on the worker host), each time something is executed
19-
it's done on a unique `tmp/` folder so it never collisions with a previous run, will potentially [change](https://github.com/xescugc/qid/issues/57) on the future
19+
it's done on a unique `$XDG_CACHE_HOME/qid/{UUID}/` folder so it never collisions with a previous run, will potentially [change](https://github.com/xescugc/qid/issues/57) on the future
2020

2121
## Server Configuration
2222

@@ -28,8 +28,7 @@ it's done on a unique `tmp/` folder so it never collisions with a previous run,
2828
* `db-user`: Database User
2929
* `db-password`: Database Password
3030
* `db-name`: Database Name
31-
* `sqlite`: Uses SQLite
32-
* `db-file`: The file in which to store the DB (does not need to exist as it'll be created)
31+
* `sqlite`: Uses SQLite. The file will be at `$XDG_DATA_HOME/qid/qid.db`
3332
* `mem`: Run the DB in memory, which basically uses the SQLite memory option
3433
* `run-worker`: Specifies if you want to run the workers on the same server or not
3534
* `concurrency`: How many parallel instances has the worker

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ go 1.25.1
44

55
require (
66
github.com/VividCortex/mysqlerr v1.0.0
7+
github.com/adrg/xdg v0.5.3
78
github.com/awalterschulze/gographviz v2.0.3+incompatible
89
github.com/cycloidio/sqlr v1.0.0
910
github.com/davecgh/go-spew v1.1.1
1011
github.com/go-kit/kit v0.13.0
1112
github.com/go-kit/log v0.2.0
1213
github.com/go-sql-driver/mysql v1.9.3
14+
github.com/google/uuid v1.6.0
1315
github.com/gorilla/handlers v1.5.2
1416
github.com/gorilla/mux v1.8.1
1517
github.com/gosimple/slug v1.15.0
@@ -44,7 +46,6 @@ require (
4446
github.com/go-logr/stdr v1.2.2 // indirect
4547
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
4648
github.com/google/go-cmp v0.7.0 // indirect
47-
github.com/google/uuid v1.6.0 // indirect
4849
github.com/googleapis/gax-go/v2 v2.15.0 // indirect
4950
github.com/gosimple/unidecode v1.0.1 // indirect
5051
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
1313
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
1414
github.com/VividCortex/mysqlerr v1.0.0 h1:5pZ2TZA+YnzPgzBfiUWGqWmKDVNBdrkf9g+DNe1Tiq8=
1515
github.com/VividCortex/mysqlerr v1.0.0/go.mod h1:xERx8E4tBhLvpjzdUyQiSfUxeMcATEQrflDAfXsqcAE=
16+
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
17+
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
1618
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
1719
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
20+
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
21+
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
22+
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
23+
github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
24+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
25+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
1826
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
1927
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
2028
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
@@ -111,6 +119,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
111119
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
112120
github.com/lopezator/migrator v0.3.1 h1:ZFPT6aC7+nGWkqhleynABZ6ftycSf6hmHHLOaryq1Og=
113121
github.com/lopezator/migrator v0.3.1/go.mod h1:X+lHDMZ9Ci3/KdbypJcQYFFwipVrJsX4fRCQ4QLauYk=
122+
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
123+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
114124
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
115125
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
116126
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
@@ -137,6 +147,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
137147
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
138148
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
139149
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
150+
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
151+
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
140152
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
141153
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
142154
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=

qid/config/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ type Config struct {
1212
DBPassword string `koanf:"db-password"`
1313
DBName string `koanf:"db-name"`
1414

15-
// SQLite
16-
DBFile string `koanf:"db-file"`
17-
1815
RunWorker bool `koanf:"run-worker"`
1916
Concurrency int `koanf:"concurrency"`
2017

qid/transport/http/templates/views/layouts/index.tmpl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,22 +767,36 @@
767767
template: _.template($('#resource-versions-view').html()),
768768
initialize: function() {
769769
this.listenTo(this.collection, "add", this.addVersion)
770+
this.listenTo(this.model, "change", this.render)
770771

771772
// TODO: most likely add the {reset: true} as the
772773
// order is really important
773774
this.collection.fetch()
775+
776+
var that = this
777+
this.intervalID = window.setInterval(function() {
778+
that.model.fetch()
779+
}, 1000);
774780
},
775781
render: function () {
776782
this.$el.html(this.template({
777783
pipeline: this.collection.resource.collection.pipeline.toJSON(),
778784
resource: this.collection.resource.toJSON(),
779785
}));
786+
var that = this;
787+
this.collection.each(function(m) {
788+
that.addVersion(m)
789+
})
780790
return this; // enable chained calls
781791
},
782792
addVersion: function(m) {
783793
var ver = new app.ResourceVersionView({model: m})
784794
$('#resource-versions').append(ver.render().el);
785-
}
795+
},
796+
remove: function() {
797+
clearInterval(this.intervalID)
798+
Backbone.View.prototype.remove.call(this);
799+
},
786800
})
787801
app.ResourceVersionView = Backbone.View.extend({
788802
// TODO: Check to set the attributes
@@ -899,6 +913,7 @@
899913
var that = this
900914
$.when.apply($, complete).done(function() {
901915
that.contentView = new app.ResourceVersionsView({
916+
model: rs,
902917
collection: versions,
903918
});
904919
$('#main').html(that.contentView.render().el)

worker/service.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"path/filepath"
910
"slices"
1011
"strings"
1112

12-
"github.com/davecgh/go-spew/spew"
13+
"github.com/adrg/xdg"
14+
"github.com/google/uuid"
1315
"github.com/xescugc/qid/qid"
1416
"github.com/xescugc/qid/qid/build"
1517
"github.com/xescugc/qid/qid/queue"
@@ -55,15 +57,17 @@ func (w *Worker) Run(ctx context.Context) error {
5557
continue
5658
}
5759

58-
cwd, err := os.MkdirTemp("", "qid")
60+
uuiddir, err := uuid.NewV7()
61+
if err != nil {
62+
return fmt.Errorf("failed to creat UUID %w", err)
63+
}
64+
// We append a file "qid" just so the CacheFile creates the full dir,
65+
// afterward we just get the Dir of the cwd
66+
cwd, err := xdg.CacheFile(filepath.Join("qid", uuiddir.String(), "qid"))
5967
if err != nil {
6068
return fmt.Errorf("failed to creat Temp Dir: %w", err)
6169
}
62-
63-
//err = os.Mkdir(cwd, 0755)
64-
//if err != nil {
65-
//return fmt.Errorf("failed to create dir %q: %w", cwd, err)
66-
//}
70+
cwd = filepath.Dir(cwd)
6771

6872
pp, err := w.qid.GetPipeline(ctx, m.PipelineName)
6973
if err != nil {
@@ -127,7 +131,7 @@ func (w *Worker) Run(ctx context.Context) error {
127131
if err != nil {
128132
b.Get = append(b.Get, build.Step{
129133
Name: g.Name,
130-
Logs: err.Error(),
134+
Logs: string(stdouterr) + "\n" + err.Error(),
131135
})
132136
w.failBuild(ctx, m, b, nil)
133137
w.logger.Log("error", fmt.Errorf("failed to run command %q with args %q (%s): %w", rt.Pull.Path, rt.Pull.Args, stdouterr, err))
@@ -156,7 +160,7 @@ func (w *Worker) Run(ctx context.Context) error {
156160
if err != nil {
157161
b.Task = append(b.Task, build.Step{
158162
Name: t.Name,
159-
Logs: err.Error(),
163+
Logs: string(stdouterr) + "\n" + err.Error(),
160164
})
161165
w.failBuild(ctx, m, b, nil)
162166
w.logger.Log("error", fmt.Errorf("failed to run command %q with args %q: %w", t.Run.Path, t.Run.Args, err))
@@ -173,7 +177,6 @@ func (w *Worker) Run(ctx context.Context) error {
173177
w.logger.Log("error", ferr)
174178
continue
175179
}
176-
spew.Dump(string(stdouterr))
177180
for _, nj := range pp.Jobs {
178181
for _, g := range nj.Get {
179182
if slices.Contains(g.Passed, j.Name) && g.Trigger {
@@ -228,10 +231,10 @@ func (w *Worker) Run(ctx context.Context) error {
228231
}
229232
stdouterr, err := cmd.CombinedOutput()
230233
if err != nil {
231-
r.Logs = string(stdouterr)
232-
err = w.qid.UpdatePipelineResource(ctx, m.PipelineName, r.Canonical, r)
233-
if err != nil {
234-
w.logger.Log("error", fmt.Errorf("failed update Resource %q.%q from Pipeline %q: %w", r.Type, r.Name, m.PipelineName, err))
234+
r.Logs = string(stdouterr) + "\n" + err.Error()
235+
nerr := w.qid.UpdatePipelineResource(ctx, m.PipelineName, r.Canonical, r)
236+
if nerr != nil {
237+
w.logger.Log("error", fmt.Errorf("failed update Resource %q.%q from Pipeline %q: %w", r.Type, r.Name, m.PipelineName, nerr))
235238
}
236239
w.logger.Log("error", fmt.Errorf("failed to run command %q with args %q (%s): %w", rt.Check.Path, rt.Check.Args, stdouterr, err))
237240
goto END

0 commit comments

Comments
 (0)