Skip to content

Commit

Permalink
Merge pull request #37 from whywaita/fix/update-status
Browse files Browse the repository at this point in the history
don't update target status if not receive job
  • Loading branch information
whywaita authored Mar 24, 2021
2 parents 8debe0f + 8296f4d commit 8690be7
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 28 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/bradleyfalzon/ghinstallation v1.1.1
github.com/go-sql-driver/mysql v1.4.0
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.0
github.com/google/go-github/v32 v32.1.0
Expand All @@ -15,7 +15,7 @@ require (
github.com/satori/go.uuid v1.2.0
goji.io v2.0.2+incompatible
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0
)
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f h1:WBZRG4aNOuI15bLRrCgN8fCq8E5Xuty6jGbmSNEvSsU=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
Expand All @@ -29,14 +28,13 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4 h1:rEvIZUSZ3fx39WIi3JkQqQBitGwpELBIYWeBVh6wn+E=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -133,23 +131,22 @@ golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191003171128-d98b1b443823 h1:Ypyv6BNJh07T1pUSrehkLemqPKXhus2MkfktJ91kRh4=
golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a h1:i47hUS795cOydZI4AwJQCKXOr4BvxzvikwDoDtHhP2Y=
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
9 changes: 9 additions & 0 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package testutils
import (
"fmt"
"log"
"net/http/httptest"
"testing"

"github.com/whywaita/myshoes/pkg/datastore"
"github.com/whywaita/myshoes/pkg/datastore/mysql"
"github.com/whywaita/myshoes/pkg/web"

"github.com/jmoiron/sqlx"
"github.com/ory/dockertest/v3"
Expand All @@ -19,6 +21,8 @@ const (
var (
testDB *sqlx.DB
testDatastore datastore.Datastore

testURL string
)

// IntegrationTestRunner is all integration test
Expand Down Expand Up @@ -56,8 +60,13 @@ func IntegrationTestRunner(m *testing.M) int {
createTablesIfNotExist()
//SetupDefaultFixtures()

mux := web.NewMux(testDatastore)
ts := httptest.NewServer(mux)
testURL = ts.URL

code := m.Run()

ts.Close()
truncateTables()

// You can't defer this because os.Exit doesn't care for defer
Expand Down
10 changes: 10 additions & 0 deletions internal/testutils/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package testutils

// GetTestURL return url of httptest.Server
func GetTestURL() string {
if testURL == "" {
panic("testURL is not initialized yet")
}

return testURL
}
26 changes: 25 additions & 1 deletion pkg/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"path"
"strings"
"time"

uuid "github.com/satori/go.uuid"

"github.com/whywaita/myshoes/pkg/gh"
"github.com/whywaita/myshoes/pkg/logger"
)

// Error values
Expand All @@ -26,6 +28,7 @@ type Datastore interface {
ListTargets(ctx context.Context) ([]Target, error)
DeleteTarget(ctx context.Context, id uuid.UUID) error

// Deprecated: Use datastore.UpdateTargetStatus.
UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, newStatus TargetStatus, description string) error

EnqueueJob(ctx context.Context, job Job) error
Expand Down Expand Up @@ -84,13 +87,33 @@ func (t *Target) OwnerRepo() (string, string) {
// CanReceiveJob check status in target
func (t *Target) CanReceiveJob() bool {
switch t.Status {
case TargetStatusSuspend:
case TargetStatusSuspend, TargetStatusDeleted:
return false
}

return true
}

// UpdateTargetStatus update datastore
func UpdateTargetStatus(ctx context.Context, ds Datastore, targetID uuid.UUID, newStatus TargetStatus, description string) error {
target, err := ds.GetTarget(ctx, targetID)
if err != nil {
return fmt.Errorf("failed to get target: %w", err)
}

if !target.CanReceiveJob() {
// not change status
return nil
}

if err := ds.UpdateTargetStatus(ctx, targetID, newStatus, description); err != nil {
logger.Logf(false, "failed to update target status: %+v", err)
return err
}

return nil
}

// TargetStatus is status for target
type TargetStatus string

Expand All @@ -100,6 +123,7 @@ const (
TargetStatusActive = "active"
TargetStatusRunning = "running"
TargetStatusSuspend = "suspend"
TargetStatusDeleted = "deleted"
TargetStatusErr = "error"
)

Expand Down
10 changes: 5 additions & 5 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func (m *Manager) permissionCheck(ctx context.Context) error {
if _, err := getTargetRunner(ctx, client, owner, repo); err != nil {
logger.Logf(false, "failed to retrieve runner from GitHub (URL: %s): %+v", target.RepoURL(), err)

if err := m.ds.UpdateTargetStatus(ctx, target.UUID, datastore.TargetStatusErr, "failed to fetch runner from GitHub"); err != nil {
if err := datastore.UpdateTargetStatus(ctx, m.ds, target.UUID, datastore.TargetStatusErr, "failed to fetch runner from GitHub"); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", target.UUID, err)
continue
}
continue
} else {
logger.Logf(true, "successfully get runners from GitHub (URL: %s)", target.RepoURL())
if err := m.ds.UpdateTargetStatus(ctx, target.UUID, datastore.TargetStatusActive, ""); err != nil {
if err := datastore.UpdateTargetStatus(ctx, m.ds, target.UUID, datastore.TargetStatusActive, ""); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s,): %+v\n", target.UUID, err)
continue
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (m *Manager) removeRunner(ctx context.Context, t *datastore.Target) error {

if len(targetRunners) == 0 {
logger.Logf(false, "runner for queueing is not found in %s", t.RepoURL())
if err := m.ds.UpdateTargetStatus(ctx, t.UUID, datastore.TargetStatusErr, "runner for queueing is not found"); err != nil {
if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, "runner for queueing is not found"); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err)
}
return nil
Expand All @@ -165,15 +165,15 @@ func (m *Manager) removeRunner(ctx context.Context, t *datastore.Target) error {
if err := m.deleteRunner(ctx, client, r.ds, *r.github.ID, owner, repo, r.status); err != nil {
logger.Logf(false, "failed to delete runner: %+v\n", err)

if err := m.ds.UpdateTargetStatus(ctx, t.UUID, datastore.TargetStatusErr, ""); err != nil {
if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, ""); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err)
}

continue
}
}

if err := m.ds.UpdateTargetStatus(ctx, t.UUID, datastore.TargetStatusActive, ""); err != nil {
if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err)
return fmt.Errorf("failed to update target status: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ func (s *Starter) do(ctx context.Context) error {
// is not ok, save job
return
}
if err := s.ds.UpdateTargetStatus(ctx, job.TargetID, datastore.TargetStatusRunning, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusRunning, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}

if err := s.bung(ctx, job); err != nil {
logger.Logf(false, "failed to bung: %+v\n", err)

if err := s.ds.UpdateTargetStatus(ctx, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}
Expand All @@ -97,7 +97,7 @@ func (s *Starter) do(ctx context.Context) error {
if err := s.ds.DeleteJob(ctx, job.UUID); err != nil {
logger.Logf(false, "failed to delete job: %+v\n", err)

if err := s.ds.UpdateTargetStatus(ctx, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil {
logger.Logf(false, "failed to update target status (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)
return
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/web/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"goji.io/pat"
)

// Serve start webhook receiver
func Serve(ds datastore.Datastore) error {
// NewMux create routed mux
func NewMux(ds datastore.Datastore) *goji.Mux {
mux := goji.NewMux()

mux.HandleFunc(pat.Get("/healthz"), func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -60,6 +60,13 @@ func Serve(ds datastore.Datastore) error {
handleTargetDelete(w, r, ds)
})

return mux
}

// Serve start webhook receiver
func Serve(ds datastore.Datastore) error {
mux := NewMux(ds)

listenAddress := fmt.Sprintf(":%d", config.Config.Port)
logger.Logf(false, "start webhook receiver, listen %s", listenAddress)
if err := http.ListenAndServe(listenAddress, mux); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/web/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package web_test

import (
"os"
"testing"

"github.com/whywaita/myshoes/internal/testutils"
)

func TestMain(m *testing.M) {
os.Exit(testutils.IntegrationTestRunner(m))
}
23 changes: 15 additions & 8 deletions pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import (
"strings"
"time"

"github.com/whywaita/myshoes/pkg/logger"

"github.com/whywaita/myshoes/pkg/datastore"
"github.com/whywaita/myshoes/pkg/gh"

"goji.io/pat"
"github.com/whywaita/myshoes/pkg/logger"

uuid "github.com/satori/go.uuid"
"github.com/whywaita/myshoes/pkg/datastore"
"goji.io/pat"
)

type targetCreateParam struct {
Expand All @@ -28,6 +26,9 @@ type targetCreateParam struct {
ProviderURL string `json:"provider_url"`
}

// GHExistGitHubRepositoryFunc is function pointer of gh.ExistGitHubRepository (for testing)
var GHExistGitHubRepositoryFunc = gh.ExistGitHubRepository

func toNullString(input string) sql.NullString {
if input == "" {
return sql.NullString{
Expand Down Expand Up @@ -103,7 +104,7 @@ func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
}
t := inputTarget.toDS()

if err := gh.ExistGitHubRepository(t.Scope, t.GHEDomain.String, t.GHEDomain.Valid, t.GitHubPersonalToken); err != nil {
if err := GHExistGitHubRepositoryFunc(t.Scope, t.GHEDomain.String, t.GHEDomain.Valid, t.GitHubPersonalToken); err != nil {
logger.Logf(false, "failed to found github repository: %+v", err)
outputErrorMsg(w, http.StatusBadRequest, "github scope is invalid (maybe, repository is not found)")
return
Expand All @@ -118,12 +119,18 @@ func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
outputErrorMsg(w, http.StatusInternalServerError, "datastore create error")
return
}
target, err := ds.GetTarget(ctx, t.UUID)
if err != nil {
logger.Logf(false, "failed to get recently target in datastore: %+v", err)
outputErrorMsg(w, http.StatusInternalServerError, "datastore get error")
return
}

target := sanitizeTarget(&t)
sanitized := sanitizeTarget(target)

w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(target)
json.NewEncoder(w).Encode(sanitized)
return
}

Expand Down
Loading

0 comments on commit 8690be7

Please sign in to comment.