forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to handle throttled expirations
- Loading branch information
1 parent
0eafabc
commit ee6a392
Showing
6 changed files
with
200 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package expirations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/tasks" | ||
"github.com/nyaruka/mailroom/core/tasks/handler" | ||
"github.com/nyaruka/mailroom/core/tasks/handler/ctasks" | ||
"github.com/nyaruka/mailroom/runtime" | ||
) | ||
|
||
// TypeBulkExpire is the type of the task | ||
const TypeBulkExpire = "bulk_expire" | ||
|
||
func init() { | ||
tasks.RegisterType(TypeBulkExpire, func() tasks.Task { return &BulkExpireTask{} }) | ||
} | ||
|
||
// BulkExpireTask is the payload of the task | ||
type BulkExpireTask struct { | ||
Expirations []*ExpiredWait `json:"expirations"` | ||
} | ||
|
||
func (t *BulkExpireTask) Type() string { | ||
return TypeBulkExpire | ||
} | ||
|
||
// Timeout is the maximum amount of time the task can run for | ||
func (t *BulkExpireTask) Timeout() time.Duration { | ||
return time.Hour | ||
} | ||
|
||
func (t *BulkExpireTask) WithAssets() models.Refresh { | ||
return models.RefreshNone | ||
} | ||
|
||
// Perform creates the actual task | ||
func (t *BulkExpireTask) Perform(ctx context.Context, rt *runtime.Runtime, oa *models.OrgAssets) error { | ||
rc := rt.RP.Get() | ||
defer rc.Close() | ||
|
||
for _, exp := range t.Expirations { | ||
err := handler.QueueTask(rc, oa.OrgID(), exp.ContactID, ctasks.NewWaitExpiration(exp.SessionID, exp.WaitExpiresOn)) | ||
if err != nil { | ||
return fmt.Errorf("error queuing handle task for expiration on session #%d: %w", exp.SessionID, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package expirations_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/nyaruka/gocommon/dates" | ||
"github.com/nyaruka/mailroom/core/tasks/expirations" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/testsuite/testdata" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBulkExpire(t *testing.T) { | ||
_, rt := testsuite.Runtime() | ||
defer testsuite.Reset(testsuite.ResetRedis) | ||
|
||
defer dates.SetNowFunc(time.Now) | ||
dates.SetNowFunc(dates.NewFixedNow(time.Date(2024, 11, 15, 13, 59, 0, 0, time.UTC))) | ||
|
||
testsuite.QueueBatchTask(t, rt, testdata.Org1, &expirations.BulkExpireTask{ | ||
Expirations: []*expirations.ExpiredWait{ | ||
{SessionID: 123456, ContactID: testdata.Cathy.ID, WaitExpiresOn: time.Date(2024, 11, 15, 13, 57, 0, 0, time.UTC)}, | ||
{SessionID: 234567, ContactID: testdata.Bob.ID, WaitExpiresOn: time.Date(2024, 11, 15, 13, 58, 0, 0, time.UTC)}, | ||
}, | ||
}) | ||
|
||
assert.Equal(t, map[string]int{"bulk_expire": 1}, testsuite.FlushTasks(t, rt, "batch", "throttled")) | ||
|
||
testsuite.AssertContactTasks(t, testdata.Org1, testdata.Cathy, []string{ | ||
`{"type":"expiration_event","task":{"session_id":123456,"time":"2024-11-15T13:57:00Z"},"queued_on":"2024-11-15T13:59:00Z"}`, | ||
}) | ||
testsuite.AssertContactTasks(t, testdata.Org1, testdata.Bob, []string{ | ||
`{"type":"expiration_event","task":{"session_id":234567,"time":"2024-11-15T13:58:00Z"},"queued_on":"2024-11-15T13:59:00Z"}`, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.