resource

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 9 Imported by: 4

README

Cloud resource utility

Asset change tracker

Asset tracker keep watching the watch URL for any change. In case modification takes place a callback is called with changed URL and resource.Operation

package mypackage;
import (
	"github.com/viant/cloudless/resource"
	"github.com/viant/afs"
	"time"
	"context"
	"fmt"
	"log"
)

func ExampleTracker_Notify() {
	watchURL := "myProto://myBucket/myFolder"
	tracker := resource.New(watchURL, time.Second)
	fs := afs.New()
	err := tracker.Notify(context.Background(), fs, func(URL string, operation resource.Operation) {
		switch operation {
		case resource.Added:
			fmt.Printf("addd :%v", URL)
		case resource.Modified:
			fmt.Printf("addd :%v", URL)
		case resource.Deleted:
			fmt.Printf("addd :%v", URL)
		}
	})
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Examples

Constants

View Source
const (
	Undefined = -1
	Added     = Operation(iota)
	Modified
	Deleted
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Asset

type Asset struct {
	SourceURL      string
	Compressed     bool
	Source         storage.Object
	CheckFrequency time.Duration
	// contains filtered or unexported fields
}

Asset represents a storage asset

func (*Asset) HasChanged

func (m *Asset) HasChanged(ctx context.Context, fs afs.Service) (bool, error)

func (*Asset) IsCheckDue

func (m *Asset) IsCheckDue(now time.Time) bool

func (*Asset) Sync

func (m *Asset) Sync()

func (*Asset) URL

func (m *Asset) URL() string

type Assets

type Assets map[string]storage.Object

Assets represents storage assets

func NewAssets

func NewAssets(assets []storage.Object) Assets

func (Assets) Added

func (a Assets) Added(ctx context.Context, assets Assets, fn func(ctx context.Context, object storage.Object))

Added calls a callback with added assets

func (Assets) Deleted

func (a Assets) Deleted(ctx context.Context, assets Assets, fn func(ctx context.Context, object storage.Object))

Deleted calls a callback with deleted assets

func (Assets) Modified

func (a Assets) Modified(ctx context.Context, assets Assets, fn func(ctx context.Context, object storage.Object))

Modified calls a callback with modified assets

type Callback added in v1.9.1

type Callback func(ctx context.Context, object storage.Object, operation Operation) error

type Error added in v1.9.0

type Error struct {
	Errors []error
	// contains filtered or unexported fields
}

func (*Error) Append added in v1.9.0

func (e *Error) Append(err error)

func (*Error) Error added in v1.9.0

func (e *Error) Error() string

func (*Error) HasError added in v1.9.0

func (e *Error) HasError() bool

type Operation

type Operation int

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

func New

func New(watchURL string, checkFrequency time.Duration) *Tracker

func (*Tracker) IsCheckDue added in v1.9.2

func (m *Tracker) IsCheckDue(now time.Time) bool

func (*Tracker) NextCheck added in v1.9.3

func (m *Tracker) NextCheck() time.Time

NextCheck returns next check time

func (*Tracker) Notify

func (m *Tracker) Notify(ctx context.Context, fs afs.Service, callback Callback) error

Notify returns true if resource under base URL have changed

Example
package main

import (
	"context"
	"fmt"
	"github.com/viant/afs"
	"github.com/viant/afs/storage"
	"github.com/viant/cloudless/resource"
	"log"
	"time"
)

func main() {
	watchURL := "myProto://myBucket/myFolder"
	tracker := resource.New(watchURL, time.Second)
	fs := afs.New()
	err := tracker.Notify(context.Background(), fs, func(ctx context.Context, object storage.Object, operation resource.Operation) error {
		switch operation {
		case resource.Added:
			fmt.Printf("addd :%v", object.URL())
		case resource.Modified:
			fmt.Printf("addd :%v", object.URL())
		case resource.Deleted:
			fmt.Printf("addd :%v", object.URL())
		}
		return nil
	})
	if err != nil {
		log.Fatal(err)
	}
}

func (*Tracker) Watch

func (m *Tracker) Watch(ctx context.Context, fs afs.Service, callback Callback, onError func(err error))

Watch checks resources in the background thread and calls callback if any modification, or calls error handler if error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL