Esempio n. 1
0
func (e CampaignEnqueuer) Enqueue(campaign collections.Campaign, jobType string) error {
	_, err := e.gobbleQueue.Enqueue(gobble.NewJob(CampaignJob{
		JobType:  jobType,
		Campaign: campaign,
	}))
	if err != nil {
		return errors.New(fmt.Sprintf("there was an error enqueuing the job: %s", err))
	}

	return nil
}
Esempio n. 2
0
	"github.com/cloudfoundry-incubator/notifications/gobble"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Job", func() {
	Describe("NewJob", func() {
		It("creates an instance of Job with a serialized payload", func() {
			data := map[string]string{
				"test":    "testing a new job",
				"example": "another field",
			}

			job := gobble.NewJob(data)

			Expect(job).To(BeAssignableToTypeOf(gobble.Job{}))
			Expect(job.Payload).To(Equal(`{"example":"another field","test":"testing a new job"}`))
		})
	})

	Describe("Unmarshal", func() {
		It("unmarshals the payload into the given object", func() {
			data := map[string]string{
				"test":    "testing a new job",
				"example": "another field",
			}

			job := gobble.NewJob(data)
	BeforeEach(func() {
		gobbleQueue = mocks.NewQueue()
		enqueuer = queue.NewCampaignEnqueuer(gobbleQueue)
		campaign = collections.Campaign{
			ID: "27",
		}
	})

	Context("Enqueue", func() {
		It("puts a campaign on the queue", func() {
			err := enqueuer.Enqueue(campaign, "campaign")
			Expect(err).NotTo(HaveOccurred())

			Expect(gobbleQueue.EnqueueCall.Receives.Jobs).To(HaveLen(1))
			Expect(gobbleQueue.EnqueueCall.Receives.Jobs[0]).To(Equal(gobble.NewJob(queue.CampaignJob{
				JobType:  "campaign",
				Campaign: campaign,
			})))
		})

		Context("when an enqueuing occurs", func() {
			BeforeEach(func() {
				gobbleQueue.EnqueueCall.Returns.Error = errors.New("some-error")
			})

			It("returns an error", func() {
				err := enqueuer.Enqueue(campaign, "campaign")
				Expect(err).To(HaveOccurred())
				Expect(err).To(MatchError("there was an error enqueuing the job: some-error"))
			})
		})
	})
Esempio n. 4
0
		spaceStrategy = mocks.NewStrategy()
		orgStrategy = mocks.NewStrategy()
		emailStrategy = mocks.NewStrategy()
		database = mocks.NewDatabase()
		determiner = strategy.NewStrategyDeterminer(userStrategy, spaceStrategy, orgStrategy, emailStrategy)
	})

	Context("when dispatching to a user", func() {
		It("determines the strategy and calls it", func() {
			err := determiner.Determine(database.Connection(), "some-uaa-host", gobble.NewJob(queue.CampaignJob{
				Campaign: collections.Campaign{
					ID:             "some-id",
					SendTo:         map[string]string{"user": "******"},
					CampaignTypeID: "some-campaign-type-id",
					Text:           "some-text",
					HTML:           "<h1>my-html</h1>",
					Subject:        "The Best subject",
					TemplateID:     "some-template-id",
					ReplyTo:        "*****@*****.**",
					ClientID:       "some-client-id",
				},
			}))

			Expect(err).NotTo(HaveOccurred())
			Expect(userStrategy.DispatchCall.Receives.Dispatch).To(Equal(services.Dispatch{
				JobType:    "v2",
				GUID:       "some-user-guid",
				UAAHost:    "some-uaa-host",
				Connection: database.Connection(),
				TemplateID: "some-template-id",
				CampaignID: "some-id",
			StrategyDeterminer:     strategyDeterminer,
			Database:               database,
			UAAHost:                "my-uaa-host",
			MessageStatusUpdater:   messageStatusUpdater,
		}

		v1Workflow = mocks.NewProcess()
		v2Workflow = mocks.NewWorkflow()
		worker = postal.NewDeliveryWorker(v1Workflow, v2Workflow, config)
	})

	Describe("Work", func() {
		It("pops Deliveries off the queue, sending emails for each", func() {
			reserveChan := make(chan gobble.Job)
			go func() {
				reserveChan <- gobble.NewJob(delivery)
			}()
			queue.ReserveCall.Returns.Chan = reserveChan

			worker.Work()

			<-time.After(10 * time.Millisecond)
			worker.Halt()

			Expect(v1Workflow.DeliverCall.CallCount).To(Equal(1))
		})

		It("can be halted", func() {
			go func() {
				worker.Halt()
			}()
Esempio n. 6
0
		TruncateTables()
		database = gobble.NewDatabase(sqlDB)

		queue = gobble.NewQueue(database, gobble.Config{
			WaitMaxDuration: 50 * time.Millisecond,
		})
	})

	AfterEach(func() {
		queue.Close()
	})

	Describe("Enqueue", func() {
		It("sticks the job in the database table", func() {
			job := gobble.NewJob(map[string]bool{
				"testing": true,
			})

			job, err := queue.Enqueue(job)
			Expect(err).NotTo(HaveOccurred())

			results, err := database.Connection.Select(gobble.Job{}, "SELECT * FROM `jobs`")
			if err != nil {
				panic(err)
			}

			jobs := []gobble.Job{}
			for _, result := range results {
				jobs = append(jobs, *(result.(*gobble.Job)))
			}
Esempio n. 7
0
				Subject:    "the subject",
				Text:       "body content",
				ReplyTo:    "*****@*****.**",
				KindID:     "some-kind",
				TemplateID: "some-template-id",
			},
			MessageID:     messageID,
			VCAPRequestID: "some-request-id",
		}
	})

	Describe("Deliver", func() {
		var job *gobble.Job

		BeforeEach(func() {
			j := gobble.NewJob(delivery)
			job = &j
		})

		It("logs the email address of the recipient", func() {
			v1Process.Deliver(job, logger)

			lines, err := parseLogLines(buffer.Bytes())
			Expect(err).NotTo(HaveOccurred())

			Expect(lines).To(ContainElement(logLine{
				Source:   "notifications",
				Message:  "notifications.worker.delivery-start",
				LogLevel: int(lager.INFO),
				Data: map[string]interface{}{
					"session":         "1",
Esempio n. 8
0
func (enqueuer Enqueuer) Enqueue(conn ConnectionInterface, users []User, options Options, space cf.CloudControllerSpace, organization cf.CloudControllerOrganization, clientID, uaaHost, scope, vcapRequestID string, reqReceived time.Time) []Response {

	responses := []Response{}
	jobsByMessageID := map[string]gobble.Job{}
	for _, user := range users {
		guid, err := enqueuer.guidGenerator()
		if err != nil {
			panic(err)
		}
		messageID := guid.String()

		jobsByMessageID[messageID] = gobble.NewJob(Delivery{
			Options:         options,
			UserGUID:        user.GUID,
			Email:           user.Email,
			Space:           space,
			Organization:    organization,
			ClientID:        clientID,
			MessageID:       messageID,
			UAAHost:         uaaHost,
			Scope:           scope,
			VCAPRequestID:   vcapRequestID,
			RequestReceived: reqReceived,
		})

		recipient := user.Email
		if recipient == "" {
			recipient = user.GUID
		}

		responses = append(responses, Response{
			Status:         StatusQueued,
			NotificationID: messageID,
			Recipient:      recipient,
			VCAPRequestID:  vcapRequestID,
		})
	}

	transaction := conn.Transaction()
	transaction.Begin()
	for messageID := range jobsByMessageID {
		_, err := enqueuer.messagesRepo.Upsert(transaction, models.Message{
			ID:     messageID,
			Status: StatusQueued,
		})
		if err != nil {
			transaction.Rollback()
			return []Response{}
		}
	}
	err := transaction.Commit()
	if err != nil {
		return []Response{}
	}

	for _, job := range jobsByMessageID {
		_, err := enqueuer.queue.Enqueue(job)
		if err != nil {
			return []Response{}
		}
	}

	return responses
}