 # find packs to be marked as finalised:true, those which have a newer pack present
 # then only consider finalised:true packs for archiving
 
-db.docHistory.find({
-	expiresAt: {$exists: false}
-	project_id: {$exists: true}
-	v_end: {$exists: true}
-	_id: {$lt: ObjectIdFromDate(new Date(Date.now() - 7 * DAYS))}
-}, {_id:1, doc_id:1, project_id:1}).sort({
-	last_checked:1
-}).limit LIMIT, (err, results) ->
-	if err?
-		logger.log {err}, 'error checking for updates'
-		finish()
-		return
-	pending = _.uniq results, false, (result) -> result.doc_id.toString()
-	logger.log "found #{pending.length} documents to archive"
+if pending?
+	logger.log "got #{pending.length} entries from #{source}"
 	processUpdates pending
+else
+	db.docHistory.find({
+		expiresAt: {$exists: false}
+		project_id: {$exists: true}
+		v_end: {$exists: true}
+		$or: [ {n:512}, {n:1024} ]
+		_id: {$lt: ObjectIdFromDate(new Date(Date.now() - 7 * DAYS))}
+	}, {_id:1, doc_id:1, project_id:1}).sort({
+		last_checked:1
+	}).limit LIMIT, (err, results) ->
+		if err?
+			logger.log {err}, 'error checking for updates'
+			finish()
+			return
+		pending = _.uniq results, false, (result) -> result.doc_id.toString()
+		logger.log "found #{pending.length} documents to archive"
+		processUpdates pending

# this worker script is forked by the main process to look for
# document histories which can be archived

LIMIT = Number(process.argv[2]) || 1000
DOCUMENT_PACK_DELAY = Number(process.argv[3]) || 1000
TIMEOUT = Number(process.argv[4]) || 30*60*1000

shutDownRequested = false
shutDownTimer = setTimeout () ->

async = require "async"
_ = require "underscore"
{db, ObjectId, BSON} = require "./mongojs"
fs = require "fs"
logger = require "logger-sharelatex"
logger.initialize("track-changes-packworker")
if Settings.sentry?.dsn?
