resource

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 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(assets Assets, fn func(object storage.Object))

Added calls a callback with added assets

func (Assets) Deleted

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

Deleted calls a callback with deleted assets

func (Assets) Modified

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

Modified calls a callback with modified assets

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) Notify

func (m *Tracker) Notify(ctx context.Context, fs afs.Service, callback func(URL string, operation Operation)) error

Notify returns true if resource under base URL have changed

Example
package main

import (
	"context"
	"fmt"
	"github.com/viant/afs"
	"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(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)
	}
}
Output:

func (*Tracker) Watch

func (m *Tracker) Watch(ctx context.Context, fs afs.Service, callback func(URL string, operation Operation), 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