go-hep.org/x/hep@v0.38.1/fwk/runner.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fwk
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"go-hep.org/x/hep/fwk/fsm"
    11  )
    12  
    13  // irunner wraps an appmgr to implement fwk.Scripter
    14  type irunner struct {
    15  	app *appmgr
    16  }
    17  
    18  func (ui irunner) lvl() Level {
    19  	return ui.app.msg.lvl
    20  }
    21  
    22  func (ui irunner) state() fsm.State {
    23  	return ui.app.state
    24  }
    25  
    26  func (ui *irunner) Configure() error {
    27  	ctx := ctxType{
    28  		id:    0,
    29  		slot:  0,
    30  		store: nil,
    31  		msg:   newMsgStream("<root>", ui.lvl(), nil),
    32  	}
    33  
    34  	err := ui.app.configure(ctx)
    35  	if err != nil {
    36  		return fmt.Errorf("fwk: could not configure application: %w", err)
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func (ui *irunner) Start() error {
    43  	ctx := ctxType{
    44  		id:    0,
    45  		slot:  0,
    46  		store: nil,
    47  		msg:   newMsgStream("<root>", ui.lvl(), nil),
    48  	}
    49  
    50  	if ui.state() < fsm.Configured {
    51  		return fmt.Errorf("fwk: invalid app state (%v). need at least %s", ui.state(), fsm.Configured)
    52  	}
    53  
    54  	err := ui.app.start(ctx)
    55  	if err != nil {
    56  		return fmt.Errorf("fwk: could not start application: %w", err)
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func (ui *irunner) Run(evtmax int64) error {
    63  	ctx := ctxType{
    64  		id:    0,
    65  		slot:  0,
    66  		store: nil,
    67  		msg:   newMsgStream("<root>", ui.lvl(), nil),
    68  	}
    69  
    70  	if ui.state() < fsm.Started {
    71  		return fmt.Errorf("fwk: invalid app state (%v). need at least %s", ui.state(), fsm.Started)
    72  	}
    73  
    74  	err := ui.app.run(ctx)
    75  	if err != nil {
    76  		return fmt.Errorf("fwk: could not run application: %w", err)
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (ui *irunner) Stop() error {
    83  	ctx := ctxType{
    84  		id:    0,
    85  		slot:  0,
    86  		store: nil,
    87  		msg:   newMsgStream("<root>", ui.lvl(), nil),
    88  	}
    89  
    90  	if ui.state() < fsm.Running {
    91  		return fmt.Errorf("fwk: invalid app state (%v). need at least %s", ui.state(), fsm.Running)
    92  	}
    93  
    94  	err := ui.app.stop(ctx)
    95  	if err != nil {
    96  		return fmt.Errorf("fwk: could not stop application: %w", err)
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func (ui *irunner) Shutdown() error {
   103  	ctx := ctxType{
   104  		id:    0,
   105  		slot:  0,
   106  		store: nil,
   107  		msg:   newMsgStream("<root>", ui.lvl(), nil),
   108  	}
   109  
   110  	if ui.state() < fsm.Stopped {
   111  		return fmt.Errorf("fwk: invalid app state (%v). need at least %s", ui.state(), fsm.Stopped)
   112  	}
   113  
   114  	err := ui.app.shutdown(ctx)
   115  	if err != nil {
   116  		return fmt.Errorf("fwk: could not shutdown application: %w", err)
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  var (
   123  	_ Scripter = (*irunner)(nil)
   124  )