How to use Alive method of runner Package

Best Gauge code snippet using runner.Alive

txns.go

Source:txns.go Github

copy

Full Screen

...10 "gopkg.in/mgo.v2/bson"11 "gopkg.in/mgo.v2/txn"12)13const (14 txnAssertEnvIsAlive = true15 txnAssertEnvIsNotAlive = false16)17// txnRunner returns a jujutxn.Runner instance.18//19// If st.transactionRunner is non-nil, then that will be20// returned and the session argument will be ignored. This21// is the case in tests only, when we want to test concurrent22// operations.23//24// If st.transactionRunner is nil, then we create a new25// transaction runner with the provided session and return26// that.27func (st *State) txnRunner(session *mgo.Session) jujutxn.Runner {28 if st.transactionRunner != nil {29 return st.transactionRunner30 }31 return newMultiEnvRunner(st.EnvironUUID(), st.db.With(session), txnAssertEnvIsAlive)32}33// txnRunnerNoEnvAliveAssert returns a jujutxn.Runner instance that does not34// add an assertion for a live environment to the transaction. It was35// introduced only to allow the initial environment to be created and should36// be used rarely.37func (st *State) txnRunnerNoEnvAliveAssert(session *mgo.Session) jujutxn.Runner {38 if st.transactionRunner != nil {39 return st.transactionRunner40 }41 return newMultiEnvRunner(st.EnvironUUID(), st.db.With(session), txnAssertEnvIsNotAlive)42}43// runTransactionNoEnvAliveAssert is a convenience method delegating to txnRunnerNoEnvAliveAssert.44func (st *State) runTransactionNoEnvAliveAssert(ops []txn.Op) error {45 session := st.db.Session.Copy()46 defer session.Close()47 return st.txnRunnerNoEnvAliveAssert(session).RunTransaction(ops)48}49// runTransaction is a convenience method delegating to transactionRunner.50func (st *State) runTransaction(ops []txn.Op) error {51 session := st.db.Session.Copy()52 defer session.Close()53 return st.txnRunner(session).RunTransaction(ops)54}55// run is a convenience method delegating to transactionRunner.56func (st *State) run(transactions jujutxn.TransactionSource) error {57 session := st.db.Session.Copy()58 defer session.Close()59 return st.txnRunner(session).Run(transactions)60}61// ResumeTransactions resumes all pending transactions.62func (st *State) ResumeTransactions() error {63 session := st.db.Session.Copy()64 defer session.Close()65 return st.txnRunner(session).ResumeTransactions()66}67func newMultiEnvRunner(envUUID string, db *mgo.Database, assertEnvAlive bool) jujutxn.Runner {68 return &multiEnvRunner{69 rawRunner: jujutxn.NewRunner(jujutxn.RunnerParams{Database: db}),70 envUUID: envUUID,71 assertEnvAlive: assertEnvAlive,72 }73}74type multiEnvRunner struct {75 rawRunner jujutxn.Runner76 envUUID string77 assertEnvAlive bool78}79// RunTransaction is part of the jujutxn.Runner interface. Operations80// that affect multi-environment collections will be modified in-place81// to ensure correct interaction with these collections.82func (r *multiEnvRunner) RunTransaction(ops []txn.Op) error {83 ops = r.updateOps(ops)84 return r.rawRunner.RunTransaction(ops)85}86// Run is part of the jujutxn.Run interface. Operations returned by87// the given "transactions" function that affect multi-environment88// collections will be modified in-place to ensure correct interaction89// with these collections.90func (r *multiEnvRunner) Run(transactions jujutxn.TransactionSource) error {91 return r.rawRunner.Run(func(attempt int) ([]txn.Op, error) {92 ops, err := transactions(attempt)93 if err != nil {94 // Don't use Trace here as jujutxn doens't use juju/errors95 // and won't deal correctly with some returned errors.96 return nil, err97 }98 ops = r.updateOps(ops)99 return ops, nil100 })101}102// Run is part of the jujutxn.Run interface.103func (r *multiEnvRunner) ResumeTransactions() error {104 return r.rawRunner.ResumeTransactions()105}106func (r *multiEnvRunner) updateOps(ops []txn.Op) []txn.Op {107 var opsNeedEnvAlive bool108 for i, op := range ops {109 if multiEnvCollections.Contains(op.C) {110 var docID interface{}111 if id, ok := op.Id.(string); ok {112 docID = addEnvUUID(r.envUUID, id)113 ops[i].Id = docID114 } else {115 docID = op.Id116 }117 if op.Insert != nil {118 switch doc := op.Insert.(type) {119 case bson.D:120 ops[i].Insert = r.updateBsonD(doc, docID, op.C)121 case bson.M:122 r.updateBsonM(doc, docID, op.C)123 default:124 r.updateStruct(doc, docID, op.C)125 }126 if r.assertEnvAlive && !opsNeedEnvAlive && envAliveColls.Contains(op.C) {127 opsNeedEnvAlive = true128 }129 }130 }131 }132 if opsNeedEnvAlive {133 ops = append(ops, assertEnvAliveOp(r.envUUID))134 }135 return ops136}137func assertEnvAliveOp(envUUID string) txn.Op {138 return txn.Op{139 C: environmentsC,140 Id: envUUID,141 Assert: isEnvAliveDoc,142 }143}144var envAliveColls = newEnvAliveColls()145// newEnvAliveColls returns a copy of multiEnvCollections minus cleanupsC.146// This set is used to check if a txn needs to assert that there is a live147// environment be inserting docs.148func newEnvAliveColls() set.Strings {149 e := set.NewStrings(multiEnvCollections.Values()...)150 e.Remove(cleanupsC)151 return e152}153func (r *multiEnvRunner) updateBsonD(doc bson.D, docID interface{}, collName string) bson.D {154 idSeen := false155 envUUIDSeen := false156 for i, elem := range doc {157 switch elem.Name {158 case "_id":159 idSeen = true160 doc[i].Value = docID161 case "env-uuid":162 envUUIDSeen = true...

