github.com/iDigitalFlame/xmt@v0.5.4/examples/example_guardian.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"os/signal"
    24  
    25  	"github.com/iDigitalFlame/xmt/man"
    26  )
    27  
    28  func exampleGuardian() {
    29  	if len(os.Args) == 2 {
    30  		var s man.Sentinel
    31  		s.AddExecute(man.Self)
    32  		ok, err := s.Wake(man.Pipe, os.Args[1])
    33  		if err != nil {
    34  			fmt.Println("error", err)
    35  		}
    36  		if ok {
    37  			fmt.Println("launched!")
    38  		}
    39  
    40  		fmt.Println("pinged!")
    41  		return
    42  	}
    43  
    44  	var (
    45  		x, c   = context.WithCancel(context.Background())
    46  		g, err = man.GuardContext(x, man.Pipe, "testguard")
    47  	)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	s := make(chan os.Signal, 1)
    53  	signal.Notify(s)
    54  
    55  	<-s
    56  	c()
    57  	g.Wait()
    58  }