Email cron job, to install contab job: rake orbit_cron:install

This commit is contained in:
manson 2014-05-22 16:08:32 +08:00
parent dda3a78c81
commit 789003b88b
7 changed files with 60 additions and 26 deletions

View File

@ -61,8 +61,9 @@ class OrbitMailer < ActionMailer::Base
})
mail_log.save
# email.destroy
email.is_sent = true
email.save
end

View File

@ -11,6 +11,8 @@ class Email
field :mail_sentdate , :type => DateTime, :default => Time.now
field :mail_lang, :default => I18n.locale
field :is_sent, :type => Boolean, :default => false
field :template # Path to template file
field :template_data # Data to render template
@ -19,14 +21,20 @@ class Email
has_many :email_files, :autosave => true, :dependent => :destroy
scope :can_deliver, ->{ where(:mail_sentdate.lte => Time.now) }
scope :can_deliver, ->{ where(:mail_sentdate.lte => Time.now, :is_sent=>false) }
def deliver
OrbitMailer.set_mail(self).deliver
end
def self.deliver_all
Email.can_deliver.each do |email|
OrbitMailer.set_mail(email).deliver
end
end
def module_app=(app)
self.module_app_key = app.key
self.module_app_key = app.key rescue nil
end
def module_app
@ -34,7 +42,7 @@ class Email
end
def create_user=(user)
self.create_user_id = user.id
self.create_user_id = user.id rescue nil
end
def create_user
@ -42,7 +50,7 @@ class Email
end
def update_user=(user)
self.update_user_id = user.id
self.update_user_id = user.id rescue nil
end
def update_user

View File

@ -9,7 +9,7 @@ class EmailLog
field :mailer_count
def module_app=(app)
self.module_app_key = app.key
self.module_app_key = app.key rescue nil
end
def module_app
@ -17,7 +17,7 @@ class EmailLog
end
def mail_user=(user)
self.mail_user_id = user.id
self.mail_user_id = user.id rescue nil
end
def mail_user

View File

@ -6,15 +6,6 @@
<% end -%>
</td>
<td><%= format_value(mail_cron.mail_sentdate) %></td>
<td><%= mail_cron.mail_subject %>
<div class="quick-edit">
<ul class="nav nav-pills">
<%#= content_tag(:li, link_to(t(:detail), admin_mail_cron_path(mail_cron),:target => '_blank')) if at_least_module_manager %>
<%#if at_least_module_manager %>
<li><%#= link_to t(:delete_), admin_mail_cron_path(mail_cron), :class=>"text-error", :confirm => t('sure?'), :method => :delete, :remote => true %></li>
<%# end -%>
</ul>
</div>
</td>
<td><%= mail_cron.mail_subject %></td>
<td><%= mail_cron.module_app.key %></td>
</tr>

View File

@ -6,14 +6,7 @@
<% end -%>
</td>
<td><%= format_value(mail_cron_log.created_at) %></td>
<td><%= mail_cron_log.mail_subject %>
<div class="quick-edit">
<ul class="nav nav-pills">
<%#= content_tag(:li, link_to(t(:detail), admin_mail_cron_log_path(mail_cron_log),:target => '_blank')) if at_least_module_manager %>
<%#= content_tag(:li, link_to(t(:delete_),admin_mail_cron_log_path(mail_cron_log), :confirm => t(:sure?), :method => :delete, :class=>"text-error", :remote => true)) if at_least_module_manager %>
</ul>
</div>
</td>
<td><%= mail_cron_log.mail_subject %></td>
<td><%= mail_cron_log.mail_user.user_name %> </td>
<td><%= mail_cron_log.module_app.key %> </td>
</tr>

5
lib/tasks/email.rake Normal file
View File

@ -0,0 +1,5 @@
namespace :email do
task :deliver_all => :environment do
Email.deliver_all
end
end

36
lib/tasks/orbit_cron.rake Normal file
View File

@ -0,0 +1,36 @@
namespace :orbit_cron do
task :install => :environment do
# Setup environment in crontab
cron_env ="#!/bin/bash\n"+
"PATH=#{ENV['PATH']}\n"+
"RUBYLIB=#{ENV['RUBYLIB']}\n"+
"GEM_HOME=#{ENV['GEM_HOME']}\n"+
"GEM_PATH=#{ENV['GEM_PATH']}\n"+
"RUBYOPT=#{ENV['RUBYOPT']}\n\n"
# Email cron job
email_cron = "* * * * * cd #{Rails.root.to_s} && bundle exec rake email:deliver_all > /dev/null\n"
File.open('lib/tasks/orbit_cron', "w+") do |f|
f.write(cron_env + email_cron)
end
# Check if current crontab is empty
if %x(crontab -l).eql? ""
%x(crontab #{Rails.root.to_s+'/lib/tasks/orbit_cron'})
%x(rm #{Rails.root.to_s+'/lib/tasks/orbit_cron'})
puts "\n"
puts "Orbit cron jobs installed\n"
else
puts "\n"
puts "============================ Warning! ============================\n"
puts "Crontab is not empty!\n"
puts "Please edit crontab with command 'crontab -e' and paste the following lines:\n"
puts "=================================================================\n"
puts cron_env + email_cron
puts "=================================================================\n"
puts "\n\n"
end
end
end