/
activities.go
48 lines (37 loc) · 1.29 KB
/
activities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"math/rand"
"time"
"go.uber.org/cadence/activity"
)
/**
* Sample activities used by file processing sample workflow.
*/
const (
initParticleActivityName = "initParticleActivityName"
updateParticleActivityName = "updateParticleActivityName"
)
var rng *rand.Rand
// This is registration process where you register all your activity handlers.
func init() {
// initialize the RNG
// WARNING: the randomness of activity scheduling with multiple workers makes random number generation truly random and not repeatable in debugging
// worker.ReplayWorkflowHistoryFromJSONFile should be used to troubleshoot a specific workflow failure.
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func initParticleActivity(ctx context.Context, swarm Swarm) (Particle, error) {
logger := activity.GetLogger(ctx)
logger.Info("initParticleActivity started.")
particle := NewParticle(&swarm, rng)
particle.UpdateFitness(&swarm)
return *particle, nil
}
func updateParticleActivity(ctx context.Context, swarm Swarm, particleIdx int) (Particle, error) {
logger := activity.GetLogger(ctx)
logger.Info("updateParticleActivity started.")
particle := swarm.Particles[particleIdx]
particle.UpdateLocation(&swarm, rng)
particle.UpdateFitness(&swarm)
return *particle, nil
}