Пример #1
0
func main() {
	log.SetFlags(log.Lshortfile)
	log.SetPrefix("beanc:")
	flag.Parse()

	if *version {
		fmt.Printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR)
		os.Exit(1)
	}

	if *verbose {
		fmt.Println("Verbose")
	}

	conn, err := gobeanstalk.Dial(*host)
	if err != nil {
		log.Printf("connect failed")
		log.Fatal(err)
	}

	switch flag.Arg(0) {
	case "push":
		pushCommand(conn)
	case "pull":
		pullCommand(conn)
	default:
		log.Println("Unrecognised command")
		os.Exit(-1)
	}

	os.Exit(0)
}
Пример #2
0
func Index(w http.ResponseWriter, r *http.Request) {
	data := []entry{
		{"Bicycle {2014-04-22}", "To the house and up the elevator. To the documents then back down even further. On my bicycle passing walkers. To the left. To the right. At the bank. Upstairs for candy again. Thank you very much. On my way again."},
	}

	if r.Method == "POST" {
		fmt.Println(r.FormValue("email"))

		conn, err := gobeanstalk.Dial("localhost:11300")

		if err != nil {
			log.Fatal(err)
		}

		id, err := conn.Put([]byte(r.FormValue("Name")), 0, 0, 10)

		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("Job %d inserted\n", id)
	}

	t := render("templates/layout.html", "templates/index.html")

	t.Execute(w, data)
}
Пример #3
0
func main() {
	// Connect to beanstalk
	beanstalkConn, err := beanstalk.Dial("127.0.0.1:11300")
	if err != nil {
		log.Fatal(err)
	}

	// Pick a tube
	err = beanstalkConn.Use("unsorted")
	if err != nil {
		log.Fatal(err)
	}

	data := []DataRow{
		{4, 0, 0, 0},
		{2, 0, 0, 0},
		{3, 0, 0, 0},
		{6, 0, 0, 0},
		{1, 0, 0, 0},
		{5, 0, 0, 0},
	}

	// Serialize
	serializedJob, err := json.Marshal(Dataset{2, 7, 3, data})
	if err != nil {
		log.Fatal(err)
	}

	// Store job in queue
	_, err = beanstalkConn.Put(serializedJob, 0, 0, 120)
	if err != nil {
		log.Fatal(err)
	}
}
Пример #4
0
/*
 * function to wait for beanstalk
 * takes an one directional channel of type Message
 */
func consumeFromBeanstalk(c chan<- Message) {
	// create a beanstalkd connection
	beanstalkdConn := os.Getenv("BEANSTALKD")

	conn, err := gobeanstalk.Dial(beanstalkdConn)
	if err != nil {
		log.Printf("connect failed")
		log.Fatal(err)
	}

	log.Printf("Connected to Beanstalk")

	// set the connection to only watch the 'emails' channel
	cnt, err := conn.Watch("emails")
	if err != nil {
		log.Println("Unable to watch email queue")
		log.Fatal(err)
	}

	log.Println("Current Tube Count: %d", cnt)

	// infinitely loop
	// since this is in it's own thead, it doesn't block
	// main program excution
	for {
		j, err := conn.Reserve()
		if err != nil {
			log.Println("reserve failed")
			log.Fatal(err)
		}
		log.Printf("id:%d, body:%s\n", j.Id, string(j.Body))

		// create a new Message and load in the json
		// that we get from beanstalk
		var m Message
		jerr := json.Unmarshal(j.Body, &m)

		if jerr != nil {
			log.Println("Bad Json")
			log.Fatal(jerr)
		}

		// Send the Message to the channel
		c <- m

		// delete the job from the tube
		err = conn.Delete(j.Id)
		if err != nil {
			log.Fatal(err)
		}
	}
}
Пример #5
0
func main() {
	// Connect to beanstalk
	beanstalkConn, err := beanstalk.Dial("127.0.0.1:11300")
	if err != nil {
		log.Fatal(err)
	}

	// Pick a tube
	_, err = beanstalkConn.Watch("unsorted")
	if err != nil {
		log.Fatal(err)
	}

	// Fetch a job
	job, err := beanstalkConn.Reserve()
	if err != nil {
		log.Fatal(err)
	}

	// Deserialize
	var jobData Dataset
	err = json.Unmarshal(job.Body, &jobData)
	if err != nil {
		log.Fatal(err)
	}

	// Sort
	toSort := xDecending{jobData}
	sort.Sort(toSort)
	sorted := toSort.Dataset

	// Put back sorted data into a different beanstalk queue
	err = beanstalkConn.Use("sortedX")
	if err != nil {
		log.Fatal(err)
	}
	serializedJob, err := json.Marshal(sorted)
	if err != nil {
		log.Fatal(err)
	}
	_, err = beanstalkConn.Put(serializedJob, 0, 0, 120)
	if err != nil {
		log.Fatal(err)
	}

	// Delete successfully handled job
	beanstalkConn.Delete(job.Id)

	if err != nil {
		fmt.Println(err)
	}
}
Пример #6
0
func main() {
	data, err := ioutil.ReadFile("config.json")

	if err != nil {
		log.Fatalln(err)
	}

	var c Config

	err = json.Unmarshal(data, &c)

	db, err := sql.Open("mysql", c.Username+":"+c.Password+"@/"+c.Database)

	if err != nil {
		log.Fatalln(err)
	}

	defer db.Close()

	conn, err := gobeanstalk.Dial(c.Address)

	if err != nil {
		log.Fatalln(err)
	}

	mg_client := mailgun.NewClient(c.Key, c.Mailbox)

	for {
		j, err := conn.Reserve()

		if err != nil {
			log.Fatalln("reserve failed", err)
		}

		err = conn.Delete(j.Id)

		if err != nil {
			log.Fatalln(err)
		}

		email := string(j.Body)

		t := time.Now()
		t.Format("2006-01-02 15:04:05")

		result, err := db.Exec(`INSERT INTO email(email, created_at) VALUES(?,?);`, email, t)

		if err != nil {
			log.Println("db error", err, result)
		}

		message := mailgun.Message{
			Body:        c.Body,
			FromAddress: c.FromAddress,
			FromName:    c.FromName,
			Subject:     c.Subject,
			ToAddress:   email,
		}

		log.Println("Attempting to send to ", mg_client.Endpoint(message))

		body, err := mg_client.Send(message)

		if err != nil {
			log.Println("Got an error:", err)
		} else {
			log.Println(body)
		}
	}
}