-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for AWS SES as mail sending provider
This commits adds the support for AWS Simple Email Service as an alternative transport for sending notification emails from nextflow. The email is sent via AWS SES when using the nf-amazon plugin, and: - the NXF_ENABLE_AWS_SES=true environment variable is set - or, not `mail.smtp` setting is provided and the AWS_REGION or AWS_DEFAULT_REGION is set in the launching environment Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
1daebee
commit df85d44
Showing
12 changed files
with
369 additions
and
88 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
modules/nextflow/src/main/groovy/nextflow/mail/BaseMailProvider.groovy
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,69 @@ | ||
/* | ||
* Copyright 2020-2023, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package nextflow.mail | ||
|
||
import javax.mail.MessagingException | ||
import javax.mail.internet.MimeMessage | ||
|
||
import groovy.transform.CompileStatic | ||
import nextflow.util.Duration | ||
|
||
/** | ||
* Base class or sending an email via sys command `mail` or `sendmail` | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@CompileStatic | ||
abstract class BaseMailProvider implements MailProvider { | ||
|
||
private long SEND_MAIL_TIMEOUT = 15_000 | ||
|
||
/** | ||
* Send a email message by using system tool such as `sendmail` or `mail` | ||
* | ||
* @param message A {@link MimeMessage} object representing the email to send | ||
*/ | ||
void send(MimeMessage message, Mailer mailer) { | ||
final cmd = [name(), '-t'] | ||
final proc = new ProcessBuilder() | ||
.command(cmd) | ||
.redirectErrorStream(true) | ||
.start() | ||
// pipe the message to the sendmail stdin | ||
final stdout = new StringBuilder() | ||
final stdin = proc.getOutputStream() | ||
message.writeTo(stdin); | ||
stdin.close() // <-- don't forget otherwise it hangs | ||
// wait for the sending to complete | ||
final consumer = proc.consumeProcessOutputStream(stdout) | ||
proc.waitForOrKill(sendTimeout(mailer)) | ||
def status = proc.exitValue() | ||
if( status != 0 ) { | ||
consumer.join() | ||
throw new MessagingException("Unable to send mail message\n $mailer exit status: $status\n reported error: $stdout") | ||
} | ||
} | ||
|
||
private long sendTimeout(Mailer mailer) { | ||
final timeout = mailer.config.sendMailTimeout as Duration | ||
return timeout ? timeout.toMillis() : SEND_MAIL_TIMEOUT | ||
} | ||
|
||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
modules/nextflow/src/main/groovy/nextflow/mail/JavaMailProvider.groovy
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,58 @@ | ||
/* | ||
* Copyright 2020-2023, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package nextflow.mail | ||
|
||
import javax.mail.internet.MimeMessage | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Slf4j | ||
|
||
/** | ||
* Implements a send mail provider based on Java Mail API | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@Slf4j | ||
@CompileStatic | ||
class JavaMailProvider implements MailProvider { | ||
@Override | ||
void send(MimeMessage message, Mailer mailer) { | ||
if( !message.getAllRecipients() ) | ||
throw new IllegalArgumentException("Missing mail message recipient") | ||
|
||
final transport = mailer.getSession().getTransport() | ||
transport.connect(mailer.host, mailer.port as int, mailer.user, mailer.password) | ||
log.trace("Connected to host=$mailer.host port=$mailer.port") | ||
try { | ||
transport.sendMessage(message, message.getAllRecipients()) | ||
} | ||
finally { | ||
transport.close() | ||
} | ||
} | ||
|
||
@Override | ||
String name() { | ||
return 'javamail' | ||
} | ||
|
||
@Override | ||
boolean textOnly() { | ||
return false | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
modules/nextflow/src/main/groovy/nextflow/mail/MailProvider.groovy
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,37 @@ | ||
/* | ||
* Copyright 2020-2023, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package nextflow.mail | ||
|
||
import javax.mail.internet.MimeMessage | ||
|
||
import org.pf4j.ExtensionPoint | ||
|
||
/** | ||
* Define a generic interface to send an email modelled as a Mime message object | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
interface MailProvider extends ExtensionPoint { | ||
|
||
void send(MimeMessage message, Mailer mailer) | ||
|
||
String name() | ||
|
||
boolean textOnly() | ||
|
||
} |
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
36 changes: 36 additions & 0 deletions
36
modules/nextflow/src/main/groovy/nextflow/mail/SendMailProvider.groovy
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 @@ | ||
/* | ||
* Copyright 2020-2023, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package nextflow.mail | ||
|
||
/** | ||
* Send a mail using the `sendmail` sys tool | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
class SendMailProvider extends BaseMailProvider { | ||
|
||
@Override | ||
String name() { | ||
return 'sendmail' | ||
} | ||
|
||
@Override | ||
boolean textOnly() { | ||
return false | ||
} | ||
} |
Oops, something went wrong.