 		clearInterval intervalDescriptor if searchJobs.length is 0 and apiJobs.length is 0
 		searchJobs.shift()?() for i in [0 ... globals.searchRequestsPerMinute]
 		apiJobs.shift()?() for i in [0 ... globals.apiRequestsPerMinute]
+		undefined
 	intervalDescriptor = setInterval doTask, 1000 * 60 # Runs every minute
 	doTask()

				console.log "Saved language statistics for repo #{repo.fullName}"

fetchRepoCommits = (repo, date = "") ->
	apiJobs.push ->
		githubApi
			url: "/repos/#{repo.full_name}/commits"
			qs:
				until: date
				per_page: 100
		, (err, response, body) ->
			items = JSON.parse(body)
			if items.length > 0
				items.forEach (item) ->
					date = item.commit.author.date
					getUserOrCreateUser item.author.login, (err, user) ->
						commit = new db.Commit
							sha: item.sha
							author: user._id
							repository: repo._id
							timestamp: item.commit.author.date
						commit.save(err, commit) ->
							if not err?
								exports.fetchCommit repo, commit
				fetchRepoCommits repo, date


getUserOrCreateUser = (username, callback) ->
	db.User.findOneAndUpdate {username: username}, {$setOnInsert: username: username}, upsert: true, new: true, (err, user) ->
		callback err, user

fetchCommit = (repo, commit) ->
	apiJobs.push ->
		githubApi
			url: "/repos/#{repo.fullName}/commits/#{commit.sha}"
		, (err, response, body) ->
			changes = []
			com = JSON.parse(body)
			items = com.files
			items.forEach (item) ->
				fileName = item.fileName
				changesMade = item.changes
				language = mapper.getLanguage fileName.substring fileName.lastIndexOf(".")
				db.Commit.findAndModify {sha: item.sha}, {$addToSet: {changes: {language: language}}}, {new:true} (err, resp) ->
					if !err
						db.Commit.findAndModify {sha: item.sha, "changes.language": language }, {$inc: {"changes.$.count": changesMade}}, {new: true} , (err, resp) ->
							if !err
								console.log "Successfully inserted commit"

exports.startJobs = ->
	intervalDescriptor = null

				items = JSON.parse(body).items
				async.each items, (item, callback) ->
					db.Repository.findOneAndUpdate {fullName: item.full_name},
						fullName: item.full_name
						stars: item.stargazers_count
						forks: item.forks_count
					, upsert: true, new: true
					, (err, repo) ->
						fetchRepoLanguages repo
						fetchRepoCommits repo
						callback()
				, ->
					console.log "Saved #{items.length} repositories of Top Repositories from page #{page}"
