<commit_before># ~ GG

{argv}    = require 'optimist'
{CronJob} = require 'cron'
Bongo     = require 'bongo'
Broker    = require 'broker'
htmlify   = require 'koding-htmlify'

Emailer   = require '../social/lib/social/emailer'
template  = require './templates'

{mq, mongo, emailWorker, uri} = \
  require('koding-config-manager').load("main.#{argv.c}")

if 'string' is typeof mongo
  mongo = "mongodb://#{mongo}?auto_reconnect"

broker = new Broker mq

worker = new Bongo {
  mongo
  mq     : broker
  root   : __dirname
  models : ['../social/lib/social/models/email.coffee']
}

log = ->
  console.log "[E-MAIL SENDER]", arguments...

log "E-Mail Sender Worker has started with PID #{process.pid}"

sendEmail = (emailContent)->
  {from, replyto, email, subject, content, unsubscribeId, bcc} = emailContent

  To       = emailWorker.forcedRecipient or email
  From     = if from is '<EMAIL>' then "Koding <#{from}>" else from
  HtmlBody = template.htmlTemplate htmlify(content, linkStyle:template.linkStyle), unsubscribeId, email
  TextBody = template.textTemplate content, unsubscribeId, email

  Emailer.send {
    From
    To
    Subject   : subject or "Notification"
    HtmlBody
    TextBody
    ReplyTo   : replyto
    Bcc       : bcc
  }, (err, { messageId: smtpId })->
    dateAttempted = new Date()
    status        = 'attempted'
    unless err then log "An e-mail sent to #{To}"
    else
      log "An error occured: #{err}"
      status = 'failed'

    emailContent.update $set: {status, dateAttempted, smtpId}, (err)->
      console.error err if err

emailSender = ->
  {JMail} = worker.models
  JMail.fetchWithUnsubscribedInfo {status: "queued"}, {limit:300}, (err, emails)->
    if err
      log "Could not load email queue!"
    else
      if emails.length > 0
        currentIds = [email._id for email in emails][0]
        JMail.update {_id: $in: currentIds}, {$set: status: 'sending'}, \
                     {multi: yes}, (err)->
          unless err
            log "Sending #{emails.length} e-mail(s)..."

            sendEmail email  for email in emails
          else
            log err

emailSenderCron = new CronJob emailWorker.cronInstant, emailSender<commit_msg>Emailsender: Make sure there is a response from Emailer<commit_after># ~ GG

{argv}    = require 'optimist'
{CronJob} = require 'cron'
Bongo     = require 'bongo'
Broker    = require 'broker'
htmlify   = require 'koding-htmlify'

Emailer   = require '../social/lib/social/emailer'
template  = require './templates'

{mq, mongo, emailWorker, uri} = \
  require('koding-config-manager').load("main.#{argv.c}")

if 'string' is typeof mongo
  mongo = "mongodb://#{mongo}?auto_reconnect"

broker = new Broker mq

worker = new Bongo {
  mongo
  mq     : broker
  root   : __dirname
  models : ['../social/lib/social/models/email.coffee']
}

log = ->
  console.log "[E-MAIL SENDER]", arguments...

log "E-Mail Sender Worker has started with PID #{process.pid}"

sendEmail = (emailContent)->
  {from, replyto, email, subject, content, unsubscribeId, bcc} = emailContent

  To       = emailWorker.forcedRecipient or email
  From     = if from is '<EMAIL>' then "Koding <#{from}>" else from
  HtmlBody = template.htmlTemplate htmlify(content, linkStyle:template.linkStyle), unsubscribeId, email
  TextBody = template.textTemplate content, unsubscribeId, email

  Emailer.send {
    From
    To
    Subject   : subject or "Notification"
    HtmlBody
    TextBody
    ReplyTo   : replyto
    Bcc       : bcc
  }, (err, res)->

    dateAttempted = new Date()

    if err
      console.warn "An error occured: #{err}"
      status = 'failed'
      smtpId = null
    else
      { smtpId } = res?.messageId
      log "An e-mail sent to #{To}"
      status = 'attempted'

    emailContent.update $set: {status, dateAttempted, smtpId}, (err)->
      console.error err if err

emailSender = ->
  {JMail} = worker.models
  JMail.fetchWithUnsubscribedInfo {status: "queued"}, {limit:300}, (err, emails)->
    if err
      log "Could not load email queue!"
    else
      if emails.length > 0
        currentIds = [email._id for email in emails][0]
        JMail.update {_id: $in: currentIds}, {$set: status: 'sending'}, \
                     {multi: yes}, (err)->
          unless err
            log "Sending #{emails.length} e-mail(s)..."

            sendEmail email  for email in emails
          else
            log err

emailSenderCron = new CronJob emailWorker.cronInstant, emailSender
{'fraction_non_alphanumeric': 0.12564389697648376, 'fraction_numerical': 0.0022396416573348264, 'mean_word_length': 2.759259259259259, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4603422', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1426, 'n_words': 415}