Skip to content

Commit bbcfc80

Browse files
committed
add healthcheck endpoint for supernode
1 parent f2cfb1a commit bbcfc80

File tree

11 files changed

+606
-70
lines changed

11 files changed

+606
-70
lines changed

gen/supernode/action/cascade/service.pb.go

Lines changed: 294 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/supernode/action/cascade/service_grpc.pb.go

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/mattn/go-sqlite3 v1.14.24
2828
github.com/patrickmn/go-cache v2.1.0+incompatible
2929
github.com/pkg/errors v0.9.1
30+
github.com/shirou/gopsutil/v3 v3.24.5
3031
github.com/sirupsen/logrus v1.9.3
3132
github.com/spf13/cobra v1.8.1
3233
github.com/spf13/viper v1.19.0
@@ -95,6 +96,7 @@ require (
9596
github.com/go-kit/kit v0.13.0 // indirect
9697
github.com/go-kit/log v0.2.1 // indirect
9798
github.com/go-logfmt/logfmt v0.6.0 // indirect
99+
github.com/go-ole/go-ole v1.2.6 // indirect
98100
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
99101
github.com/gogo/googleapis v1.4.1 // indirect
100102
github.com/gogo/protobuf v1.3.2 // indirect
@@ -127,6 +129,7 @@ require (
127129
github.com/kr/pretty v0.3.1 // indirect
128130
github.com/kr/text v0.2.0 // indirect
129131
github.com/linxGnu/grocksdb v1.9.8 // indirect
132+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
130133
github.com/magiconair/properties v1.8.7 // indirect
131134
github.com/mattn/go-colorable v0.1.13 // indirect
132135
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -142,6 +145,7 @@ require (
142145
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
143146
github.com/petermattis/goid v0.0.0-20240813172612-4fcff4a6cae7 // indirect
144147
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
148+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
145149
github.com/prometheus/client_golang v1.20.5 // indirect
146150
github.com/prometheus/client_model v0.6.1 // indirect
147151
github.com/prometheus/common v0.60.1 // indirect
@@ -153,6 +157,7 @@ require (
153157
github.com/sagikazarmark/locafero v0.4.0 // indirect
154158
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
155159
github.com/sasha-s/go-deadlock v0.3.5 // indirect
160+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
156161
github.com/sourcegraph/conc v0.3.0 // indirect
157162
github.com/spf13/afero v1.11.0 // indirect
158163
github.com/spf13/cast v1.7.1 // indirect
@@ -162,7 +167,10 @@ require (
162167
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
163168
github.com/tendermint/go-amino v0.16.0 // indirect
164169
github.com/tidwall/btree v1.7.0 // indirect
170+
github.com/tklauser/go-sysconf v0.3.12 // indirect
171+
github.com/tklauser/numcpus v0.6.1 // indirect
165172
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
173+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
166174
github.com/zondax/hid v0.9.2 // indirect
167175
github.com/zondax/ledger-go v0.14.3 // indirect
168176
go.etcd.io/bbolt v1.4.0-alpha.0.0.20240404170359-43604f3112c5 // indirect

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
297297
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
298298
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
299299
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
300+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
301+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
300302
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
301303
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
302304
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
@@ -531,6 +533,8 @@ github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-b
531533
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
532534
github.com/linxGnu/grocksdb v1.9.8 h1:vOIKv9/+HKiqJAElJIEYv3ZLcihRxyP7Suu/Mu8Dxjs=
533535
github.com/linxGnu/grocksdb v1.9.8/go.mod h1:C3CNe9UYc9hlEM2pC82AqiGS3LRW537u9LFV4wIZuHk=
536+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
537+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
534538
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
535539
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
536540
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
@@ -656,6 +660,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
656660
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
657661
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
658662
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
663+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
664+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
659665
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
660666
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
661667
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
@@ -716,6 +722,12 @@ github.com/sasha-s/go-deadlock v0.3.5/go.mod h1:bugP6EGbdGYObIlx7pUZtWqlvo8k9H6v
716722
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
717723
github.com/shamaton/msgpack/v2 v2.2.0 h1:IP1m01pHwCrMa6ZccP9B3bqxEMKMSmMVAVKk54g3L/Y=
718724
github.com/shamaton/msgpack/v2 v2.2.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
725+
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
726+
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
727+
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
728+
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
729+
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
730+
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
719731
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
720732
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
721733
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
@@ -772,6 +784,10 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l
772784
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
773785
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
774786
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
787+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
788+
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
789+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
790+
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
775791
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
776792
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
777793
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
@@ -788,6 +804,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
788804
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
789805
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
790806
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
807+
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
808+
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
791809
github.com/zondax/hid v0.9.2 h1:WCJFnEDMiqGF64nlZz28E9qLVZ0KSJ7xpc5DLEyma2U=
792810
github.com/zondax/hid v0.9.2/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
793811
github.com/zondax/ledger-go v0.14.3 h1:wEpJt2CEcBJ428md/5MgSLsXLBos98sBOyxNmCjfUCw=
@@ -933,6 +951,7 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w
933951
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
934952
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
935953
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
954+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
936955
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
937956
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
938957
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -949,6 +968,7 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
949968
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
950969
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
951970
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
971+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
952972
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
953973
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
954974
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -969,6 +989,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
969989
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
970990
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
971991
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
992+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
993+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
972994
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
973995
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
974996
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

proto/supernode/action/cascade/service.proto

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ option go_package = "github.com/LumeraProtocol/supernode/gen/supernode/action/ca
44

55
service CascadeService {
66
rpc Register (stream RegisterRequest) returns (stream RegisterResponse);
7+
rpc HealthCheck(HealthCheckRequest) returns (HealthCheckResponse);
8+
79
}
810

911
message RegisterRequest {
@@ -41,4 +43,25 @@ enum SupernodeEventType {
4143
RQID_VERIFIED = 9;
4244
ARTEFACTS_STORED = 10;
4345
ACTION_FINALIZED = 11;
46+
}
47+
48+
message HealthCheckRequest {}
49+
50+
// The HealthCheckResponse represents system health status.
51+
message HealthCheckResponse {
52+
message CPU {
53+
string usage = 1;
54+
string remaining = 2;
55+
}
56+
57+
message Memory {
58+
uint64 total = 1;
59+
uint64 used = 2;
60+
uint64 available = 3;
61+
double used_perc = 4;
62+
}
63+
64+
CPU cpu = 1;
65+
Memory memory = 2;
66+
repeated string tasks_in_progress = 3;
4467
}

supernode/node/action/server/cascade/cascade_action_server.go

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package cascade
22

33
import (
4+
"context"
45
"encoding/hex"
56
"fmt"
7+
pb "github.com/LumeraProtocol/supernode/gen/supernode/action/cascade"
68
"github.com/LumeraProtocol/supernode/pkg/errors"
9+
"github.com/LumeraProtocol/supernode/pkg/logtrace"
10+
cascadeService "github.com/LumeraProtocol/supernode/supernode/services/cascade"
711
"google.golang.org/grpc"
812
"io"
9-
"lukechampine.com/blake3"
1013
"os"
11-
"path/filepath"
12-
13-
pb "github.com/LumeraProtocol/supernode/gen/supernode/action/cascade"
14-
"github.com/LumeraProtocol/supernode/pkg/logtrace"
15-
cascadeService "github.com/LumeraProtocol/supernode/supernode/services/cascade"
1614
)
1715

1816
type ActionServer struct {
@@ -162,31 +160,28 @@ func (server *ActionServer) Register(stream pb.CascadeService_RegisterServer) er
162160
return nil
163161
}
164162

165-
func initializeHasherAndTempFile() (*blake3.Hasher, *os.File, string, error) {
166-
hasher := blake3.New(32, nil)
163+
func (server *ActionServer) HealthCheck(req *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error) {
164+
ctx := context.Background()
167165

168-
tempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("cascade-upload-%d.tmp", os.Getpid()))
169-
tempFile, err := os.Create(tempFilePath)
166+
resp, err := server.factory.NewCascadeRegistrationTask().HealthCheck(ctx)
170167
if err != nil {
171-
return nil, nil, "", fmt.Errorf("could not create temp file: %w", err)
172-
}
173-
174-
return hasher, tempFile, tempFilePath, nil
175-
}
176-
177-
func replaceTempDirWithTaskDir(taskID, tempFilePath string, tempFile *os.File) (targetPath string, err error) {
178-
if err := tempFile.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
179-
return "", fmt.Errorf("failed to close temp file: %w", err)
180-
}
181-
182-
targetDir := filepath.Join(os.TempDir(), taskID)
183-
if err := os.MkdirAll(targetDir, 0755); err != nil {
184-
return "", fmt.Errorf("could not create task directory: %w", err)
185-
}
186-
targetPath = filepath.Join(targetDir, fmt.Sprintf("uploaded-%s.dat", taskID))
187-
if err := os.Rename(tempFilePath, targetPath); err != nil {
188-
return "", fmt.Errorf("could not move file to final location: %w", err)
168+
logtrace.Error(ctx, "error retrieving health-check metrics for supernode", logtrace.Fields{})
169+
return nil, err
189170
}
190171

191-
return targetPath, nil
172+
return &pb.HealthCheckResponse{
173+
Cpu: &pb.HealthCheckResponse_CPU{
174+
User: resp.CPU.User,
175+
Idle: resp.CPU.Idle,
176+
System: resp.CPU.System,
177+
Overall: resp.CPU.Overall,
178+
},
179+
Memory: &pb.HealthCheckResponse_Memory{
180+
Total: resp.Memory.Total,
181+
Used: resp.Memory.Used,
182+
Available: resp.Memory.Available,
183+
UsedPerc: resp.Memory.UsedPerc,
184+
},
185+
TasksInProgress: resp.TasksInProgress,
186+
}, nil
192187
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cascade
2+
3+
import (
4+
"fmt"
5+
"lukechampine.com/blake3"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
func initializeHasherAndTempFile() (*blake3.Hasher, *os.File, string, error) {
11+
hasher := blake3.New(32, nil)
12+
13+
tempFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("cascade-upload-%d.tmp", os.Getpid()))
14+
tempFile, err := os.Create(tempFilePath)
15+
if err != nil {
16+
return nil, nil, "", fmt.Errorf("could not create temp file: %w", err)
17+
}
18+
19+
return hasher, tempFile, tempFilePath, nil
20+
}
21+
22+
func replaceTempDirWithTaskDir(taskID, tempFilePath string, tempFile *os.File) (targetPath string, err error) {
23+
if err := tempFile.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
24+
return "", fmt.Errorf("failed to close temp file: %w", err)
25+
}
26+
27+
targetDir := filepath.Join(os.TempDir(), taskID)
28+
if err := os.MkdirAll(targetDir, 0755); err != nil {
29+
return "", fmt.Errorf("could not create task directory: %w", err)
30+
}
31+
targetPath = filepath.Join(targetDir, fmt.Sprintf("uploaded-%s.dat", taskID))
32+
if err := os.Rename(tempFilePath, targetPath); err != nil {
33+
return "", fmt.Errorf("could not move file to final location: %w", err)
34+
}
35+
36+
return targetPath, nil
37+
}

0 commit comments

Comments
 (0)