<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>42 &#187; crontab</title>
	<atom:link href="http://www.42.mach7x.com/tag/crontab/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.42.mach7x.com</link>
	<description>Thoughts and tips about programming with Ruby on Rails</description>
	<lastBuildDate>Wed, 01 Feb 2012 14:38:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Running script/runner in production environment</title>
		<link>http://www.42.mach7x.com/2008/09/30/running-scriptrunner-in-production-environment/</link>
		<comments>http://www.42.mach7x.com/2008/09/30/running-scriptrunner-in-production-environment/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 11:47:56 +0000</pubDate>
		<dc:creator>kosmas</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[email scheduling]]></category>
		<category><![CDATA[production]]></category>
		<category><![CDATA[script/runner]]></category>

		<guid isPermaLink="false">http://www.42.mach7x.com/?p=47</guid>
		<description><![CDATA[Problem Following from a previous post about email scheduling with runner and cron, it turns out that the runner default behaviour is to run in the development environment. Solution Although by reading the help for the script/runner, there is a suggestion to run it with the -e production added to the end, it doesn&#8217;t seem [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
Following from a previous post <a href="http://www.42.mach7x.com/2008/06/11/ruby-on-rails-email-scheduling-using-runner-and-cron/">about email scheduling with runner and cron</a>, it turns out that the runner default behaviour is to run in the development environment.</p>
<p><strong>Solution</strong><br />
Although by reading the help for the script/runner, there is a suggestion to run it with the <quote>-e production</quote> added to the end, it doesn&#8217;t seem to be working.</p>
<p>The solution to make it running in the production environment was to delete the first line (shebang) from step 3 on this <a href="http://www.42.mach7x.com/2008/06/11/ruby-on-rails-email-scheduling-using-runner-and-cron/">post</a></p>
<pre class="code">#!/usr/bin/env /path_to_your_app/script/runner</pre>
<p>and then use the following in the cron setup:</p>
<pre class="code">RAILS_ENV=production /path/to/your_ror_project/script/runner /path/to/your_ror_project/lib/email_scheduler.rb</pre>
<p>Have a look on paragraph <b>Alternative Usage</b> <a href="http://wiki.rubyonrails.org/rails/pages/RunnerScript">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.42.mach7x.com/2008/09/30/running-scriptrunner-in-production-environment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails email scheduling using runner and cron</title>
		<link>http://www.42.mach7x.com/2008/06/11/ruby-on-rails-email-scheduling-using-runner-and-cron/</link>
		<comments>http://www.42.mach7x.com/2008/06/11/ruby-on-rails-email-scheduling-using-runner-and-cron/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 13:50:01 +0000</pubDate>
		<dc:creator>kosmas</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[email scheduling]]></category>
		<category><![CDATA[observer]]></category>

		<guid isPermaLink="false">http://www.42.mach7x.com/?p=37</guid>
		<description><![CDATA[Problem You want to send emails from a Ruby on Rails application, when there is a specific condition on a database table. If the database table gets modified by another application outside Rails you cannot use an observer model. Solution We already assume that: You are using a database You have a model named voicemail [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
You want to send emails from a Ruby on Rails application, when there is a specific condition on a database table. If the database table gets modified by another application outside Rails you cannot use an observer model.</p>
<p><strong>Solution</strong><br />
We already assume that:</p>
<ul>
<li>You are using a database</li>
<li>You have a model named voicemail (id, number_id, audio, created_at, updated_at)</li>
<li>You have a model named number (id, voicemail_email_set, voicemail_email, &#8230;.)</li>
<li>A mail server to use (smtp in our case)</li>
<li>Another application (voice application) populates the voicemail table but with empty updated_at values</li>
</ul>
<p>So the steps we have to follow are:</p>
<ol>
<li>Change the settings in your config/environment.rb file to use the settings for your mail server, and make sure you restart your application after the changes:
<pre class="code">ActionMailer::Base.smtp_settings = {
  :address        => "yourmailserver.com",
  :port           =>  25,
  :domain         => "your.domain.com",
  :authentication => :login,
  :user_name      => "your_smtp_username",
  :password       => "your_smtp_password",
  :raise_delivery_errors  => true}</pre>
</li>
<li>Create your mailer model (ie voicemail_mailer.rb), in app/models:
<pre class="code">class VoicemailMailer < ActionMailer::Base
   # We need the open-uri to be able to open url *** if the file to attach is in an http location ***
  require 'open-uri'

  def sent(email_to,email_from,email_subject,email_body,voicemail_to_send)
    # Check to see if we have a file for the email body message
    @subject    = email_subject
    @body       = email_body
    @recipients = email_to
    @from       = email_from
    @sent_on    = Time.now

    # Split the file in directory and filename
    file_path = File.split(voicemail_to_send)
    file_dir  = file_path[0]
    file_name = file_path[1]

    # Get the file
    tmp_file = open(voicemail_to_send).read

    part( :content_type => "application/wav",
          :disposition => "attachment; filename=#{file_name}",
          :transfer_encoding => "base64") do |attachment|
            attachment.body = tmp_file
    end
  end
end
</pre>
</li>
<li>Create your email scheduler in file lib/email_scheduler.rb:
<pre class="code">#!/usr/bin/env /path_to_your_app/script/runner

# get all the voicemails that have not been sent yet
voicemails_to_email = VoiceMail.find(:all, :conditions => 'updated_at is null')

# For all the voicemails we have, send them and update the field date_sent
for vm2email in voicemails_to_email do
  # Get the number for the voicemail
  number = Number.find(vm2email.number_id)

  # check to see if the send to email is set for the number
  if number.voicemail_email_set
    # Get number details (email_to,email_from etc)
    email_to          = number.voicemail_email
    voicemail_to_send = vm2email.audio
    # Set other details
    email_from      = 'Service@yourdomain.com'
    email_subject   = 'Please find attached your voicemail message'
    email_body      = "Received on: #{Time.now} \n for number: #{number.phone_no}"

    # Now send the email
    VoicemailMailer.deliver_sent(email_to,email_from,email_subject,email_body,voicemail_to_send)

    # And update the record's date_sent field
    vm2email.updated_at = Time.now
    vm2email.save
  end
end
</li>
<li>Create a task in your crontab that runs the scheduler (every five minutes):
<pre class="code">0,5,10,15,20,25,30,35,40,45,50,55 * * * * path_to_your_ror_app/lib/email_scheduler.rb</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.42.mach7x.com/2008/06/11/ruby-on-rails-email-scheduling-using-runner-and-cron/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

