Skip to content
Browse files

Using winston and adding a print function as helper

  • Loading branch information...
1 parent 70865f3 commit 09bd8f81bf20b369936bc6ec6f006e6c8567f6de @sreeix sreeix committed
Showing with 47 additions and 53 deletions.
  1. +6 −0 lib/helper.coffee
  2. +14 −14 models/package.coffee
  3. +7 −6 models/package_metadata.coffee
  4. +2 −1 package.json
  5. +9 −14 server.coffee
  6. +9 −18 server.js
View
6 lib/helper.coffee
@@ -0,0 +1,6 @@
+exports.print = (module = "") ->
+ (err, info) ->
+ if err
+ console.log "Error: #{module} : #{err}"
+ else
+ console.log "Complete: #{module} : #{info}"
View
28 models/package.coffee
@@ -139,36 +139,36 @@ exports.watch_updates = () ->
redisClient.set 'current_npm_id', value, redis.print
else
redisPosition = value
- console.log "setting redis current_npm_id to #{redisPosition}"
+ winston.log "setting redis current_npm_id to #{redisPosition}"
packages_db.changes(since: parseInt(redisPosition, 10) , feed: 'continuous').on 'response', (res) ->
res.on 'data', (change) ->
- console.log "New change on #{util.inspect(change)}"
+ winston.log "New change on #{util.inspect(change)}"
packages_db.get change.id, (err, doc) ->
redisClient.incr 'current_npm_id', redis.print
if not err and doc?.keywords
- console.log "updating changes for keywords #{doc.keywords}"
+ winston.log "updating changes for keywords #{doc.keywords}"
exports.updateChanged doc
else
- console.log "Error in getting document for #{change._id} #{util.inspect(err)}" if err
+ winston.log "Error in getting document for #{change._id} #{util.inspect(err)}" if err
exports.updateChanged = (doc) ->
try
keywords = if _.isArray(doc.keywords) then doc.keywords else [doc.keywords]
categories = _.map doc.keywords, (keyword) ->
CategoryMap.from_keyword keyword
- exports.save_categories doc.id, categories, (err, doc) -> console.log "updateChanged:docid-> #{doc._id}"
+ exports.save_categories doc.id, categories, (err, doc) -> winston.log "updateChanged:docid-> #{doc._id}"
catch error
- console.log "updateChanged:Error #{error}"
+ winston.log "updateChanged:Error #{error}"
if doc.repository?.url
- console.log "Repo url -> #{doc.repository.url}"
+ winston.log "Repo url -> #{doc.repository.url}"
regex = /github.com\/(.*)\/(.*)\.git/
match = doc.repository.url.match regex
if match and match[1] and match[2]
PackageMetadata.createOrUpdate id: doc.id, user: match[1], repo: match[2], (err, res) ->
if err
- console.log "createOrUpdateError:error : #{err}"
+ winston.log "createOrUpdateError:error : #{err}"
else
- console.log "createOrUpdateError:response : #{response}"
+ winston.log "createOrUpdateError:response : #{response}"
exports.import_from_npm = (o, callback) ->
couchConfig = conf.couchdb
@@ -178,7 +178,7 @@ exports.import_from_npm = (o, callback) ->
exports.import_from_github = (o, callback) ->
packages_db.view 'repositories/git', _.extend(o, include_docs: true), (err, docs) ->
updateGithubInfo = (view_doc) ->
- PackageMetadata.createOrUpdate id: view_doc.doc['_id'], user: view_doc.value.user, repo: view_doc.value.repo, (err, res) -> console.log( err || res)
+ PackageMetadata.createOrUpdate id: view_doc.doc['_id'], user: view_doc.value.user, repo: view_doc.value.repo, (err, res) -> winston.log( err || res)
count = 0
_.each docs, (view_doc) ->
count = count + 1
@@ -190,7 +190,7 @@ exports.save_categories = (name, category_name, callback) ->
categories = _.flatten [category_name]
metadata_db.get name, (err, metaDoc) ->
if(err)
- console.log "creating new doc for #{name}"
+ winston.log "creating new doc for #{name}"
metadata_db.save name, categories: categories
else
if metaDoc['categories']?
@@ -199,9 +199,9 @@ exports.save_categories = (name, category_name, callback) ->
metaDoc['categories'] = categories
metadata_db.save name, metaDoc['_rev'], metaDoc, (err, res) ->
if err
- console.log "save_categories: error:#{name} #{err}"
+ winston.log "save_categories: error:#{name} #{err}"
else
- console.log "Successfuly saved #{name} : #{res}"
+ winston.log "Successfuly saved #{name} : #{res}"
exports.by_rank = (number_of_items = 10, callback) ->
metadata_db.view 'categories/rank', {limit: number_of_items, descending: true}, (err, docs) ->
@@ -235,7 +235,7 @@ exports.by_category = (category_name, top_count = 10, callback) ->
callback.apply null, [results]
exports.find = (name, callback) ->
- console.log " finding package #{name}"
+ winston.log " finding package #{name}"
metadata_db.get name, (err, doc) ->
if err or not doc
callback err, null
View
13 models/package_metadata.coffee
@@ -5,7 +5,8 @@ async = require 'async'
extensions = require '../lib/extensions'
Conf = require '../conf'
GitHubApi = require("github").GitHubApi
-github = new GitHubApi(true)
+github = new GitHubApi true
+winston = require 'winston'
PackageMetadata = exports = module.exports
metadataDb = new cradle.Connection(Conf.couchdb.host, 5984, auth: Conf.couchdb.auth).database(Conf.couchdb.metadata_database)
@@ -47,21 +48,21 @@ PackageMetadata.rank = (docs, callback) ->
PackageMetadata.createOrUpdate = (opts, callback) ->
if not callback
- callback = (err, res)-> console.log(err || res)
+ callback = (err, res)-> winston.log(err || res)
github.getRepoApi().show opts.user, opts.repo, (err, github_info) ->
if err or !github_info
- console.log "error during fetch of the github info for #{opts.repo}, #{util.inspect(err)}"
+ winston.log "error during fetch of the github info for #{opts.repo}, #{util.inspect(err)}"
callback.apply null, [err, null]
else
- console.log "Saving new document for #{opts.id}"
+ winston.log "Saving new document for #{opts.id}"
metadataDb.get opts.id , (err, doc) ->
if doc
data = {}
_.extend data, doc, github: github_info
metadataDb.save opts.id, doc['_rev'], data, (err, res) ->
- console.log "update doc #{res}"
+ winston.log "update doc #{res}"
callback.apply null, [err, res]
else
metadataDb.save opts.id, github: github_info, (err, res) ->
- console.log "new doc #{res}"
+ winston.log "new doc #{res}"
callback.apply null, [err, res]
View
3 package.json
@@ -28,6 +28,7 @@
},
"devDependencies": {
}, "scripts": {
- "start": "node server.js"
+ "start": "node server.js",
+ "console": "node"
}
}
View
23 server.coffee
@@ -1,9 +1,12 @@
coffee = require 'coffee-script'
express = require 'express'
cron = require 'cron'
-sys = require 'sys'
+util = require 'util'
+winston = require 'winston'
+
Conf = require './conf'
everyauth = require('./auth').create(Conf)
+helper = require './lib/helper'
packages = require './models/package'
RedisStore = require('connect-redis')(express)
@@ -105,23 +108,15 @@ app.post '/packages/:name/like', (req, res, next) ->
port = process.env.PORT || 4000
app.listen port, () ->
- console.log "app started at port #{port}"
+ winston.log "app started at port #{port}"
do packages.watch_updates
if process.env.ENV_VARIABLE is 'production'
new cron.CronJob '0 0 4 * * * *', () ->
- console.log "Running Cron now"
- packages.import_from_github {}, (err, info) ->
- if err
- console.log err
- else
- console.log info
+ winston.log "Running github sync Cron now"
+ packages.import_from_github {}, helper.print("github sync")
new cron.CronJob '0 0 5 * * * *', () ->
- console.log "Running Cron now"
- packages.import_from_npm {}, (err, info) ->
- if err
- console.log err
- else
- console.log info
+ winston.log "Running Import job Cron now"
+ packages.import_from_npm {}, helper.print( "NPM Import")
View
27 server.js
@@ -1,11 +1,13 @@
(function() {
- var Conf, RedisStore, app, coffee, cron, ensureAuthenticated, everyauth, express, helpers, packages, port, sys;
+ var Conf, RedisStore, app, coffee, cron, ensureAuthenticated, everyauth, express, helper, helpers, packages, port, util, winston;
coffee = require('coffee-script');
express = require('express');
cron = require('cron');
- sys = require('sys');
+ util = require('util');
+ winston = require('winston');
Conf = require('./conf');
everyauth = require('./auth').create(Conf);
+ helper = require('./lib/helper');
packages = require('./models/package');
RedisStore = require('connect-redis')(express);
app = express.createServer();
@@ -162,28 +164,17 @@
});
port = process.env.PORT || 4000;
app.listen(port, function() {
- return console.log("app started at port " + port);
+ return winston.log("app started at port " + port);
});
packages.watch_updates();
if (process.env.ENV_VARIABLE === 'production') {
new cron.CronJob('0 0 4 * * * *', function() {
- console.log("Running Cron now");
- packages.import_from_github({}, function(err, info) {});
- if (err) {
- return console.log(err);
- } else {
- return console.log(info);
- }
+ winston.log("Running github sync Cron now");
+ return packages.import_from_github({}, helper.print("github sync"));
});
new cron.CronJob('0 0 5 * * * *', function() {
- console.log("Running Cron now");
- return packages.import_from_npm({}, function(err, info) {
- if (err) {
- return console.log(err);
- } else {
- return console.log(info);
- }
- });
+ winston.log("Running Import job Cron now");
+ return packages.import_from_npm({}, helper.print("NPM Import"));
});
}
}).call(this);

0 comments on commit 09bd8f8

Please sign in to comment.
Something went wrong with that request. Please try again.