fix(tasks): Check Task is in registry, remove any non-tasks when loading

This commit is contained in:
Ben Gotow 2016-05-19 11:35:38 -07:00
parent c8097f1cd9
commit c6f6c7c3a0
2 changed files with 16 additions and 0 deletions

View file

@ -54,6 +54,14 @@ describe "TaskQueue", ->
expect(@retryInFutureTask.queueState.retryDelay).toEqual(undefined)
expect(TaskQueue._updateSoon).toHaveBeenCalled()
it "should remove any items in the queue which were not deserialized as tasks", ->
queue = [@processingTask, {type: 'bla'}, @retryInFutureTask]
spyOn(DatabaseStore, 'findJSONBlob').andCallFake => Promise.resolve(queue)
spyOn(TaskQueue, '_updateSoon')
waitsForPromise =>
TaskQueue._restoreQueue().then =>
expect(TaskQueue._queue).toEqual([@processingTask, @retryInFutureTask])
describe "findTask", ->
beforeEach ->
@subclassA = new TaskSubclassA()

View file

@ -6,6 +6,7 @@ path = require 'path'
CoffeeHelpers = require '../coffee-helpers'
Task = require("../tasks/task").default
TaskRegistry = require('../../task-registry').default
Utils = require "../models/utils"
Reflux = require 'reflux'
Actions = require '../actions'
@ -112,6 +113,8 @@ class TaskQueue
enqueue: (task) =>
if not (task instanceof Task)
throw new Error("You must queue a `Task` instance")
if not (TaskRegistry.isInRegistry(task.constructor.name))
throw new Error("You must queue a `Task` instance which is registred with the TaskRegistry")
if not task.id
throw new Error("Tasks must have an ID prior to being queued. Check that your Task constructor is calling `super`")
if not task.queueState
@ -297,6 +300,11 @@ class TaskQueue
delete task.queueState['retryAfter']
delete task.queueState['retryDelay']
# The Task queue is completely wrecked if an item in the queue is not a
# task instance. This can happen if we removed or renamed the Task class,
# or if it was not registred with the TaskRegistry properly.
queue = queue.filter (task) => task instanceof Task
@_queue = queue
@_updateSoon()