Skip to content

Commit

Permalink
feat: job list
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelncui committed Aug 29, 2023
1 parent cda9244 commit 852cf82
Show file tree
Hide file tree
Showing 80 changed files with 6,163 additions and 1,844 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/
output/
frontend/node_modules/
client/node_modules/
Expand Down
6 changes: 4 additions & 2 deletions apis/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/abc950309/tapewriter/library"
)

// JobGet(context.Context, *entity.JobGetRequest) (*entity.JobGetReply, error)
var (
_ = entity.ServiceServer(&API{})
)

type API struct {
entity.UnimplementedServiceServer
entity.UnsafeServiceServer

lib *library.Library
exe *executor.Executor
Expand Down
70 changes: 70 additions & 0 deletions apis/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package apis

import (
"fmt"
"io"
"net/http"
"runtime/debug"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

func (api *API) Uploader() *gin.Engine {
r := gin.Default()
r.Use(func(ctx *gin.Context) {
defer func() {
err := recover()
if err == nil {
return
}

method := ctx.Request.Method
path := ctx.Request.URL.Path
status := 500
remoteAddr := ctx.Request.RemoteAddr
clientIP := ctx.ClientIP()

var e error
switch v := err.(type) {
case error:
e = v
default:
e = fmt.Errorf("%v", v)
}

logrus.WithContext(ctx).
WithError(e).WithField("stack", string(debug.Stack())).
Errorf(
"panic recover: method= %s path= %s status= %d remote_addr= %s client_ip= %s",
method, path, status, remoteAddr, clientIP,
)

reason := e.Error()
ctx.JSON(status, gin.H{"reason": reason})
ctx.Abort()
}()

ctx.Next()
})
r.GET("/ping", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"result": "pong"})
})
r.POST("/library/_import", func(ctx *gin.Context) {
logrus.WithContext(ctx).Infof("get library import request, %t %t", ctx == nil, ctx.Request == nil)

defer ctx.Request.Body.Close()
buf, err := io.ReadAll(ctx.Request.Body)
if err != nil {
panic(err)
}

if err := api.lib.Import(ctx, buf); err != nil {
panic(err)
}

ctx.JSON(http.StatusOK, gin.H{"result": "ok"})
})

return r
}
2 changes: 1 addition & 1 deletion apis/job_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func (api *API) JobCreate(ctx context.Context, req *entity.JobCreateRequest) (*entity.JobCreateReply, error) {
job, err := api.exe.CreateJob(ctx, &executor.Job{
Status: entity.JobStatus_Pending,
Status: entity.JobStatus_PENDING,
Priority: req.Job.Priority,
}, req.Job.Param)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions apis/job_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package apis

import (
"context"

"github.com/abc950309/tapewriter/entity"
)

func (api *API) JobDelete(ctx context.Context, req *entity.JobDeleteRequest) (*entity.JobDeleteReply, error) {
if err := api.exe.DeleteJobs(ctx, req.Ids...); err != nil {
return nil, err
}

return &entity.JobDeleteReply{}, nil
}
3 changes: 3 additions & 0 deletions apis/job_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import (
"context"

"github.com/abc950309/tapewriter/entity"
"github.com/sirupsen/logrus"
)

func (api *API) JobDisplay(ctx context.Context, req *entity.JobDisplayRequest) (*entity.JobDisplayReply, error) {
job, err := api.exe.GetJob(ctx, req.Id)
if err != nil {
logrus.WithContext(ctx).WithError(err).Infof("get job fail, job_id= %d", req.Id)
return &entity.JobDisplayReply{}, nil
}

result, err := api.exe.Display(ctx, job)
if err != nil {
logrus.WithContext(ctx).WithError(err).Infof("get job display fail, job_id= %d", req.Id)
return &entity.JobDisplayReply{}, nil
}

Expand Down
16 changes: 16 additions & 0 deletions apis/library_export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package apis

import (
"context"

"github.com/abc950309/tapewriter/entity"
)

func (api *API) LibraryExport(ctx context.Context, req *entity.LibraryExportRequest) (*entity.LibraryExportReply, error) {
buf, err := api.lib.Export(ctx, req.Types)
if err != nil {
return nil, err
}

return &entity.LibraryExportReply{Json: buf}, nil
}
15 changes: 15 additions & 0 deletions apis/tape_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package apis

import (
"context"

"github.com/abc950309/tapewriter/entity"
)

func (api *API) TapeDelete(ctx context.Context, req *entity.TapeDeleteRequest) (*entity.TapeDeleteReply, error) {
if err := api.lib.DeleteTapes(ctx, req.Ids...); err != nil {
return nil, err
}

return &entity.TapeDeleteReply{}, nil
}
30 changes: 0 additions & 30 deletions apis/tape_get.go

This file was deleted.

47 changes: 47 additions & 0 deletions apis/tape_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package apis

import (
"context"
"fmt"

"github.com/abc950309/tapewriter/entity"
"github.com/abc950309/tapewriter/library"
"github.com/samber/lo"
)

func (api *API) TapeList(ctx context.Context, req *entity.TapeListRequest) (*entity.TapeListReply, error) {
tapes, err := func() ([]*library.Tape, error) {
switch v := req.GetParam().(type) {
case *entity.TapeListRequest_List:
return api.lib.ListTape(ctx, v.List)
case *entity.TapeListRequest_Mget:
m, err := api.lib.MGetTape(ctx, v.Mget.GetIds()...)
if err != nil {
return nil, err
}

return lo.Values(m), nil
default:
return nil, fmt.Errorf("unexpected list tape param, %T", req.GetParam())
}
}()
if err != nil {
return nil, err
}

converted := make([]*entity.Tape, 0, len(tapes))
for _, tape := range tapes {
converted = append(converted, &entity.Tape{
Id: tape.ID,
Barcode: tape.Barcode,
Name: tape.Name,
Encryption: tape.Encryption,
CreateTime: tape.CreateTime.Unix(),
DestroyTime: convertOptionalTime(tape.DestroyTime),
CapacityBytes: tape.CapacityBytes,
WritenBytes: tape.WritenBytes,
})
}

return &entity.TapeListReply{Tapes: converted}, nil
}
7 changes: 3 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env bash
set -e;
set -ex;

CURDIR=$(cd $(dirname $0); pwd);
cd ${CURDIR};

rm -rf output;
mkdir -p output;
go build -o ./output/httpd ./cmd/tape-httpd;
go build -o ./output/loadtape ./cmd/tape-loadtape;
go build -o ./output/import ./cmd/tape-import;
go build -mod=vendor -o ./output/httpd ./cmd/tape-httpd;
go build -mod=vendor -o ./output/lto-info ./cmd/lto-info;

cp -r scripts ./output/;
cp -r ./frontend/dist ./output/frontend;
Expand Down
Loading

0 comments on commit 852cf82

Please sign in to comment.