github.com/matthieudolci/hatcher@v0.2.8/scheduler/scheduler.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"database/sql"
     5  	"time"
     6  
     7  	"github.com/matthieudolci/hatcher/common"
     8  	"github.com/matthieudolci/hatcher/standup"
     9  
    10  	log "github.com/Sirupsen/logrus"
    11  	"github.com/matthieudolci/gocron"
    12  	"github.com/matthieudolci/hatcher/database"
    13  )
    14  
    15  var scheduler *gocron.Scheduler
    16  var stop chan bool
    17  
    18  // GetTimeAndUsersForScheduler gets the time selected by a user for standup
    19  func GetTimeAndUsersForScheduler(s *common.Slack) error {
    20  	type ScheduleData struct {
    21  		TimesStandup sql.NullString
    22  		UserID       string
    23  	}
    24  
    25  	rows, err := database.DB.Query("SELECT to_char(standup_schedule, 'HH24:MI'), userid FROM hatcher.users;")
    26  	if err != nil {
    27  		if err == sql.ErrNoRows {
    28  			log.WithError(err).Error("There is no result time or userid")
    29  		}
    30  	}
    31  	defer rows.Close()
    32  	if scheduler != nil {
    33  		stop <- true
    34  		scheduler.Clear()
    35  	}
    36  	scheduler = gocron.NewScheduler()
    37  	for rows.Next() {
    38  		scheduledata := ScheduleData{}
    39  		err = rows.Scan(&scheduledata.TimesStandup, &scheduledata.UserID)
    40  		if err != nil {
    41  			log.WithError(err).Error("During the scan")
    42  		}
    43  
    44  		if scheduledata.TimesStandup.Valid {
    45  			err := runSchedulerStandup(s, scheduledata.TimesStandup.String, scheduledata.UserID)
    46  			if err != nil {
    47  				log.WithError(err).Error("Running runSchedulerStandup failed")
    48  			}
    49  		} else {
    50  			log.Info("Nothing to schedule standup")
    51  
    52  		}
    53  	}
    54  	stop = scheduler.Start()
    55  	// get any error encountered during iteration
    56  	err = rows.Err()
    57  	if err != nil {
    58  		log.WithError(err).Error("During iteration")
    59  	}
    60  	return nil
    61  }
    62  
    63  // Runs the jobs standupYesterdayScheduled at a times defined by the user
    64  func runSchedulerStandup(s *common.Slack, timeStandup, userid string) error {
    65  
    66  	location, err := time.LoadLocation("America/Vancouver")
    67  	if err != nil {
    68  		log.Println("Unfortunately can't load a location")
    69  		log.Println(err)
    70  	} else {
    71  		gocron.ChangeLoc(location)
    72  	}
    73  
    74  	scheduler.Every(1).Monday().At(timeStandup).Do(standup.AskStandupYesterdayScheduled, s, userid)
    75  	scheduler.Every(1).Tuesday().At(timeStandup).Do(standup.AskStandupYesterdayScheduled, s, userid)
    76  	scheduler.Every(1).Wednesday().At(timeStandup).Do(standup.AskStandupYesterdayScheduled, s, userid)
    77  	scheduler.Every(1).Thursday().At(timeStandup).Do(standup.AskStandupYesterdayScheduled, s, userid)
    78  	scheduler.Every(1).Friday().At(timeStandup).Do(standup.AskStandupYesterdayScheduled, s, userid)
    79  	log.WithFields(log.Fields{
    80  		"userid": userid,
    81  		"time":   timeStandup,
    82  	}).Info("Standup schedule tasks posted")
    83  
    84  	return nil
    85  }