Ejemplo n.º 1
0
func main() {
	service, err := cocaine.NewService("echo")
	if err != nil {
		fmt.Printf("Unable to create service: %s\n", err)
		return
	}

	res, isOpened := <-service.Call("enqueue", "ping", "Hello, World!!!")
	if !isOpened {
		fmt.Println("Stream closed")
		return
	}

	if res.Err() != nil {
		fmt.Printf("Calling error: %s\n", res.Err())
		return
	}

	var result string
	err = res.Extract(&result)
	if err != nil {
		fmt.Printf("Extraction error: %s", err)
		return
	}

	fmt.Println(result)
}
Ejemplo n.º 2
0
func MustCreateService(name string) *cocaine.Service {
	service, err := cocaine.NewService(name)
	if err != nil {
		panic(fmt.Sprintf("Unable to create Cocaine service %s, but must %v", name, err))
	}
	return service
}
Ejemplo n.º 3
0
func NewAppWrapper(name string, endpoint string) (wa appWrapper, err error) {
	app, err := cocaine.NewService("flow-tools", endpoint)
	if err != nil {
		log.Printf("Error %s", err)
		return nil, err
	}
	wa = &wrappedApp{app}
	return
}
Ejemplo n.º 4
0
func NewCocaineCache(config *configs.PluginConfig) (Cache, error) {
	storage, err := cocaine.NewService("storage")
	if err != nil {
		return nil, err
	}

	c := &CocaineCache{
		storage: storage,
	}

	return c, nil
}
Ejemplo n.º 5
0
func resolve(appname, endpoint string) <-chan resolveInfo {
	res := make(chan resolveInfo)
	go func() {
		app, err := cocaine.NewService(appname, endpoint)
		select {
		case res <- resolveInfo{
			slave: NewSlave(app),
			err:   err,
		}:
		default:
			if err == nil {
				app.Close()
			}
		}
	}()
	return res
}
Ejemplo n.º 6
0
func main() {
	binds := map[string]cocaine.EventHandler{
		"generate":      on_generate,
		"generate-http": on_http_generate,
	}

	Worker, err := cocaine.NewWorker()
	if err != nil {
		panic(err)
	}

	storage, err = cocaine.NewService("storage")
	if err != nil {
		panic(err)
	}

	Worker.Loop(binds)
}
Ejemplo n.º 7
0
func (c *cacher) create(name string) (*cocaine.Service, error) {
	c.mutex.Lock()
	defer c.mutex.Unlock()
	s, ok := c.get(name)
	if ok {
		return s, nil
	}

	s, err := cocaine.NewService(name)
	if err != nil {
		return nil, err
	}

	data := c.data.Load().(cache)
	data[name] = s
	c.data.Store(data)

	return s, nil
}
Ejemplo n.º 8
0
func (l *localCocaineResolver) Resolve(_ context.Context, name string) (Slave, error) {
	cachemap := l.cache.Load().(cacheMap)
	if slave, ok := cachemap[name]; ok {
		return slave, nil
	}

	l.mu.Lock()
	defer l.mu.Unlock()
	cachemap = l.cache.Load().(cacheMap)
	if slave, ok := cachemap[name]; ok {
		return slave, nil
	}

	app, err := cocaine.NewService(name)
	if err != nil {
		return nil, err
	}

	slv := NewSlave(app)
	cachemap[name] = slv
	l.cache.Store(cachemap)
	return slv, nil
}
Ejemplo n.º 9
0
func Parsing(task Task) (err error) {
	log := cocaine.NewLogger()
	log.Info("Start ", task)
	defer log.Close()

	//Wrap it
	log.Debug("Create configuration manager")
	cfgManager := cocaine.NewService(common.CFGMANAGER)
	defer cfgManager.Close()

	log.Debug("Fetch configuration file")
	res := <-cfgManager.Call("enqueue", "parsing", task.Config)
	if err = res.Err(); err != nil {
		return
	}
	var rawCfg []byte
	if err = res.Extract(&rawCfg); err != nil {
		return
	}
	var cfg common.ParsingConfig
	common.Encode(rawCfg, &cfg)

	res = <-cfgManager.Call("enqueue", "common", "")
	if err = res.Err(); err != nil {
		return
	}
	if err = res.Extract(&rawCfg); err != nil {
		return
	}
	var combainerCfg common.CombainerConfig
	common.Encode(rawCfg, &combainerCfg)

	common.MapUpdate(&(combainerCfg.CloudCfg.DF), &(cfg.DF))
	cfg.DF = combainerCfg.CloudCfg.DF
	common.MapUpdate(&(combainerCfg.CloudCfg.DS), &(cfg.DS))
	cfg.DS = combainerCfg.CloudCfg.DS
	common.MapUpdate(&(combainerCfg.CloudCfg.DG), &(cfg.DG))
	cfg.DG = combainerCfg.CloudCfg.DG

	fetcherType, err := common.GetType(cfg.DF)
	if err != nil {
		return
	}

	log.Info(fmt.Sprintf("Use %s for fetching data", fetcherType))
	fetcher := cocaine.NewService(fetcherType)
	defer fetcher.Close()

	fetcherTask := common.FetcherTask{
		Target:    task.Host,
		StartTime: task.PrevTime,
		EndTime:   task.CurrTime,
	}

	ft := struct {
		Config map[string]interface{} "Config"
		Task   common.FetcherTask     "Task"
	}{cfg.DF, fetcherTask}

	js, _ := common.Pack(ft)

	res = <-fetcher.Call("enqueue", "get", js)
	if err = res.Err(); err != nil {
		return
	}

	var t []byte
	if err = res.Extract(&t); err != nil {
		return
	}

	// ParsingApp stage
	parserApp := cocaine.NewService(common.PARSINGAPP)
	defer parserApp.Close()
	taskToParser, err := common.Pack([]interface{}{cfg.Parser, t})
	if err != nil {
		return
	}
	res = <-parserApp.Call("enqueue", "parse", taskToParser)
	if err = res.Err(); err != nil {
		return
	}
	var z interface{}
	if err = res.Extract(&z); err != nil {
		return
	}

	// Datagrid stage
	dgType, err := common.GetType(cfg.DG)
	if err != nil {
		return
	}

	log.Info(fmt.Sprintf("Use %s for handle data", dgType))
	datagrid := cocaine.NewService(dgType)
	defer datagrid.Close()
	taskToDatagrid, err := common.Pack([]interface{}{cfg.DG, z})
	if err != nil {
		return
	}
	res = <-datagrid.Call("enqueue", "put", taskToDatagrid)
	if err = res.Err(); err != nil {
		return
	}
	var token string
	if err = res.Extract(&token); err != nil {
		return
	}
	log.Info(token)
	defer func() {
		taskToDatagrid, _ = common.Pack([]interface{}{cfg.DG, token})
		<-datagrid.Call("enqueue", "drop", taskToDatagrid)
	}()

	log.Info("Stop ", task)
	return nil
}