Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup DB and Add Pagination to Conform to OGC Specs #39

Merged
merged 31 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fd6bb2a
update readme to include S3_META_DIR
ar-siddiqui Jun 20, 2023
9bde73b
Change cache to activeJobs
ar-siddiqui Jun 20, 2023
63bd8b9
Handle addition and removal from ActiveJobs
ar-siddiqui Jun 20, 2023
f8cbe66
Clear cache artifacts
ar-siddiqui Jun 20, 2023
89bb5c2
create database
ar-siddiqui Jun 21, 2023
2a5e387
Add docker_job records to database
ar-siddiqui Jun 21, 2023
8723ed1
Update AWSBatchJob to write to DB
ar-siddiqui Jun 21, 2023
a39c921
Write updates and logs to database
ar-siddiqui Jun 21, 2023
6a2aab5
Make API logs colorful
ar-siddiqui Jun 28, 2023
73c88f2
update tests
ar-siddiqui Jun 28, 2023
b41b828
Set maximum limit to 100 for job lists
ar-siddiqui Jun 28, 2023
4f6b9a8
Add pagination to process list
ar-siddiqui Jun 28, 2023
b0b7818
Update tests for limit parameter
ar-siddiqui Jun 29, 2023
2f7b463
Prettify job logs
ar-siddiqui Jun 29, 2023
cc9e1f0
Add script to allow Dbeaver connection
ar-siddiqui Jun 29, 2023
541e316
When shutting down allow db queries to finish
ar-siddiqui Jun 29, 2023
25b22d0
Fetch AWS logs only when needed
ar-siddiqui Jun 29, 2023
3407cc9
Upsert logs
ar-siddiqui Jun 29, 2023
c499163
Replace aepGrid with dfc process in e2e tests
ar-siddiqui Jun 29, 2023
caefccb
Update dfc process
ar-siddiqui Jun 29, 2023
1c0a4f2
Update design diagram
ar-siddiqui Jun 29, 2023
bb00828
Conform to `/req/job-list/job-list-success`
ar-siddiqui Jun 29, 2023
fad7dcd
Fix HTML Pages
ar-siddiqui Jun 29, 2023
dc8e6f0
Delete scratch postman collection
ar-siddiqui Jun 29, 2023
cfe55f6
Fix prev page link
ar-siddiqui Jun 29, 2023
0217906
Add pagination to processes endpoint
ar-siddiqui Jun 29, 2023
0b2a920
Deprecate ioutil
ar-siddiqui Jun 30, 2023
ea4aa22
Create .data directory if doesn't exist
ar-siddiqui Jun 30, 2023
52846db
Add documentation to database functions
ar-siddiqui Jun 30, 2023
b541550
Update tests for pagination links
ar-siddiqui Jun 30, 2023
6b44aad
Update env variables in readme
ar-siddiqui Jul 6, 2023
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
1 change: 0 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
echo S3_BUCKET=${{ secrets.S3_BUCKET }} >> .env
echo S3_RESULTS_DIR='tests/results' >> .env
echo S3_META_DIR='tests/metadata' >> .env
echo S3_LOGS_DIR='tests/logs' >> .env
echo BATCH_LOG_STREAM_GROUP='/aws/batch/job' >> .env
echo CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE='YES' >> .env
echo EXPIRY_DAYS='1' >> .env
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ https://developer.ogc.org/api/processes/index.html
3. Update swagger documents and compile the server: `swag init && go build main.go`.
4. Run the server: `./main`, with the following available flags:
```
`-c [type int] specify the path of the max cache size for storing jobs (default 11073741824 (1GB))`
`-o [type bool] specify if cache should be overwritten`
`-d [type string] specify the path of the processes directory to load (default "plugins" assuming program called from inside repo)`
`-e [type string] specify the path of the dot env file to load (default ".env")`
`-p [type string] specify the port to run the api on (default "5050")`
Expand Down Expand Up @@ -68,8 +66,6 @@ When a job is submitted, a local container is fired up immediately for sync jobs

The API responds to all GET requests (except `/jobs/<jobID>/results`) as HTML or JSON depending upon if the request is being originated from Browser or not or if it specifies the format using query parameter ‘f’.

The app maintains a cache of all jobs submitted, a snapshot of this cache is saved to disk at `./.data/snapshot.gob` every 60 minutes and at the graceful shutdown of the server. If during the restart the snapshot is found at the above location, the cache is repopulated with the snapshot data.

## Example .env file

For AWS services, an env file should be located at the root of this repository (`./.env`) and be formatted like so:
Expand All @@ -83,7 +79,7 @@ AWS_DEFAULT_REGION='us-east-1'
# S3
S3_BUCKET='********'
S3_RESULTS_DIR='results'
S3_LOGS_DIR='logs'
S3_META_DIR='metadata'

# BATCH
BATCH_LOG_STREAM_GROUP='/aws/batch/job'
Expand Down
526 changes: 245 additions & 281 deletions design.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/docs.go

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

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ require (
github.com/go-openapi/swag v0.19.15 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-sqlite3 v1.14.17
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down Expand Up @@ -80,6 +80,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA=
github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
Expand Down
43 changes: 15 additions & 28 deletions handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"app/jobs"
pr "app/processes"
"io"
"log"
"text/template"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
)

// Store for templates and a receiver function to render them
Expand All @@ -29,13 +29,15 @@ type RESTHandler struct {
ConformsTo []string
T Template
S3Svc *s3.S3
JobsCache *jobs.JobsCache
DB *jobs.DB
ActiveJobs *jobs.ActiveJobs
ProcessList *pr.ProcessList
}

// Initializes resources and return a new handler
func NewRESTHander(pluginsDir string, cacheSize uint64, o bool) (*RESTHandler, error) {
// working with pointers here so as not to copy large templates, yamls, and job cache
// errors are fatal
func NewRESTHander(pluginsDir string) *RESTHandler {
// working with pointers here so as not to copy large templates, yamls, and ActiveJobs
config := RESTHandler{
Title: "process-api",
Description: "ogc process api written in Golang for use with cloud service controllers to manage asynchronous requests",
Expand All @@ -59,34 +61,19 @@ func NewRESTHander(pluginsDir string, cacheSize uint64, o bool) (*RESTHandler, e
}))
config.S3Svc = s3.New(sess)

// Setup Job Cache that will store all jobs
jc := jobs.JobsCache{MaxSizeBytes: uint64(cacheSize),
CurrentSizeBytes: 0, TrimThreshold: 0.80}
jc.Jobs = make(map[string]*jobs.Job)
if !o {
// load from previous snapshot if it exist
err := jc.LoadCacheFromFile()
if err != nil {
if err.Error() == "not found" {
log.Info("Starting with clean Jobs cache.")
} else {
// if file exists and there is an error in loading, we do not want to start from clean cache
// as this will overwrite the old file when the server is shutdown
log.Fatalf("Error loading snapshot: %s\nSet -o flag to true to overwrite snapshot", err.Error())
}
} else {
log.Info("Starting from snapshot saved at .data/snapshot.gob")
}
} else {
log.Info("Starting with clean Jobs cache.")
}
config.JobsCache = &jc
// Setup Active Jobs that will store all jobs currently in process
ac := jobs.ActiveJobs{}
ac.Jobs = make(map[string]*jobs.Job)
config.ActiveJobs = &ac

adb := jobs.InitDB(".data/db.sqlite")
config.DB = adb

processList, err := pr.LoadProcesses(pluginsDir)
if err != nil {
return nil, err
log.Fatal(err)
}
config.ProcessList = &processList

return &config, nil
return &config
}
Loading