請將例如公牛視為替代方案。謝謝你!
Kue是由Redis支持的優先職務隊列,該k.
PROTIP這是最新的庫文檔,請確保還閱讀Changelist。
最新版本:
$ npm install kue
主分支:
$ npm install http://github.com/Automattic/kue/tarball/master
首先使用kue.createQueue()
創建一個作業Queue
:
var kue = require ( 'kue' )
, queue = kue . createQueue ( ) ;
使用作業類型(“電子郵件”)調用queue.create()
,任意工作數據將返回Job
,然後可以save()
ed,將其添加到redis中,默認優先級為“正常”。 save()
方法可選地接受回調,如果出現error
,則會出現錯誤。 title
密鑰是特殊的,將顯示在UI中的作業清單中,從而更容易找到特定的作業。
var job = queue . create ( 'email' , {
title : 'welcome email for tj'
, to : '[email protected]'
, template : 'welcome-email'
} ) . save ( function ( err ) {
if ( ! err ) console . log ( job . id ) ;
} ) ;
要指定作業的優先級,只需調用帶有數字或優先級名稱的priority()
方法,將其映射到一個數字。
queue . create ( 'email' , {
title : 'welcome email for tj'
, to : '[email protected]'
, template : 'welcome-email'
} ) . priority ( 'high' ) . save ( ) ;
默認優先級映射如下:
{
low : 10
, normal : 0
, medium : - 5
, high : - 10
, critical : - 15
} ;
默認情況下,作業只有一次嘗試,即當他們失敗時,它們被標記為失敗,並保持這種方式直到您進行干預。但是,Kue允許您指定此內容,這對於諸如傳輸電子郵件之類的工作很重要,該電子郵件通常在失敗後可能會無問題重試。為此,用一個數字調用.attempts()
方法。
queue . create ( 'email' , {
title : 'welcome email for tj'
, to : '[email protected]'
, template : 'welcome-email'
} ) . priority ( 'high' ) . attempts ( 5 ) . save ( ) ;
即使您的工作通過Job#delay
設置了延遲,就可以盡快進行工作重試嘗試。如果您想在失敗時延遲作業重新攻擊(稱為退縮),則可以以不同的方式使用Job#backoff
方法:
// Honor job's original delay (if set) at each attempt, defaults to fixed backoff
job . attempts ( 3 ) . backoff ( true )
// Override delay value, fixed backoff
job . attempts ( 3 ) . backoff ( { delay : 60 * 1000 , type : 'fixed' } )
// Enable exponential backoff using original delay (if set)
job . attempts ( 3 ) . backoff ( { type : 'exponential' } )
// Use a function to get a customized next attempt delay value
job . attempts ( 3 ) . backoff ( function ( attempts , delay ) {
//attempts will correspond to the nth attempt failure so it will start with 0
//delay will be the amount of the last delay, not the initial delay unless attempts === 0
return my_customized_calculated_delay ;
} )
在最後一個方案中,將在每個重新攻擊上執行(通過評估),以獲取下一個嘗試延遲值,這意味著您無法參考其中的外部/上下文變量。
求職者可以設定工作能夠處於活躍狀態的時間的到期價值,因此,如果工人沒有及時回复,kue會在TTL exceeded
錯誤消息,從而阻止該作業處於活躍狀態並破壞。並發。
queue . create ( 'email' , { title : 'email job with TTL' } ) . ttl ( milliseconds ) . save ( ) ;
特定於工作的日誌使您可以在工作一生的任何時候將信息暴露於UI。為此,只需調用job.log()
,它接受消息字符串和可變題材的類似sprintf的支持:
job . log ( '$%d sent to %s' , amount , user . name ) ;
或其他任何東西(內部使用util.inspect()):
job . log ( { key : 'some key' , value : 10 } ) ;
job . log ( [ 1 , 2 , 3 , 5 , 8 ] ) ;
job . log ( 10.1 ) ;
工作進度對於長期運行的工作(例如視頻轉換)非常有用。要更新工作的進度,請簡單地調用job.progress(completed, total [, data])
:
job . progress ( frames , totalFrames ) ;
數據可用於傳遞有關工作的額外信息。例如,消息或對象具有一些額外的上下文數據到當前狀態。
通過Redis Pubsub在Job
實例上發射特定於工作的事件。目前支持以下事件:
enqueue
該工作現在排隊start
工作現在正在運行promotion
該工作從延遲狀態晉升為排隊progress
工作的進度範圍為0-100failed attempt
失敗了,但仍在嘗試failed
工作失敗了,沒有剩餘的嘗試complete
工作已經完成remove
工作已刪除例如,這看起來像以下內容:
var job = queue . create ( 'video conversion' , {
title : 'converting loki's to avi'
, user : 1
, frames : 200
} ) ;
job . on ( 'complete' , function ( result ) {
console . log ( 'Job completed with data ' , result ) ;
} ) . on ( 'failed attempt' , function ( errorMessage , doneAttempts ) {
console . log ( 'Job failed' ) ;
} ) . on ( 'failed' , function ( errorMessage ) {
console . log ( 'Job failed' ) ;
} ) . on ( 'progress' , function ( progress , data ) {
console . log ( 'r job #' + job . id + ' ' + progress + '% complete with data ' , data ) ;
} ) ;
請注意,由於重新啟動node.js流程將失去對特定工作對象的引用,因此不能保證在過程重新啟動時收到工作級別事件。如果您想要一個更可靠的事件處理程序來尋找隊列活動。
注意庫將作業對象存儲在內存中,直到它們完成/無法在其上散發事件為止。如果您在未完成的作業中具有巨大的並發性,請關閉此功能,並使用隊列級別事件進行更好的內存縮放。
kue . createQueue ( { jobEvents : false } )
另外,您可以使用工作級功能events
來控制是否在工作級別上為作業發射事件。
var job = queue . create ( 'test' ) . events ( false ) . save ( ) ;
隊列級事件提供了對前面提到的作業級事件的訪問權限,但是范圍範圍為Queue
實例,以在“全局”級別應用邏輯。一個例子是刪除完成的工作:
queue . on ( 'job enqueue' , function ( id , type ) {
console . log ( 'Job %s got queued of type %s' , id , type ) ;
} ) . on ( 'job complete' , function ( id , result ) {
kue . Job . get ( id , function ( err , job ) {
if ( err ) return ;
job . remove ( function ( err ) {
if ( err ) throw err ;
console . log ( 'removed completed job #%d' , job . id ) ;
} ) ;
} ) ;
} ) ;
可用的事件與“工作事件”中提到的事件相同,但是以“工作”為前綴。
可以通過調用.delay(ms)
方法,將延遲的作業安排為任意距離排隊,並將其傳遞到現在的毫秒數。另外,您可以將來通過特定時間傳遞JavaScript Date
對象。這會自動將Job
標記為“延遲”。
var email = queue . create ( 'email' , {
title : 'Account renewal required'
, to : '[email protected]'
, template : 'renewal-email'
} ) . delay ( milliseconds )
. priority ( 'high' )
. save ( ) ;
Kue將使用計時器檢查延遲的作業,如果超出計劃的延遲,則將其促進它們,默認為每秒前1000名作業的檢查。
使用Kue處理工作很簡單。首先創建一個Queue
實例,就像我們為創建作業,為我們提供redis等的訪問,然後使用關聯類型調用queue.process()
。請注意,與createQueue
建議的名稱不同,它當前返回Singleton Queue
實例。因此,您可以在Node.js進程中配置和使用一個Queue
對象。
在下面的示例中,我們將done
回調傳遞給email
,當發生錯誤時,我們調用done(err)
告訴kue發生某些事情,否則我們僅在作業完成時就調用done()
。如果此功能響應錯誤,則將顯示在UI中,並且該作業將被標記為故障。傳遞給完成的錯誤對象應為標準類型Error
。
var kue = require ( 'kue' )
, queue = kue . createQueue ( ) ;
queue . process ( 'email' , function ( job , done ) {
email ( job . data . to , done ) ;
} ) ;
function email ( address , done ) {
if ( ! isValidEmail ( address ) ) {
//done('invalid to address') is possible but discouraged
return done ( new Error ( 'invalid to address' ) ) ;
}
// email send stuff...
done ( ) ;
}
工人還可以將工作結果作為完成的第二個參數done(null,result)
以將其存儲在Job.result
鍵中。 result
還通過complete
活動處理程序傳遞,以便如果願意的話,就可以收到它。
默認情況下,呼叫queue.process()
一次只能接受一項處理。對於諸如發送電子郵件之類的小任務,這不是理想的選擇,因此我們可以通過一個數字來指定此類型的最大活動作業:
queue . process ( 'email' , 20 , function ( job , done ) {
// ...
} ) ;
工人可以暫時暫停並恢復他們的活動。也就是說,在調用pause
之後,他們將在調用resume
之前沒有收到過程回調的工作。 pause
功能優雅地關閉了該工人,並在優雅關閉中使用與shutdown
方法相同的內部功能。
queue . process ( 'email' , function ( job , ctx , done ) {
ctx . pause ( 5000 , function ( err ) {
console . log ( "Worker is paused... " ) ;
setTimeout ( function ( ) { ctx . resume ( ) ; } , 10000 ) ;
} ) ;
} ) ;
請注意,來自kue的ctx
參數>=0.9.0
是過程回調函數的第二個參數,並且done
始終是最後一個
請注意, pause
方法簽名已從kue >=0.9.0
更改為將回調函數移至最後一個。
對於一個“真實”示例,假設我們需要從帶有節點 - 膠囊的眾多幻燈片中編譯PDF。我們的工作可能包含以下數據,請注意,通常您不應將大量數據存儲在工作中,最好存儲像ID之類的參考,在處理時將其拉入。
queue . create ( 'slideshow pdf' , {
title : user . name + "'s slideshow"
, slides : [ ... ] // keys to data stored in redis, mongodb, or some other store
} ) ;
我們可以通過job.data
屬性在處理時在單獨的過程中訪問相同的任意數據。在示例中,我們渲染每個幻燈片一對一,更新作業的日誌和進度。
queue . process ( 'slideshow pdf' , 5 , function ( job , done ) {
var slides = job . data . slides
, len = slides . length ;
function next ( i ) {
var slide = slides [ i ] ; // pretend we did a query on this slide id ;)
job . log ( 'rendering %dx%d slide' , slide . width , slide . height ) ;
renderSlide ( slide , function ( err ) {
if ( err ) return done ( err ) ;
job . progress ( i , len , { nextSlide : i == len ? 'itsdone' : i + 1 } ) ;
if ( i == len ) done ( )
else next ( i + 1 ) ;
} ) ;
}
next ( 0 ) ;
} ) ;
Queue#shutdown([timeout,] fn)
在完成當前的活動作業後,所有工人都會停止處理。工人將等待timeout
毫秒,以便將其主動作業稱為“被調用”或“關閉錯誤原因”的主動作業failed
。當所有工人告訴Kue時,他們被停止了fn
。
var queue = require ( 'kue' ) . createQueue ( ) ;
process . once ( 'SIGTERM' , function ( sig ) {
queue . shutdown ( 5000 , function ( err ) {
console . log ( 'Kue shutdown: ' , err || '' ) ;
process . exit ( 0 ) ;
} ) ;
} ) ;
請注意, shutdown
方法簽名從庫>=0.9.0
更改為將回調函數移至最後一個。
REDIS客戶庫中的所有錯誤或隊列都散發到Queue
對象。您應該綁定到error
事件,以防止未知的異常或調試庫錯誤。
var queue = require ( 'kue' ) . createQueue ( ) ;
queue . on ( 'error' , function ( err ) {
console . log ( 'Oops... ' , err ) ;
} ) ;
Kue標記了您的工人調用done
後完成/失敗的工作,因此您應該使用適當的錯誤處理來防止工人代碼中未知的例外和Node.js進程在手柄作業之前退出。這可以通過兩種方式實現:
queue . process ( 'my-error-prone-task' , function ( job , done ) {
var domain = require ( 'domain' ) . create ( ) ;
domain . on ( 'error' , function ( err ) {
done ( err ) ;
} ) ;
domain . run ( function ( ) { // your process function
throw new Error ( 'bad things happen' ) ;
done ( ) ;
} ) ;
} ) ;
通知 -域是從穩定性0的nodej中棄用,不建議使用。
這是最柔軟,最佳的解決方案,但是不是與庫一起內置的。請參閱此討論。您可以在相關的開放式問題中對此功能發表評論。
您也可以使用承諾做類似的事情
queue . process ( 'my-error-prone-task' , function ( job , done ) {
Promise . method ( function ( ) { // your process function
throw new Error ( 'bad things happen' ) ;
} ) ( ) . nodeify ( done )
} ) ;
但這不會像域那樣在異步調用堆棧中捕獲異常。
uncaughtException
並優雅地關閉了庫,但是這不是建議在JavaScript中處理成語的建議,因為您正在失去錯誤上下文。 process . once ( 'uncaughtException' , function ( err ) {
console . error ( 'Something bad happened: ' , err ) ;
queue . shutdown ( 1000 , function ( err2 ) {
console . error ( 'Kue shutdown result: ' , err2 || 'OK' ) ;
process . exit ( 0 ) ;
} ) ;
} ) ;
Kue當前使用客戶端的工作狀態管理以及REDIS在該操作中間崩潰時,會發生一些卡住的工作或索引不一致。結果是,只有創建新的作業,如果沒有再創造新的工作,他們將永遠卡住,他們才會被工人撤出,並被工人撤出。因此,我們強烈建議您通過致電:
queue . watchStuckJobs ( interval )
interval
為毫秒,默認為1000ms
Kue將重構為1.0版的完全原子工作狀態管理,這將通過LUA腳本和/或BRPOPLPUSH組合進行。您可以在這里和這裡閱讀更多。
隊列對像有兩種類型的方法來告訴您每個狀態的作業數量
queue . inactiveCount ( function ( err , total ) { // others are activeCount, completeCount, failedCount, delayedCount
if ( total > 100000 ) {
console . log ( 'We need some back pressure here' ) ;
}
} ) ;
您也可以查詢特定的作業類型:
queue . failedCount ( 'my-critical-job' , function ( err , total ) {
if ( total > 10000 ) {
console . log ( 'This is tOoOo bad' ) ;
}
} ) ;
並迭代工作ID
queue . inactive ( function ( err , ids ) { // others are active, complete, failed, delayed
// you may want to fetch each id to get the Job object out of it...
} ) ;
但是,第二個沒有擴展到大型部署,您可以使用更具體的Job
靜態方法:
kue . Job . rangeByState ( 'failed' , 0 , n , 'asc' , function ( err , jobs ) {
// you have an array of maximum n Job objects here
} ) ;
或者
kue . Job . rangeByType ( 'my-job-type' , 'failed' , 0 , n , 'asc' , function ( err , jobs ) {
// you have an array of maximum n Job objects here
} ) ;
請注意,最後兩種方法可能會更改以後的庫版本。
如果您在錯誤處理部分或以任何方式丟失了主動作業的過程中沒有做任何事情,則可以在重新啟動過程時從它們中恢復過來。盲目的邏輯是重新加入所有卡住的工作:
queue . active ( function ( err , ids ) {
ids . forEach ( function ( id ) {
kue . Job . get ( id , function ( err , job ) {
// Your application should check if job is a stuck one
job . inactive ( ) ;
} ) ;
} ) ;
} ) ;
注意在集群部署中,您的應用程序應該注意不要涉及有效的作業,目前是其他工人的工作。
作業數據和搜索索引佔據了重新的內存空間,因此您需要在現實世界部署中進行一些工作。您的第一個機會是在完成時使用自動刪除工作。
queue . create ( ... ) . removeOnComplete ( true ) . save ( )
但是,如果您最終/暫時需要完成的工作數據,則可以設置一個按需刪除腳本, n
kue . Job . rangeByState ( 'complete' , 0 , n , 'asc' , function ( err , jobs ) {
jobs . forEach ( function ( job ) {
job . remove ( function ( ) {
console . log ( 'removed ' , job . id ) ;
} ) ;
} ) ;
} ) ;
請注意,您應該為每個工作對象.remove
足夠的時間來完成。
默認情況下,Kue將使用客戶端默認設置連接到REDIS(端口默認為6379
,主機默認為127.0.0.1
,前綴默認為q
)。 Queue#createQueue(options)
接受options.redis
鍵中的redis連接選項。
var kue = require ( 'kue' ) ;
var q = kue . createQueue ( {
prefix : 'q' ,
redis : {
port : 1234 ,
host : '10.0.50.20' ,
auth : 'password' ,
db : 3 , // if provided select a non-default redis db
options : {
// see https://github.com/mranney/node_redis#rediscreateclient
}
}
} ) ;
prefix
控制Redis中使用的關鍵名稱。默認情況下,這只是q
通常不應更改前綴,除非您需要為多個應用程序使用一個redis實例。它對於在主應用程序中提供隔離的測試台也很有用。
您還可以將連接信息指定為URL字符串。
var q = kue . createQueue ( {
redis : 'redis://example.com:1234?redis_option=value&redis_option=value'
} ) ;
由於Node_redis支持Unix域插座,因此您也可以告訴Kue這樣做。有關REDIS服務器配置,請參見Unix-Domain插座。
var kue = require ( 'kue' ) ;
var q = kue . createQueue ( {
prefix : 'q' ,
redis : {
socket : '/data/sockets/redis.sock' ,
auth : 'password' ,
options : {
// see https://github.com/mranney/node_redis#rediscreateclient
}
}
} ) ;
任何符合node_redis api符合符合(或適應)的node.js redis客戶庫都可以注入kue。您應該僅提供createClientFactory
函數作為REDIS連接工廠,而不是提供Node_redis連接選項。
以下是一個示例代碼,可以使Redis-Sentinel連接到Redis Sentinel以進行自動主/從失敗轉移。
var kue = require ( 'kue' ) ;
var Sentinel = require ( 'redis-sentinel' ) ;
var endpoints = [
{ host : '192.168.1.10' , port : 6379 } ,
{ host : '192.168.1.11' , port : 6379 }
] ;
var opts = options || { } ; // Standard node_redis client options
var masterName = 'mymaster' ;
var sentinel = Sentinel . Sentinel ( endpoints ) ;
var q = kue . createQueue ( {
redis : {
createClientFactory : function ( ) {
return sentinel . createClient ( masterName , opts ) ;
}
}
} ) ;
請注意,所有<0.8.x
客戶端代碼都應重構以將redis選項傳遞為Queue#createQueue
而不是猴子修補的樣式redis#createClient
的覆蓋層,否則它們將從kue 0.8.x
中損壞。
var Redis = require ( 'ioredis' ) ;
var kue = require ( 'kue' ) ;
// using https://github.com/72squared/vagrant-redis-cluster
var queue = kue . createQueue ( {
redis : {
createClientFactory : function ( ) {
return new Redis . Cluster ( [ {
port : 7000
} , {
port : 7001
} ] ) ;
}
}
} ) ;
UI是一個小快報。在bin/
中提供了一個腳本,用於將接口作為具有默認設置的獨立應用程序運行。您可以傳遞端口,redis-url和前綴的選項。例如:
node_modules/kue/bin/kue-dashboard -p 3050 -r redis://127.0.0.1:3000 -q prefix
您也可以在另一個應用程序中啟動它:
var kue = require ( 'kue' ) ;
kue . createQueue ( ... ) ;
kue . app . listen ( 3000 ) ;
標題默認為“庫”,以更改此調用:
kue . app . set ( 'title' , 'My Application' ) ;
請注意,如果您使用的是非默認kue選項,則必須在訪問kue.app
之前調用kue.createQueue(...)
。
您也可以使用ArnaudBénard貢獻的Kue-UI Web界面
與UI庫一起,還暴露了一個由UI使用的JSON API。
查詢作業,例如“ get /job /search?q = avi視頻”:
[ "5" , "7" , "10" ]
默認情況下,kue索引了整個作業數據對象進行搜索,但是可以通過調用Job#searchKeys
來自定義kue,以告訴kue在工作數據上要創建索引上的哪些鍵:
var kue = require ( 'kue' ) ;
queue = kue . createQueue ( ) ;
queue . create ( 'email' , {
title : 'welcome email for tj'
, to : '[email protected]'
, template : 'welcome-email'
} ) . searchKeys ( [ 'to' , 'title' ] ) . save ( ) ;
默認情況下關閉搜索功能>=0.9.0
。在此處閱讀有關此信息的更多信息。如果需要:
var kue = require ( 'kue' ) ;
q = kue . createQueue ( {
disableSearch : false
} ) ;
npm install reds --save
目前以國家計數和工人活動時間的響應毫秒:
{ "inactiveCount" : 4 , "completeCount" : 69 , "activeCount" : 2 , "failedCount" : 0 , "workTime" : 20892 }
通過:id
:
{ "id" : "3" , "type" : "email" , "data" : { "title" : "welcome email for tj" , "to" : "[email protected]" , "template" : "welcome-email" } , "priority" : - 10 , "progress" : "100" , "state" : "complete" , "attempts" : null , "created_at" : "1309973155248" , "updated_at" : "1309973155248" , "duration" : "15002" }
獲取工作:id
的日誌:
[ 'foo' , 'bar' , 'baz' ]
獲取具有指定範圍的工作:from
:to
:例如“/jobs/0..2”,其中:order
可能為“ asc”或“ desc”:
[ { "id" : "12" , "type" : "email" , "data" : { "title" : "welcome email for tj" , "to" : "[email protected]" , "template" : "welcome-email" } , "priority" : - 10 , "progress" : 0 , "state" : "active" , "attempts" : null , "created_at" : "1309973299293" , "updated_at" : "1309973299293" } , { "id" : "130" , "type" : "email" , "data" : { "title" : "welcome email for tj" , "to" : "[email protected]" , "template" : "welcome-email" } , "priority" : - 10 , "progress" : 0 , "state" : "active" , "attempts" : null , "created_at" : "1309975157291" , "updated_at" : "1309975157291" } ]
與上述相同,限制以下限制:state
是:
- active
- inactive
- failed
- complete
與上述相同,多麼限於:type
和:state
。
刪除工作:id
:
$ curl -X DELETE http://local:3000/job/2
{"message":"job 2 removed"}
創建工作:
$ curl -H "Content-Type: application/json" -X POST -d
'{
"type": "email",
"data": {
"title": "welcome email for tj",
"to": "[email protected]",
"template": "welcome-email"
},
"options" : {
"attempts": 5,
"priority": "high"
}
}' http://localhost:3000/job
{"message": "job created", "id": 3}
您可以通過陣列一次創建多個作業。在這種情況下,響應也將是一個數組,保留了訂單:
$ curl -H "Content-Type: application/json" -X POST -d
'[{
"type": "email",
"data": {
"title": "welcome email for tj",
"to": "[email protected]",
"template": "welcome-email"
},
"options" : {
"attempts": 5,
"priority": "high"
}
},
{
"type": "email",
"data": {
"title": "followup email for tj",
"to": "[email protected]",
"template": "followup-email"
},
"options" : {
"delay": 86400,
"attempts": 5,
"priority": "high"
}
}]' http://localhost:3000/job
[
{"message": "job created", "id": 4},
{"message": "job created", "id": 5}
]
注意:批量插入多個作業時,如果一個插入失敗,則kue將繼續按順序處理剩餘的作業。響應陣列將包含成功添加的作業的ID,任何失敗的元素將是描述錯誤的對象: {"error": "error reason"}
。
下面的示例顯示瞭如何使用群集在CPU上擴散工作加載負載。請參閱群集模塊的文檔,以獲取有關使用它的更多詳細示例。
當cluster .isMaster
在主過程的上下文中執行文件時,在這種情況下,您可以執行僅需要一次的任務,例如啟動Web應用程序與Kue捆綁在一起。每個工人都執行else
塊中的邏輯。
var kue = require ( 'kue' )
, cluster = require ( 'cluster' )
, queue = kue . createQueue ( ) ;
var clusterWorkerSize = require ( 'os' ) . cpus ( ) . length ;
if ( cluster . isMaster ) {
kue . app . listen ( 3000 ) ;
for ( var i = 0 ; i < clusterWorkerSize ; i ++ ) {
cluster . fork ( ) ;
}
} else {
queue . process ( 'email' , 10 , function ( job , done ) {
var pending = 5
, total = pending ;
var interval = setInterval ( function ( ) {
job . log ( 'sending!' ) ;
job . progress ( total - pending , total ) ;
-- pending || done ( ) ;
pending || clearInterval ( interval ) ;
} , 1000 ) ;
} ) ;
}
這將根據每台機器CPU內核創建一個email
工作處理器(Worker),每個機器都可以處理10個並發的電子郵件作業,從而導致10 * N
並發的電子郵件作業在您的N
Core Machine中處理。
現在,當您在瀏覽器中訪問Kue的UI時,您會發現N
更快地處理工作! (如果您有N
內核)。
通過使用應用程序安裝,您可以自定義Web應用程序,啟用TLS或添加其他中間件(例如basic-auth-connect
。
$ npm install --save basic-auth-connect
var basicAuth = require ( 'basic-auth-connect' ) ;
var app = express . createServer ( { ... tls options ... } ) ;
app . use ( basicAuth ( 'foo' , 'bar' ) ) ;
app . use ( kue . app ) ;
app . listen ( 3000 ) ;
啟用測試模式可以將所有作業推向jobs
數組。對該數組中的工作做出斷言,以確保正在測試的代碼正確地完成工作。
queue = require ( 'kue' ) . createQueue ( ) ;
before ( function ( ) {
queue . testMode . enter ( ) ;
} ) ;
afterEach ( function ( ) {
queue . testMode . clear ( ) ;
} ) ;
after ( function ( ) {
queue . testMode . exit ( )
} ) ;
it ( 'does something cool' , function ( ) {
queue . createJob ( 'myJob' , { foo : 'bar' } ) . save ( ) ;
queue . createJob ( 'anotherJob' , { baz : 'bip' } ) . save ( ) ;
expect ( queue . testMode . jobs . length ) . to . equal ( 2 ) ;
expect ( queue . testMode . jobs [ 0 ] . type ) . to . equal ( 'myJob' ) ;
expect ( queue . testMode . jobs [ 0 ] . data ) . to . eql ( { foo : 'bar' } ) ;
} ) ;
重要:默認情況下,在測試模式下創建時未處理作業。您可以通過將TREA TREA轉到TestMode.enter來啟用工作處理
before ( function ( ) {
queue . testMode . enter ( true ) ;
} ) ;
我們喜歡貢獻!
貢獻時,請遵循簡單的規則:
(麻省理工學院許可證)
版權(c)2011 LearnBoost <[email protected]>
特此免費授予任何獲得此軟件副本和相關文檔文件(“軟件”)的人,以無限制處理該軟件,包括無限制的使用權,複製,修改,修改,合併,發布,分發,分佈和/或出售該軟件的副本,並允許提供該軟件的人,但要遵守以下條件:
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“按原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,適合特定目的和非侵害的保證。 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE軟體.