3
0

Round timestamps to a second accuracy

Different parts of the system (FE, API, store) may use different
standards so operations like IsStale check may fail.
This commit is contained in:
Tomaž Jerman 2020-12-18 18:18:30 +01:00
parent 8c31bf1b86
commit ce34993b74
18 changed files with 38 additions and 37 deletions

View File

@ -863,8 +863,7 @@ func (svc record) procUpdate(ctx context.Context, s store.Storer, invokerID uint
}
func (svc record) recordInfoUpdate(r *types.Record) {
now := time.Now()
r.UpdatedAt = &now
r.UpdatedAt = now()
r.UpdatedBy = auth.GetIdentityFromContext(svc.ctx).Identity()
}

View File

@ -65,7 +65,7 @@ var (
// wrapper around time.Now() that will aid service testing
now = func() *time.Time {
c := time.Now()
c := time.Now().Round(time.Second)
return &c
}

View File

@ -48,7 +48,7 @@ var (
// wrapper around time.Now() that will aid service testing
now = func() *time.Time {
c := time.Now()
c := time.Now().Round(time.Second)
return &c
}

View File

@ -2,6 +2,8 @@ package service
import (
"context"
"time"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/healthcheck"
"github.com/cortezaproject/corteza-server/pkg/id"
@ -12,7 +14,6 @@ import (
"github.com/cortezaproject/corteza-server/pkg/rbac"
"github.com/cortezaproject/corteza-server/store"
"go.uber.org/zap"
"time"
)
type (
@ -53,7 +54,7 @@ var (
// wrapper around time.Now() that will aid service testing
now = func() *time.Time {
c := time.Now()
c := time.Now().Round(time.Second)
return &c
}

View File

@ -2,7 +2,6 @@ package store
import (
"context"
"time"
"github.com/cortezaproject/corteza-server/pkg/envoy"
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
@ -29,7 +28,7 @@ func NewApplicationState(res *resource.Application, cfg *EncoderConfig) resource
func (n *applicationState) Prepare(ctx context.Context, s store.Storer, rs *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
if n.res.Res.Unify == nil {
n.res.Res.Unify = &types.ApplicationUnify{}

View File

@ -3,7 +3,6 @@ package store
import (
"context"
"fmt"
"time"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/envoy"
@ -35,7 +34,7 @@ func NewComposeChartState(res *resource.ComposeChart, cfg *EncoderConfig) resour
func (n *composeChartState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Get relate namespace

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/envoy"
@ -38,7 +37,7 @@ func NewComposeModuleState(res *resource.ComposeModule, cfg *EncoderConfig) reso
func (n *composeModuleState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Get relate namespace
@ -160,7 +159,7 @@ func (n *composeModuleState) Encode(ctx context.Context, s store.Storer, state *
f.ModuleID = res.ID
f.Place = i
f.DeletedAt = nil
f.CreatedAt = time.Now()
f.CreatedAt = *now()
if f.Kind == "Record" {
refM := f.Options.String("module")

View File

@ -3,7 +3,6 @@ package store
import (
"context"
"fmt"
"time"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/envoy"
@ -32,7 +31,7 @@ func NewComposeNamespaceState(res *resource.ComposeNamespace, cfg *EncoderConfig
func (n *composeNamespaceState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Try to get the original chart

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"time"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/envoy"
@ -43,7 +42,7 @@ func NewComposePageState(res *resource.ComposePage, cfg *EncoderConfig) resource
func (n *composePageState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Get relate namespace

View File

@ -2,7 +2,6 @@ package store
import (
"context"
"time"
"github.com/cortezaproject/corteza-server/messaging/types"
@ -32,7 +31,7 @@ func NewMessagingChannelState(res *resource.MessagingChannel, cfg *EncoderConfig
func (n *messagingChannelState) Prepare(ctx context.Context, s store.Storer, rs *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Sys users

View File

@ -3,7 +3,6 @@ package store
import (
"context"
"fmt"
"time"
"github.com/cortezaproject/corteza-server/pkg/envoy"
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
@ -32,7 +31,7 @@ func NewRole(res *resource.Role, cfg *EncoderConfig) resourceState {
func (n *roleState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
n.rl, err = findRoleS(ctx, s, makeGenericFilter(n.res.Identifiers()))

View File

@ -2,7 +2,6 @@ package store
import (
"context"
"time"
"github.com/cortezaproject/corteza-server/pkg/envoy"
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
@ -51,7 +50,7 @@ func (n *settingsState) Prepare(ctx context.Context, s store.Storer, state *envo
// Default values
for _, s := range n.res {
if s.UpdatedAt.IsZero() {
s.UpdatedAt = time.Now()
s.UpdatedAt = *now()
}
gSettingsState.res = append(gSettingsState.res, s)

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/mail"
"time"
"github.com/cortezaproject/corteza-server/pkg/envoy"
"github.com/cortezaproject/corteza-server/pkg/envoy/resource"
@ -32,7 +31,7 @@ func NewUserState(res *resource.User, cfg *EncoderConfig) resourceState {
func (n *userState) Prepare(ctx context.Context, s store.Storer, state *envoy.ResourceState) (err error) {
// Initial values
if n.res.Res.CreatedAt.IsZero() {
n.res.Res.CreatedAt = time.Now()
n.res.Res.CreatedAt = *now()
}
// Try to get the original user

View File

@ -29,6 +29,12 @@ var (
return id.Next()
}
// wrapper around time.Now() that will aid testing
now = func() *time.Time {
c := time.Now().Round(time.Second)
return &c
}
exprP = expr.Parser()
)

View File

@ -16,13 +16,15 @@ func roles(ctx context.Context, s store.Storer) error {
return nil
}
now := time.Now().Round(time.Second)
rr := types.RoleSet{
&types.Role{ID: rbac.AdminsRoleID, Name: "Administrators", Handle: "admins"},
&types.Role{ID: rbac.EveryoneRoleID, Name: "Everyone", Handle: "everyone"},
}
err := rr.Walk(func(r *types.Role) error {
r.CreatedAt = time.Now()
r.CreatedAt = now
return store.CreateRole(ctx, s, r)
})

View File

@ -2,6 +2,10 @@ package tests
import (
"context"
"os"
"testing"
"time"
"github.com/cortezaproject/corteza-server/pkg/logger"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/store/mysql"
@ -10,14 +14,11 @@ import (
"github.com/cortezaproject/corteza-server/tests/helpers"
_ "github.com/joho/godotenv/autoload"
"go.uber.org/zap"
"os"
"testing"
"time"
)
var (
now = func() *time.Time {
n := time.Now()
n := time.Now().Round(time.Second)
return &n
}
)

View File

@ -3,6 +3,8 @@ package service
import (
"context"
"errors"
"time"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
intAuth "github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
@ -16,7 +18,6 @@ import (
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
"go.uber.org/zap"
"time"
)
type (
@ -89,7 +90,7 @@ var (
// wrapper around time.Now() that will aid service testing
now = func() *time.Time {
c := time.Now()
c := time.Now().Round(time.Second)
return &c
}

View File

@ -3,14 +3,14 @@ package service
import (
"context"
"fmt"
"strings"
"github.com/cortezaproject/corteza-server/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/logger"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"strings"
"time"
)
type (
@ -108,14 +108,14 @@ func (svc settings) Set(ctx context.Context, v *types.SettingValue) (err error)
var current *types.SettingValue
current, err = store.LookupSettingByNameOwnedBy(ctx, svc.store, v.Name, v.OwnedBy)
if errors.IsNotFound(err) {
v.UpdatedAt = time.Now()
v.UpdatedAt = *now()
err = store.CreateSetting(ctx, svc.store, v)
} else if err != nil {
return err
}
if !current.Eq(v) {
v.UpdatedAt = time.Now()
v.UpdatedAt = *now()
err = store.UpdateSetting(ctx, svc.store, v)
}
@ -140,7 +140,7 @@ func (svc settings) BulkSet(ctx context.Context, vv types.SettingValueSet) (err
} else {
vv = current.Changed(vv)
_ = vv.Walk(func(v *types.SettingValue) error {
v.UpdatedAt = time.Now()
v.UpdatedAt = *now()
return nil
})