Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions cmd/apiserver-mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/grpc/reflection"

"github.com/skpr/api/internal/server/mock/compass"
"github.com/skpr/api/internal/server/mock/cron"
"github.com/skpr/api/internal/server/mock/version"
"github.com/skpr/api/pb"
)
Expand Down Expand Up @@ -56,6 +57,9 @@ func main() {
log.Println("Registering service: Compass")
pb.RegisterCompassServer(server, &compass.Server{})

log.Println("Registering service: Cron")
pb.RegisterCronServer(server, &cron.Server{})

log.Println("Registering service: Version")
pb.RegisterVersionServer(server, &version.Server{})

Expand Down
146 changes: 146 additions & 0 deletions internal/server/mock/cron/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package cron

import (
"context"
"fmt"
"time"

"github.com/skpr/api/pb"
)

var state = map[string]*pb.CronDetail{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the state to an "environment" state that has the lot eg. config, cron, database images etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might leave this one for a bit while I check interactions with other APIs. If we have one that can create and destro environments we need more integrations (and probably a global state across all APIs).

"drush": {
Name: "drush",
Schedule: "* * * * *",
Command: "drush cron",
LastScheduleTime: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
LastSuccessfulTime: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
},
"search-api-index": {
Name: "search-api-index",
Schedule: "*/6 * * * *",
Command: "drush search-api:index example",
LastScheduleTime: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
LastSuccessfulTime: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
},
"queue-run": {
Name: "queue-run",
Schedule: "* * * * *",
Command: "drush queue:run example",
LastScheduleTime: time.Now().Add(-2 * time.Hour).Format(time.RFC3339),
LastSuccessfulTime: time.Now().Add(-2 * time.Minute).Format(time.RFC3339),
Suspended: true,
},
}

// Server implements the GRPC "cron" definition.
type Server struct {
pb.UnimplementedCronServer
}

// Suspend all the cron jobs
func (c *Server) Suspend(ctx context.Context, req *pb.CronSuspendRequest) (*pb.CronSuspendResponse, error) {
if req.Environment == "" {
return nil, fmt.Errorf("environment not provided")
}

for key := range state {
state[key].Suspended = true
}

return nil, nil
}

// Resume all the cron jobs
func (c *Server) Resume(ctx context.Context, req *pb.CronResumeRequest) (*pb.CronResumeResponse, error) {
if req.Environment == "" {
return nil, fmt.Errorf("environment not provided")
}

for key := range state {
state[key].Suspended = false
}

return nil, nil
}

// List of mocked cron jobs.
func (c *Server) List(ctx context.Context, req *pb.CronListRequest) (*pb.CronListResponse, error) {
if req.Environment == "" {
return nil, fmt.Errorf("environment not provided")
}

resp := &pb.CronListResponse{}

for _, value := range state {
resp.List = append(resp.List, value)
}

return resp, nil
}

// JobList about when cron jobs last ran.
func (c *Server) JobList(ctx context.Context, req *pb.CronJobListRequest) (*pb.CronJobListResponse, error) {
if req.Environment == "" {
return nil, fmt.Errorf("environment not provided")
}

resp := &pb.CronJobListResponse{
List: []*pb.CronJobDetail{
{
Name: "drush",
Phase: pb.CronJobDetail_Running,
StartTime: time.Now().Add(-1 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},
{
Name: "drush",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-2 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},

{
Name: "drush",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-3 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},

{
Name: "drush",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-4 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},

{
Name: "drush",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-5 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},

{
Name: "drush",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-6 * time.Hour).Format(time.RFC3339),
Duration: (10 * time.Second).String(),
},
{
Name: "search-api-index",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-7 * time.Hour).Format(time.RFC3339),
Duration: (5 * time.Minute).String(),
},
{
Name: "queue-run",
Phase: pb.CronJobDetail_Succeeded,
StartTime: time.Now().Add(-8 * time.Hour).Format(time.RFC3339),
Duration: time.Minute.String(),
},
},
}

return resp, nil
}