webdevRefinery Forum: Attempt: Retry your functions - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 11 June 2012 - 11:01 AM (#1)

Attempt: Retry your functions


My first NPM-published lib!
So I made a quick library called Attempt because I am friggin sick of webservice APIs that fail for no good reason (cough loggly cough). Here's what it looks like:

Instead of this...
logToLoggly("My log message", function(err) {
    if (err) console.log("IT FAILED AGAIN DAMNIT");
    else console.log("Holy crap it worked for once.");
});


Just wrap it in an Attempt, and use 'this' for the callback!
var attempt = require('attempt');
attempt(function() {
    logToLoggly("My log message", this);
}, function(err) {
    if (err) console.log("logToLoggly() failed after 5 attempts.");
    else console.log("Success!");
});


It's way more customizable, but if you want the details, check the readme :)

Attempt on GitHub

Hope this is useful for more people than just me ;-)
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 11 June 2012 - 11:17 AM (#2)

Looks helpful! How about an "attempts" parameter?just read docs…
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 11 June 2012 - 11:24 AM (#3)

View Postcallumacrae, on 11 June 2012 - 11:17 AM, said:

Looks helpful! How about an "attempts" parameter?just read docs…

xD One thing I don't call out in the docs is that this is namespaced and usable in the browser, too. Haven't tested that yet, though.
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 13 June 2012 - 09:55 AM (#4)

Really interesting, I'm using it in my next project to retry MySQL queries if they happen to fail :)
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Koen 

  • Leroy Jenkins
  • Group: Members
  • Posts: 2503
  • Joined: 10-March 10
  • Locationthe Netherlands
  • Expertise:HTML,CSS,Javascript,Graphics

Posted 13 June 2012 - 05:53 PM (#5)

View PostFike, on 13 June 2012 - 09:55 AM, said:

Really interesting, I'm using it in my next project to retry MySQL queries if they happen to fail :)

Are you sure that's a good idea? Hitting your database server multiple times with the same query might not be too clever. Why would the query work the second (etc.) time?
Please click the + if I helped you!
Twitter: @KoenKlaren

<callumacrae> YOU DID A ROMNEY
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 14 June 2012 - 12:10 PM (#6)

View PostKoen, on 13 June 2012 - 05:53 PM, said:

Are you sure that's a good idea? Hitting your database server multiple times with the same query might not be too clever. Why would the query work the second (etc.) time?

It's handy for remote servers, especially in cases where the query isn't the thing that's likely to be failing. Everything from maxConnections being hit to DNS not responding before the timeout could benefit from a simply retry. With that said, though, I'd definitely set retries to 1 or maybe two for an SQL query.

I have attempt() wrapped around my JsonRPC client, every call that I make to DynamoDB (It's reliable, but prematurely closed connections have happened -- AND I can have onError refresh my auth token), and every call I make to a remote logging service -- but so far, since my remote MySQL server DNS resolves at the first router, I haven't really had a need to put it there yet. I could see it helping in a couple rare situations though :)
1


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 19 June 2012 - 06:11 AM (#7)

I love that .gitignore file.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 19 June 2012 - 06:49 AM (#8)

Kyek, lrn2globalgitignore
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 19 June 2012 - 07:26 AM (#9)

View PostQuinn, on 19 June 2012 - 06:11 AM, said:

I love that .gitignore file.

lol, what about it?

View Postcallumacrae, on 19 June 2012 - 06:49 AM, said:

Kyek, lrn2globalgitignore

Noooo, that's a silly idea. Globals are convenient, sure, but you can't enforce your global gitignore on everyone who forks and maybe commits to the project. If you check it in, you potentially save yourself some annoyances later. Plus, while Attempt is a fairly simple lib, there are very very few projects I've worked on where I didn't have some project-specific stuff to throw in there :)
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 19 June 2012 - 07:28 AM (#10)

View PostKyek, on 19 June 2012 - 07:26 AM, said:

Noooo, that's a silly idea. Globals are convenient, sure, but you can't enforce your global gitignore on everyone who forks and maybe commits to the project. If you check it in, you potentially save yourself some annoyances later. Plus, while Attempt is a fairly simple lib, there are very very few projects I've worked on where I didn't have some project-specific stuff to throw in there :)

You're doing it wrong!
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 19 June 2012 - 08:05 AM (#11)

View Postcallumacrae, on 19 June 2012 - 07:28 AM, said:

You're doing it wrong!

Your mom is doing it wrong!
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 19 June 2012 - 08:22 AM (#12)

View PostKyek, on 19 June 2012 - 08:05 AM, said:

Your mom is doing it wrong!


Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 19 June 2012 - 03:25 PM (#13)

View PostKyek, on 19 June 2012 - 07:26 AM, said:

lol, what about it?

Shouldn't you have git ignore that file?
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is offline gibbonweb 

  • 兄ヨハネス
  • Group: Members
  • Posts: 2062
  • Joined: 23-June 10
  • LocationMunich(DE)
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL,Graphics

Posted 19 June 2012 - 04:23 PM (#14)

View PostQuinn, on 19 June 2012 - 03:25 PM, said:

Shouldn't you have git ignore that file?

If git ignored the .gitignore file, how could it ignore the files the .gitignore file tells it to ignore?
Some deep shit right here.
0


User is offline Quinn 

  • More pew-pew, less QQ
  • Group: Members
  • Posts: 1307
  • Joined: 08-March 10
  • LocationPalmyra, PA, USA
  • Expertise:HTML,PHP,Javascript

Posted 19 June 2012 - 05:04 PM (#15)

View Postgibbonweb, on 19 June 2012 - 04:23 PM, said:

If git ignored the .gitignore file, how could it ignore the files the .gitignore file tells it to ignore?
Some deep shit right here.

I don't really know much about Git, but it seems like it would make sense to read the file, ignore whatever the file says to ignore, and then ignore the file automatically.

Else just put .gitignore at the end.
<Imp> [F3ar 40]  [PWNbear 17]  [magik 15]  [dissident 10]  [mark 7]

View PostKyek, on 07 February 2011 - 07:11 AM, said:

Though anyone who thinks Europe is a country should be smacked in the face. By a train.
0


User is online Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5078
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 19 June 2012 - 06:26 PM (#16)

That defeats the purpose, though. I *want* it in the repo. That way, others check it out when they fork, and they don't pollute the repo accidentally. If you take a gander through some of the more trafficked repos, the grand majority of them have a .gitignore. It's a very handy way to prevent really annoying mistakes.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services