-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatom.xml
More file actions
118 lines (97 loc) · 17.9 KB
/
atom.xml
File metadata and controls
118 lines (97 loc) · 17.9 KB
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Matt Atkinson]]></title>
<subtitle><![CDATA[A developers blog]]></subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://mattpker.com//"/>
<updated>2015-08-13T20:16:57.000Z</updated>
<id>http://mattpker.com//</id>
<author>
<name><![CDATA[Matt Atkinson]]></name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title><![CDATA[How to schedule jobs in your Node.js service]]></title>
<link href="http://mattpker.com/2015/08/07/How-to-schedule-jobs-in-NodeJS/"/>
<id>http://mattpker.com/2015/08/07/How-to-schedule-jobs-in-NodeJS/</id>
<published>2015-08-07T23:12:44.000Z</published>
<updated>2015-08-13T20:16:57.000Z</updated>
<content type="html"><![CDATA[<p>When dealing with large applications there are a lot of cases where you need some sort of scheduled task or cron job. Modifying these and redeploying them can be a pain as they are separate from your service/code. In addition, making sure the correct version of your software and the cron jobs are running on the right servers can add headache to troubleshooting.</p>
<p>Since your NodeJS service is an always running process, and you have things like setTimeout and setInterval at your disposal, setting these up in your NodeJS service can solve all these problems. Additionally, since you already have all your models set up, doing cleanup tasks on your databases or caches are a breeze.</p>
<p>While developing these tasks, I realized that if and when I cluster these services, I will run into an issue where multiple tasks will run at the same time, and this was not a good thing. I searched for a while for a solution but I could not find anything, so I decided to write it myself.</p>
<a id="more"></a>
<h2 id="is-master">is-master</h2><p><a href="https://www.npmjs.com/package/is-master" target="_blank" rel="external">https://www.npmjs.com/package/is-master</a><br><a href="https://github.com/mattpker/node-is-master" target="_blank" rel="external">https://github.com/mattpker/node-is-master</a></p>
<p>is-master uses an existing mongoose singleton as its “cache”, since most projects I work on already have this running. It is very simple to use; all you need to do is require the module, start the worker process, and then you can check if the process is the master.</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">'../node_modules/mongoose'</span>);</span><br><span class="line"><span class="keyword">var</span> im = <span class="built_in">require</span>(<span class="string">'../is-master.js'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Start the mongoose db connection</span></span><br><span class="line">mongoose.connect(<span class="string">'mongodb://127.0.0.1:27017/im'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">err</span>) </span>{</span><br><span class="line"> <span class="keyword">if</span> (err) {</span><br><span class="line"> <span class="built_in">console</span>.error(<span class="string">'\x1b[31m'</span>, <span class="string">'Could not connect to MongoDB!'</span>);</span><br><span class="line"> <span class="keyword">throw</span> (err);</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="comment">// Start the is-master worker</span></span><br><span class="line">im.start();</span><br><span class="line"></span><br><span class="line"><span class="comment">// Check if this current process is the master</span></span><br><span class="line"><span class="keyword">if</span> (im.master) {</span><br><span class="line"> <span class="comment">// Do stuff here</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>This works by inserting each process into your mongodb database, and then whichever process is the oldest is considered the master. Each entry in the mongodb is set to expire if they are not updated within 2 minutes, this way if a process goes offline a new process will be promoted to master.</p>
<p>To setup scheduled tasks with this, you can use setTimeout or setInterval. I highly recommend using setTimeout, setInterval may seem like an easier choice, but it can cause issues if the previous interval got stuck so then you have 2 scheduled tasks running on top of each other.</p>
<p>Here is an example of using setTimeout to scheduled tasks:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">cronJob</span><span class="params">()</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">done</span> = <span class="title">setTimeout</span><span class="params">(function<span class="params">()</span> {</span><br><span class="line"> cronJob<span class="params">()</span>;</span><br><span class="line"> }, <span class="number">120000</span>)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (im.master) {</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Do your work here</span></span><br><span class="line"></span><br><span class="line"> someCallbackFunction(<span class="function"><span class="keyword">function</span><span class="params">(err, results)</span> </span>{</span><br><span class="line"> <span class="comment">// Work with your callback database</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// When you are done make sure to call done</span></span><br><span class="line"> done();</span><br><span class="line"> })</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> done();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">im.on(<span class="string">'connected'</span>, cronjob);</span><br></pre></td></tr></table></figure>
<p>In the example above, the cronJob function will run when is-master emits that is has connected and is ready. The cronJob function will then check if it is master and if it is you can proceed with doing whatever work is necessary. You just need to make sure to call the done() function in every case that it finishes so that it will run again.</p>
<p>If you are looking for more of a cron syntax and style for setting these up, check out the package <a href="https://www.npmjs.com/package/cron" target="_blank" rel="external">https://www.npmjs.com/package/cron</a>. Here is another example using that package:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> CronJob = <span class="built_in">require</span>(<span class="string">'cron'</span>).CronJob;</span><br><span class="line"><span class="keyword">var</span> job = <span class="keyword">new</span> CronJob(<span class="string">'0 * * * * *'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</span><br><span class="line"> <span class="keyword">if</span> (im.master) {</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Do your work here</span></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line">}, <span class="literal">null</span>, <span class="literal">false</span>, <span class="string">"America/Los_Angeles"</span>);</span><br><span class="line"></span><br><span class="line">im.on(<span class="string">'connected'</span>, job.start);</span><br></pre></td></tr></table></figure>
<p>I hope this helps, let me know in the comments if you have any issues or suggestions.</p>
]]></content>
<summary type="html">
<![CDATA[<p>When dealing with large applications there are a lot of cases where you need some sort of scheduled task or cron job. Modifying these and redeploying them can be a pain as they are separate from your service/code. In addition, making sure the correct version of your software and the cron jobs are running on the right servers can add headache to troubleshooting.</p>
<p>Since your NodeJS service is an always running process, and you have things like setTimeout and setInterval at your disposal, setting these up in your NodeJS service can solve all these problems. Additionally, since you already have all your models set up, doing cleanup tasks on your databases or caches are a breeze.</p>
<p>While developing these tasks, I realized that if and when I cluster these services, I will run into an issue where multiple tasks will run at the same time, and this was not a good thing. I searched for a while for a solution but I could not find anything, so I decided to write it myself.</p>]]>
</summary>
<category term="Node.js" scheme="http://mattpker.com/tags/Node-js/"/>
<category term="node" scheme="http://mattpker.com/tags/node/"/>
</entry>
<entry>
<title><![CDATA[My Editor: Atom]]></title>
<link href="http://mattpker.com/2015/07/06/My-Editor-Atom/"/>
<id>http://mattpker.com/2015/07/06/My-Editor-Atom/</id>
<published>2015-07-06T18:04:10.000Z</published>
<updated>2015-08-13T20:17:44.000Z</updated>
<content type="html"><![CDATA[<center><img src="/images/atom.png" style="border:0px"></center>
<p>Coming from a background in Linux server administration, of course my first editor was Vim. I really like Vim as a basic editor, but when I started programing it really did not fit my needs. I realize that you can highly customize Vim and make it much like “modern” editors of today, this just seemed way to complex for me and I wanted something a bit more user friendly with mouse support.</p>
<p>The editor everyone recommended to me was <a href="http://www.sublimetext.com/" target="_blank" rel="external">Sublime 2</a>. I enjoyed the look and feel of Sublime, but it still did not seem as user friendly that I hoped. Even so, I decided to go ahead with it as it seemed to be the best option at that time.</p>
<p>I used Sublime 2 as my primary editor for about 2 years. I had it very customized to my liking and I was very confortable with it. So changing editors at this point was not even on my radar.</p>
<p>About 6 months ago I stumbled across <a href="https://atom.io/" target="_blank" rel="external">Atom</a>, an editor still in beta from GitHub. It was promising an awesome packaging system, theme support, GitHub support baked in, and powered all by NodeJS and web technologies. At the time I was really getting into NodeJS and decided that I wanted to give it a try to see what it was capable of.</p>
<a id="more"></a>
<p><img src="/images/atom2.png" alt="Atom Screenshot" title="Atom Screenshot"></p>
<p>Right out of the box Atom blew me away with an amazing packaging system. Within minutes I found a theme and syntax that I liked and all the comparable plugins I was using in Sublime 2. I felt at home with Atom as it was much like Sublime 2 and most the keyboard shortcuts were the same. It wasn’t till I started coding in an actual Git project that I realized I would not be going back to Sublime 2. It had built in Git support that I had never seen before, such as lines were colored that had been changed/added/deleted and the same thing with new/changed files/folders in the tree view.</p>
<p>Atom recently released <a href="http://blog.atom.io/2015/06/25/atom-1-0.html" target="_blank" rel="external">version 1.0</a> and at this point I would recommend it to everyone. It is extremely stable and they have greatly improved the startup time and speed. I am excited to see what they do next with Atom, hopefully some more great baked in GitHub features and solving the problem of opening very large files (200MB+).</p>
<h2 id="Recommended_Plugins">Recommended Plugins</h2><ul>
<li><a href="https://atom.io/packages/atom-beautify" target="_blank" rel="external">atom-beautify</a></li>
<li><a href="https://atom.io/packages/color-picker" target="_blank" rel="external">color-picker</a></li>
<li><a href="https://atom.io/packages/pigments" target="_blank" rel="external">pigments</a></li>
<li><a href="https://atom.io/packages/file-icons" target="_blank" rel="external">file-icons</a></li>
<li><a href="https://atom.io/packages/linter" target="_blank" rel="external">linter</a> - List of compatible linters <a href="http://atomlinter.github.io/" target="_blank" rel="external">http://atomlinter.github.io/</a></li>
<li><a href="https://atom.io/packages/merge-conflicts" target="_blank" rel="external">merge-conflicts</a></li>
</ul>
<h2 id="Recommended_Theme">Recommended Theme</h2><ul>
<li><a href="https://atom.io/themes/isotope-ui" target="_blank" rel="external">Isotope UI</a></li>
<li><a href="https://atom.io/themes/monokai" target="_blank" rel="external">Monokai Syntax</a></li>
</ul>
]]></content>
<summary type="html">
<![CDATA[<center><img src="/images/atom.png" style="border:0px"/></center>
<p>Coming from a background in Linux server administration, of course my first editor was Vim. I really like Vim as a basic editor, but when I started programing it really did not fit my needs. I realize that you can highly customize Vim and make it much like “modern” editors of today, this just seemed way to complex for me and I wanted something a bit more user friendly with mouse support.</p>
<p>The editor everyone recommended to me was <a href="http://www.sublimetext.com/">Sublime 2</a>. I enjoyed the look and feel of Sublime, but it still did not seem as user friendly that I hoped. Even so, I decided to go ahead with it as it seemed to be the best option at that time.</p>
<p>I used Sublime 2 as my primary editor for about 2 years. I had it very customized to my liking and I was very confortable with it. So changing editors at this point was not even on my radar.</p>
<p>About 6 months ago I stumbled across <a href="https://atom.io/">Atom</a>, an editor still in beta from GitHub. It was promising an awesome packaging system, theme support, GitHub support baked in, and powered all by NodeJS and web technologies. At the time I was really getting into NodeJS and decided that I wanted to give it a try to see what it was capable of.</p>]]>
</summary>
<category term="atom" scheme="http://mattpker.com/tags/atom/"/>
<category term="editors" scheme="http://mattpker.com/tags/editors/"/>
<category term="reviews" scheme="http://mattpker.com/tags/reviews/"/>
</entry>
<entry>
<title><![CDATA[My Blog]]></title>
<link href="http://mattpker.com/2015/07/06/My-Blog/"/>
<id>http://mattpker.com/2015/07/06/My-Blog/</id>
<published>2015-07-06T17:17:13.000Z</published>
<updated>2015-07-06T23:06:09.000Z</updated>
<content type="html"><![CDATA[<p>I am starting a blog based on what I am currently doing in development and technology. I plan on having a new post at least every week. Head on over to the <a href="/about">about</a> page for more information about me.</p>
<p>This blog is being created with <a href="https://hexo.io/" target="_blank" rel="external">Hexo</a> and hosting on <a href="https://pages.github.com/" target="_blank" rel="external">GitHub Pages</a>. I wanted to use a blogging framework that used the same technologies that I am currently working with. When I get more well versed in Hexo, ill make a post reviewing it and even possibly a tutorial.</p>
<p>I am welcome to any feedback, hit me up on my twitter <a href="https://twitter.com/mattpker" target="_blank" rel="external">@mattpker</a> or use the commenting system.</p>
]]></content>
<summary type="html">
<![CDATA[<p>I am starting a blog based on what I am currently doing in development and technology. I plan on having a new post at least every week. H]]>
</summary>
<category term="hexo" scheme="http://mattpker.com/tags/hexo/"/>
</entry>
</feed>