Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Messages weren't flushed to the database when send.immediately is false #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,29 @@ logger('grails.plugins.quartz', DEBUG, ['STDOUT'])
...
```

Mapping
-------

The default mappings can be overriden with the following options:
```groovy
asynchronous.mapping.datasource='mailDs'
asynchronous.mapping.message.table='emails'
asynchronous.mapping.attachment.table='email_attachments'
asynchronous.constraints.attachment.size=30*1024*1024
```

or you can add a custom closure like this:
```groovy
asynchronous.mapping.message= {

}
asynchronous.constraints.attachment={
content(maxSize:25*1024*1024) // 25 Mb attachment
}

```
This closure runs after the default mappings/constraints closure.

Indexes
-------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package grails.plugin.asyncmail

import static grails.util.Holders.config as HC

class AsynchronousMailAttachment implements Serializable {
static final CONF_DS = 'asynchronous.mapping.datasource'
static final CONF_MAPPING = 'asynchronous.mapping.attachment'
static final CONF_MAPPING_TABLE = 'asynchronous.mapping.attachment.table'
static final CONF_CONSTRAINTS = 'asynchronous.constraints.attachment'
static final CONF_SIZE = 'asynchronous.constraints.attachment.size'


static final DEFAULT_MIME_TYPE = 'application/octet-stream'

private static final SIZE_30_MB = 30*1024*1024
Expand All @@ -14,13 +22,32 @@ class AsynchronousMailAttachment implements Serializable {
static belongsTo = [message:AsynchronousMailMessage]

static mapping = {
table 'async_mail_attachment'
if(HC.getProperty(CONF_DS)){
datasource HC.getProperty(CONF_DS)
}

table HC.getProperty(CONF_MAPPING_TABLE,'async_mail_attachment')

version false

def customMapping = HC.getProperty(CONF_MAPPING,Closure)
if(customMapping){
def mappingCode = customMapping.rehydrate(delegate,this,this)
mappingCode.resolveStrategy = Closure.DELEGATE_FIRST
mappingCode()
}
}

static constraints = {
attachmentName(blank:false)
//mimeType()
content(maxSize:SIZE_30_MB)
content(maxSize:HC.getProperty(CONF_SIZE,Integer.class,SIZE_30_MB))

def customConstraints = HC.getProperty(CONF_CONSTRAINTS,Closure)
if(customConstraints){
def constraintCode = customConstraints.rehydrate(delegate,this,this)
constraintCode.resolveStrategy = Closure.DELEGATE_FIRST
constraintCode()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package grails.plugin.asyncmail

import static grails.plugin.asyncmail.enums.MessageStatus.*
import static grails.util.Holders.config as HC

import grails.plugin.asyncmail.enums.MessageStatus
import groovy.transform.ToString
import org.apache.commons.lang.StringUtils

import static grails.plugin.asyncmail.enums.MessageStatus.*

@ToString(includeNames = true, includeFields = true, includes = 'id,subject,to,status')
class AsynchronousMailMessage implements Serializable {
static final CONF_DS = 'asynchronous.mapping.datasource'
static final CONF_MAPPING = 'asynchronous.mapping.message'
static final CONF_MAPPING_TABLE = 'asynchronous.mapping.message.table'
static final CONF_CONSTRAINTS = 'asynchronous.constraints.message'


/**
* This date is accepted as the max date because different DBMSs store dates in
* different formats. We can't use a date which is the maximum in Java.
Expand Down Expand Up @@ -119,7 +125,11 @@ class AsynchronousMailMessage implements Serializable {

static hasMany = [to: String, cc: String, bcc: String, attachments: AsynchronousMailAttachment]
static mapping = {
table 'async_mail_mess'
if(HC.getProperty(CONF_DS)){
datasource HC.getProperty(CONF_DS)
}

table HC.getProperty(CONF_MAPPING_TABLE,'async_mail_mess')

from column: 'from_column'

Expand Down Expand Up @@ -169,6 +179,13 @@ class AsynchronousMailMessage implements Serializable {
text type: 'text'

attachments cascade: "all-delete-orphan"

def customMapping = HC.getProperty(CONF_MAPPING,Closure)
if(customMapping){
def mappingCode = customMapping.rehydrate(delegate,this,this)
mappingCode.resolveStrategy = Closure.DELEGATE_FIRST
mappingCode()
}
}

static constraints = {
Expand Down Expand Up @@ -245,5 +262,12 @@ class AsynchronousMailMessage implements Serializable {
maxAttemptsCount(min: 1)
lastAttemptDate(nullable: true)
attemptInterval(min: 0l)

def customConstraints = HC.getProperty(CONF_CONSTRAINTS,Closure)
if(customConstraints){
def constraintCode = customConstraints.rehydrate(delegate,this,this)
constraintCode.resolveStrategy = Closure.DELEGATE_FIRST
constraintCode()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class AsynchronousMailService {
AsynchronousMailPersistenceService asynchronousMailPersistenceService
AsynchronousMailMessageBuilderFactory asynchronousMailMessageBuilderFactory
AsynchronousMailConfigService asynchronousMailConfigService




/**
* Create asynchronous message and save it to the DB.
*
Expand Down Expand Up @@ -36,14 +38,25 @@ class AsynchronousMailService {
message.beginDate.time <= System.currentTimeMillis() &&
!asynchronousMailConfigService.disable


boolean flushOnSave
if(messageBuilder.useFlushOnSaveSetted){
flushOnSave = messageBuilder.useFlushOnSave
}
else{
flushOnSave = asynchronousMailConfigService.useFlushOnSave
}

// Save message to DB
def savedMessage = null
if(immediately && asynchronousMailConfigService.newSessionOnImmediateSend) {
AsynchronousMailMessage.withNewSession {
savedMessage = asynchronousMailPersistenceService.save(message, true, true)
savedMessage = asynchronousMailPersistenceService.save(message, flushOnSave, true)
}
} else {
savedMessage = asynchronousMailPersistenceService.save(message, immediately, true)
AsynchronousMailMessage.withTransaction {
savedMessage = asynchronousMailPersistenceService.save(message, flushOnSave, true)
}
}

if (!savedMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class AsynchronousMailMessageBuilder {
private AsynchronousMailMessage message
private boolean immediately = false
private boolean immediatelySetted = false


private boolean useFlushOnSaveSetted = false
private boolean useFlushOnSave = false

private Locale locale

final boolean mimeCapable
Expand Down Expand Up @@ -100,6 +103,11 @@ class AsynchronousMailMessageBuilder {
immediately = value
immediatelySetted = true
}

void flush(boolean value){
useFlushOnSave = value
useFlushOnSaveSetted = true
}

// Mark message must be deleted after sent
void delete(Object value) {
Expand Down Expand Up @@ -437,4 +445,11 @@ class AsynchronousMailMessageBuilder {
boolean getImmediatelySetted() {
return immediatelySetted
}
boolean getUseFlushOnSave() {
return useFlushOnSave
}

boolean getUseFlushOnSaveSetted() {
return useFlushOnSaveSetted
}
}