Full Screen

Full Screen

pipe.go

Source:pipe.go Github

copy

Full Screen

...7 "time"8)9type RunnerClient struct {10 jobManager work.JobManager11 runnerAlive RunnerAlive12}13func NewRunnerClient() RunnerClient {14 httpApi := api.NewRunnerHttpApi(15 config.GlobalConfig.Server.Address,16 config.GlobalConfig.Server.RunnerId,17 config.GlobalConfig.Server.Token,18 )19 return RunnerClient{20 jobManager: work.NewWorkerManager(config.GlobalConfig.Server.MaxJobNum),21 runnerAlive: NewRunnerAlive(&httpApi),22 }23}24func (runnerManager *RunnerClient) run() {25 // 单线程循环向服务端请求26 for true {27 logrus.Info("Start get new jobs from server")28 runnerManager.requestServer()29 var sleepMills = 200030 time.Sleep(time.Duration(sleepMills) * time.Millisecond)31 }32}33// 向服务端请求34func (runnerManager *RunnerClient) requestServer() {35 manageStatus, workersStatus := runnerManager.jobManager.ReadStatus()36 acceptJobs, denyJobs := runnerManager.jobManager.GetAndCleanAcceptDenyRunningJobIds()37 aliveResponse, err := runnerManager.runnerAlive.alive(&manageStatus, workersStatus, acceptJobs, denyJobs)38 if err != nil {39 logrus.Warning("alive error:" + err.Error())40 return41 }42 newJobs := aliveResponse.NewJobs43 logrus.Info("Get ", len(newJobs), " jobs")44 runnerManager.handleAliveNewJobs(newJobs)45}46// 处理新的Jobs47func (runnerManager *RunnerClient) handleAliveNewJobs(newJobs []api.NewJob) {48 var acceptRunningJobIds []string49 var denyRunningJobIds []string50 for _, job := range newJobs {51 err := runnerManager.jobManager.AddNewJob(job.JobRunningId, job.Sources, &job)52 if err != nil {53 logrus.Info("add new job error" + err.Error())54 denyRunningJobIds = append(denyRunningJobIds, job.JobRunningId)55 } else {56 logrus.Info("add new job success, JobRunningId:" + job.JobRunningId)57 acceptRunningJobIds = append(acceptRunningJobIds, job.JobRunningId)58 }59 }60 runnerManager.jobManager.AddAcceptDenyRunningJobId(acceptRunningJobIds, denyRunningJobIds)61}...

Full Screen

Full Screen

alive.go

Source:alive.go Github

copy

Full Screen

...4 "client/work"5 "io/ioutil"6 "net/http"7)8type RunnerAlive struct {9 runnerHttpApi *api.RunnerHttpApi10 //workStatusCache map[string]WorkStatusCache11}12type WorkStatusCache struct {13}14func NewRunnerAlive(runnerHttpApi *api.RunnerHttpApi) RunnerAlive {15 return RunnerAlive{16 runnerHttpApi: runnerHttpApi,17 }18}19func (alive *RunnerAlive) alive(20 runnerStatus *work.ManagerStatus,21 workersStatus map[string]work.JobOutStatus,22 acceptRunningJobIds []string,23 denyRunningJobIds []string,24) (*api.AliveResponse, error) {25 aliveRequest := api.AliveRequest{26 HostStatus: api.HostStatus{},27 RunnerStatus: api.RunnerStatus{RunningNum: runnerStatus.RunningNum},28 JobsStatus: changeJobStatusToRequest(workersStatus),29 AcceptJobs: acceptRunningJobIds,30 DenyJobs: denyRunningJobIds,31 }32 aliveResponse, err := alive.runnerHttpApi.AliveToServer(&aliveRequest)33 if err != nil {34 return nil, err35 }36 return aliveResponse, nil37}38func changeJobStatusToRequest(workerStatus map[string]work.JobOutStatus) []api.JobsStatus {39 var jobsStatus []api.JobsStatus40 for _, workStatus := range workerStatus {41 status := api.JobsStatus{42 JobRunningId: workStatus.JobRunningId,43 AtomLogs: workStatus.AtomLogs,44 Finished: workStatus.Finished,45 }46 if workStatus.Finished {...

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := NewRunner(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to interrupt")8 os.Exit(2)9 fmt.Println("Terminating due to timeout")10 os.Exit(1)11 }12 }13 fmt.Println("Process ended")14}15import (16var (17 ErrTimeout = errors.New("received timeout")18 ErrInterrupt = errors.New("received interrupt")19type Runner struct {20 tasks []func(int)21}22func NewRunner(d time.Duration) *Runner {23 return &Runner{24 complete: make(chan error),25 timeout: time.After(d),26 interrupt: make(chan os.Signal, 1),27 }28}29func (r *Runner) Add(tasks ...func(int)) {30 r.tasks = append(r.tasks, tasks...)31}32func (r *Runner) Start() error {33 signal.Notify(r.interrupt, os.Interrupt)34 go func() {35 r.complete <- r.run()36 }()37 select {38 }39}40func (r *Runner) run() error {41 for id, task := range r.tasks {42 if r.gotInterrupt() {43 }44 task(id)45 }46}47func (r *Runner) gotInterrupt() bool {48 select {49 signal.Stop(r.interrupt)50 }51}52import (53func createTask() func(int) {54 return func(id int) {55 fmt.Printf("Processor - Task #%d.\n", id)56 time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)57 }58}

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := NewRunner(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to interrupt")8 os.Exit(2)9 fmt.Println("Terminating due to timeout")10 os.Exit(1)11 }12 }13 fmt.Println("Process ended")14}15import (16type Runner struct {17 tasks []func(int)18}19var ErrTimeout = errors.New("received timeout")20var ErrInterrupt = errors.New("received interrupt")21func NewRunner(timeout time.Duration) *Runner {22 return &Runner{23 interrupt: make(chan os.Signal, 1),24 complete: make(chan error),25 timeout: time.After(timeout),26 }27}28func (r *Runner) Add(tasks ...func(int)) {29 r.tasks = append(r.tasks, tasks...)30}31func (r *Runner) Start() error {32 signal.Notify(r.interrupt, os.Interrupt)33 go func() {34 r.complete <- r.run()35 }()36 select {37 }38}39func (r *Runner) run() error {

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := NewRunner(30 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to interrupt")8 fmt.Println("Terminating due to timeout")9 }10 }11 fmt.Println("Process ended")12}13import (14func main() {15 r := NewRunner(30 * time.Second)16 r.Add(createTask(), createTask(), createTask())17 if err := r.Start(); err != nil {18 switch err {19 fmt.Println("Terminating due to interrupt")20 fmt.Println("Terminating due to timeout")21 }22 }23 fmt.Println("Process ended")24}25import (26func main() {27 r := NewRunner(30 * time.Second)28 r.Add(createTask(), createTask(), createTask())29 if err := r.Start(); err != nil {30 switch err {31 fmt.Println("Terminating due to interrupt")32 fmt.Println("Terminating due to timeout")

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 r := runner.NewRunner(3*time.Second, 10*time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to interrupt")8 os.Exit(2)9 fmt.Println("Terminating due to timeout")10 os.Exit(1)11 }12 }13 fmt.Println("Process ended")14}15import (16func main() {17 fmt.Println("Starting work.")18 r := runner.New(timeout)19 r.Add(createTask(), createTask(), createTask())20 if err := r.Start(); err != nil {21 switch err {22 fmt.Println("Terminating due to interrupt")23 os.Exit(2)24 fmt.Println("Terminating due to timeout")25 os.Exit(1)26 }27 }28 fmt.Println("Process ended")29}30import (31func main() {32 fmt.Println("Starting work.")33 r := runner.New(timeout)34 r.Add(createTask(), createTask(), createTask())35 if err := r.Start(); err != nil {36 switch err {37 fmt.Println("Terminating due to interrupt")38 os.Exit(2)39 fmt.Println("Terminating due to timeout")40 os.Exit(1)41 }42 }43 fmt.Println("Process ended")44}45import (46func main() {47 fmt.Println("Starting work.")48 r := runner.New(timeout)49 r.Add(createTask(),

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Starting the program")4 r := runner.New(30 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 if err := r.Start(); err != nil {7 switch err {8 fmt.Println("Terminating due to interrupt")9 os.Exit(2)10 fmt.Println("Terminating due to timeout")11 os.Exit(1)12 }13 }14 fmt.Println("Process ended")15}16import (17func main() {18 fmt.Println("Starting the program")19 r := runner.New(30 * time.Second)20 r.Add(createTask(), createTask(), createTask())21 if err := r.Start(); err != nil {22 switch err {23 fmt.Println("Terminating due to interrupt")24 os.Exit(2)25 fmt.Println("Terminating due to timeout")26 os.Exit(1)27 }28 }29 fmt.Println("Process ended")30}31import (32func main() {33 fmt.Println("Starting the program")34 r := runner.New(30 * time.Second)35 r.Add(createTask(), createTask(), createTask())36 if err := r.Start(); err != nil {37 switch err {38 fmt.Println("Terminating due to interrupt")39 os.Exit(2)40 fmt.Println("Terminating due to timeout")41 os.Exit(1)42 }43 }44 fmt.Println("Process ended")45}46import (47func main() {48 fmt.Println("Starting the program")49 r := runner.New(30 * time.Second)50 r.Add(createTask(), createTask(), createTask())51 if err := r.Start(); err != nil {52 switch err {

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runner := golrunner.Runner{golconfig.Config{"1.go", "1.go"}}4 fmt.Println(runner.Alive())5}6import (7func main() {8 runner := golrunner.Runner{golconfig.Config{"2.go", "2.go"}}9 fmt.Println(runner.Alive())10}11import (12func main() {13 runner := golrunner.Runner{golconfig.Config{"3.go", "3.go"}}14 fmt.Println(runner.Alive())15}16import (17func main() {18 runner := golrunner.Runner{golconfig.Config{"4.go", "4.go"}}19 fmt.Println(runner.Alive())20}21import (22func main() {23 runner := golrunner.Runner{golconfig.Config{"5.go", "5.go"}}24 fmt.Println(runner.Alive())25}26import (27func main() {

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 r = runner{}4 r.run()5 fmt.Println(r.Alive())6}7import "fmt"8func main() {9 r = runner{}10 r.run()11 r.die()12 fmt.Println(r.Alive())13}14import "fmt"15func main() {16 r = runner{}17 fmt.Println(r.Alive())18}19import "fmt"20func main() {21 r = runner{}22 r.die()23 fmt.Println(r.Alive())24}25import "fmt"26func main() {27 r = runner{}28 r.die()29 r.run()30 fmt.Println(r.Alive())31}32import "fmt"33func main() {34 r = runner{}35 r.run()36 r.die()37 r.run()38 fmt.Println(r.Alive())39}40import "fmt"41func main() {42 r = runner{}43 r.run()44 r.run()45 fmt.Println(r.Alive())46}47import "fmt"48func main() {49 r = runner{}50 r.die()51 r.die()52 fmt.Println(r.Alive())53}54import "fmt"55func main() {56 r = runner{}57 r.run()58 r.die()59 r.die()60 fmt.Println(r.Alive())61}62import "fmt"63func main() {

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 r = &dog{}4 r.Alive()5 r = &cat{}6 r.Alive()7}

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := runner.New(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to timeout.")8 os.Exit(1)9 fmt.Println("Terminating due to interrupt.")10 os.Exit(2)11 }12 }13 fmt.Println("Process ended.")14}15func createTask() func(int) {16 return func(id int) {17 time.Sleep(time.Duration(id) * time.Second)18 }19}20import (21func main() {22 log.Println("Starting work.")23 timeout := flag.Duration("timeout",

Full Screen

Full Screen

Alive

Using AI Code Generation

copy

Full Screen

1import (2type Runner struct {3 tasks []func(int)4}5var ErrTimeout = errors.New("received timeout")6var ErrInterrupt = errors.New("received interrupt")7func New(d time.Duration) *Runner {8 return &Runner{9 interrupt: make(chan os.Signal, 1),10 complete: make(chan error),11 timeout: time.After(d),12 }13}14func (r *Runner) Add(tasks ...func(int)) {15 r.tasks = append(r.tasks, tasks...)16}17func (r *Runner) Start() error {18 signal.Notify(r.interrupt, os.Interrupt)19 for id, task := range r.tasks {20 r.wg.Add(1)21 go func(id int, task func

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful