github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/server/cron.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package server
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/robfig/cron"
    22  
    23  	"github.com/cloudwan/gohan/schema"
    24  	"github.com/cloudwan/gohan/util"
    25  )
    26  
    27  //CRON Process
    28  func startCRONProcess(server *Server) {
    29  	manager := schema.GetManager()
    30  	config := util.GetConfig()
    31  	jobList := config.GetParam("cron", nil)
    32  	if jobList == nil {
    33  		return
    34  	}
    35  	log.Info("Started CRON process")
    36  	c := cron.New()
    37  	for _, rawJob := range jobList.([]interface{}) {
    38  		job := rawJob.(map[string]interface{})
    39  		path := job["path"].(string)
    40  		timing := job["timing"].(string)
    41  		env := newEnvironment(server.db, server.keystoneIdentity)
    42  		err := env.LoadExtensionsForPath(manager.Extensions, path)
    43  		if err != nil {
    44  			log.Fatal(fmt.Sprintf("Extensions parsing error: %v", err))
    45  		}
    46  		log.Info("New job for %s / %s", path, timing)
    47  		c.AddFunc(timing, func() {
    48  			lockKey := lockPath + "/" + path
    49  			err := server.sync.Lock(lockKey, false)
    50  			if err != nil {
    51  				return
    52  			}
    53  			defer func() {
    54  				server.sync.Unlock(lockKey)
    55  			}()
    56  			context := map[string]interface{}{
    57  				"path": path,
    58  			}
    59  			if err != nil {
    60  				log.Warning(fmt.Sprintf("extension error: %s", err))
    61  				return
    62  			}
    63  			if err := env.HandleEvent("notification", context); err != nil {
    64  				log.Warning(fmt.Sprintf("extension error: %s", err))
    65  				return
    66  			}
    67  			return
    68  		})
    69  	}
    70  	c.Start()
    71  }
    72  
    73  func stopCRONProcess(server *Server) {
    74  
    75  }