Skip to content
Snippets Groups Projects
Select Git revision
  • 711b178dcaa47d99465441fc0a40997c6a244025
  • master default protected
  • 1.31
  • 4.38.8
  • 4.38.7
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
23 results

pnpm-lock.yaml

Blame
  • runnable-counter.go 1.48 KiB
    // Copyright 2023 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    package jobqueue
    
    import (
    	"context"
    	"fmt"
    	"sync"
    )
    
    func NewCounterRunnableFromMap(data map[string]any) (*CounterRunnable, error) {
    
    	// in go numbers are float64 by default
    	floatCount, ok := data["count"].(float64)
    	if !ok {
    		return nil, fmt.Errorf("%w: Invalid count: %v", ErrInvalidData, data["count"])
    	}
    
    	count := int(floatCount)
    
    	return &CounterRunnable{Count: count}, nil
    }
    
    // CounterResult is a result of a counter
    type CounterResult struct {
    	Count int
    }
    
    func (c *CounterResult) GetResult() string {
    	return fmt.Sprintf("Count: %d", c.Count)
    }
    
    func (c *CounterResult) GetError() (string, int) {
    	return "", 0
    }
    
    // CounterRunnable is a runnable that counts
    type CounterRunnable struct {
    	Count int `json:"count" yaml:"count"`
    	mu    sync.Mutex
    }
    
    // GetCount returns the current count
    func (c *CounterRunnable) GetCount() int {
    	c.mu.Lock()
    	defer c.mu.Unlock()
    	return c.Count
    }
    
    // Run runs the counter
    func (c *CounterRunnable) Run(_ context.Context) (RunResult[CounterResult], error) {
    	c.mu.Lock()
    	defer c.mu.Unlock()
    
    	c.Count++
    
    	return RunResult[CounterResult]{
    		Status: ResultStatusSuccess,
    		Data: CounterResult{
    			Count: c.Count,
    		},
    	}, nil
    }
    
    func (c *CounterRunnable) GetType() string {
    	return "counter"
    }
    
    func (c *CounterRunnable) GetPersistence() RunnableImport {
    	c.mu.Lock()
    	defer c.mu.Unlock()
    
    	data := JSONMap{
    		"count": c.Count,
    	}
    
    	return RunnableImport{
    		Type: c.GetType(),
    		Data: data,
    	}
    }