-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
237 lines (169 loc) · 9.72 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>RESTful Blog</title>
<meta name="author" content="RESTful Labs">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://blog.restful-labs.com/index.html"/>
<link href="/favicon.png" rel="shortcut icon" />
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="http://s3.amazonaws.com/ender-js/jeesh.min.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="RESTful Blog" type="application/atom+xml"/>
<script type="text/javascript">
(function() {
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
script.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
})();
</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
<!--Fonts from Google's Web font directory at http://google.com/webfonts -->
<link href='http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'>
</head>
<body >
<header><hgroup>
<h1><a href="/"><img src="/images/restful_labs.png"></a></h1>
<h2>The official blog of RESTful Labs</h2>
</hgroup>
</header>
<nav role=navigation><ul role=subscription data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="site-search">
<input type="hidden" name="q" value="site:blog.restful-labs.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul role=main-navigation>
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
<li><a href="/about">About</a></li>
<li><a href="http://restful-labs.com">RESTful Labs</a></li>
<li><a href="http://restfulmetrics.com">RESTful Metrics</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2011/08/23/catching-errors-in-heroku-cron-tasks/">Catching Errors in Heroku Cron Tasks</a></h1>
<p class="meta">
<time datetime="2011-08-23 21:10:00 -0400" pubdate updated >Aug 23<span>rd</span>, 2011</time>
</p>
</header>
<div class="entry-content"><p>Once we started integrating <a href="http://www.restfulmetrics.com">RESTful Metrics</a> into our own applications, we quickly ran into some problems with the way we were using the task to invoke other Rake tasks.</p>
<p>Our <code>cron.rake</code> looked something like this:</p>
<figure role=code><div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers"><span class='line'>1</span>
<span class='line'>2</span>
<span class='line'>3</span>
<span class='line'>4</span>
<span class='line'>5</span>
</pre></td><td class='code' width='100%'><pre><code class='ruby'><div class='line'><span class="n">desc</span> <span class="s2">"Heroku Cron Task"</span>
</div><div class='line'><span class="n">task</span> <span class="ss">:cron</span> <span class="o">=></span> <span class="ss">:environment</span> <span class="k">do</span>
</div><div class='line'> <span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="o">[</span><span class="s2">"accounts:bill"</span><span class="o">].</span><span class="n">invoke</span>
</div><div class='line'> <span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="o">[</span><span class="s2">"accounts:expire"</span><span class="o">].</span><span class="n">invoke</span>
</div><div class='line'><span class="k">end</span>
</div></code></pre></td></tr></table></div></figure>
<p>As we needed our cron to do more, we just invoked more tasks from within the cron task.</p>
<p>The problem with this approach is that once you start stacking code in your cron task, there is a chance that one of your tasks in the stack will raise an exception and prevent the remaining tasks from being invoked. Worse, unless you check your cron logs frequently, it’s likely that your developers aren’t aware that these tasks are failing.</p>
<p>In our case, we had created some tasks near the end of the cron stack that sent some data points to RESTful Metrics for aggregation. On the days that our cron script encountered an exception, metrics weren’t collected.</p>
<p>To fix this, we’ve started to wrap each individual task invocation around a begin/rescue/end block and we send the exceptions to <a href="http://airbrakeapp.com">Airbrake</a>. This means that errors generate emails to developers and an error in one task doesn’t prevent the remaining tasks from running. The above cron script was re-written to:</p>
<figure role=code><div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers"><span class='line'>1</span>
<span class='line'>2</span>
<span class='line'>3</span>
<span class='line'>4</span>
<span class='line'>5</span>
<span class='line'>6</span>
<span class='line'>7</span>
<span class='line'>8</span>
<span class='line'>9</span>
<span class='line'>10</span>
<span class='line'>11</span>
<span class='line'>12</span>
<span class='line'>13</span>
<span class='line'>14</span>
</pre></td><td class='code' width='100%'><pre><code class='ruby'><div class='line'><span class="n">desc</span> <span class="s2">"Heroku Cron Task"</span>
</div><div class='line'><span class="n">task</span> <span class="ss">:cron</span> <span class="o">=></span> <span class="ss">:environment</span> <span class="k">do</span>
</div><div class='line'> <span class="k">begin</span>
</div><div class='line'> <span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="o">[</span><span class="s2">"accounts:bill"</span><span class="o">].</span><span class="n">invoke</span>
</div><div class='line'> <span class="k">rescue</span>
</div><div class='line'> <span class="no">HoptoadNotifier</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="vg">$!</span><span class="p">)</span>
</div><div class='line'> <span class="k">end</span>
</div><div class='line'>
</div><div class='line'> <span class="k">begin</span>
</div><div class='line'> <span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="o">[</span><span class="s2">"accounts:expire"</span><span class="o">].</span><span class="n">invoke</span>
</div><div class='line'> <span class="k">rescue</span>
</div><div class='line'> <span class="no">HoptoadNotifier</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="vg">$!</span><span class="p">)</span>
</div><div class='line'> <span class="k">end</span>
</div><div class='line'><span class="k">end</span>
</div></code></pre></td></tr></table></div></figure>
<p>This is a little verbose, but it does the trick for now. Ideally there would be some method that automatically wraps each task invocation in the cron task. This would ensure developers would automatically be protected and the cron task wouldn’t become bloated.</p>
<p>What do you guys do?</p>
</div>
</article>
<nav role="pagination">
<div>
<a href="/blog/archives">Blog Archives</a>
</div>
</nav>
<script type="text/javascript">
var disqus_shortname = 'restfulblog';
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
</div>
<aside role=sidebar>
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/2011/08/23/catching-errors-in-heroku-cron-tasks/">Catching Errors in Heroku Cron Tasks</a>
</li>
</ul>
</section>
<section>
<h1>Latest Tweets</h1>
<ul id="tweets">
<li class="loading">Status updating…</li>
</ul>
<script type="text/javascript">
$.domReady(function(){
getTwitterFeed("RESTfulLabs", 4, false);
});
</script>
<script src="/javascripts/twitter.js" type="text/javascript"> </script>
<a href="http://twitter.com/RESTfulLabs" class="twitter-follow-button" data-width="208px" data-show-count="false">Follow @RESTfulLabs</a>
</section>
</aside>
</div>
</div>
<footer><p>
Copyright © 2011 - RESTful Labs -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
</body>
</html>