@@ -16,8 +16,8 @@ import {
1616 NodeOptions ,
1717 NodeRequestManager ,
1818 Queue ,
19+ JobHandler ,
1920 createNode ,
20- createJobHandler ,
2121} from '../../src/index.js' ;
2222import { OfferData } from '../../src/shared/types.js' ;
2323import { DealStatus , ProtocolContracts } from '../../src/shared/contracts.js' ;
@@ -63,6 +63,14 @@ process.once('unhandledRejection', (error) => {
6363 process . exit ( 1 ) ;
6464} ) ;
6565
66+ const createJobHandler =
67+ < JobData = unknown , HandlerOptions = unknown > (
68+ handler : JobHandler < JobData , HandlerOptions > ,
69+ ) =>
70+ ( options : HandlerOptions = { } as HandlerOptions ) =>
71+ ( data : JobData ) =>
72+ handler ( data , options ) ;
73+
6674/**
6775 * This is interface of object that you want to pass to the job handler as options
6876 */
@@ -76,8 +84,18 @@ interface DealHandlerOptions {
7684const dealHandler = createJobHandler <
7785 OfferData < RequestQuery , OfferOptions > ,
7886 DealHandlerOptions
79- > ( async ( { name, id, data : offer } , { contracts } ) => {
80- logger . trace ( `Job "${ name } " #${ id } Checking for a deal. Offer #${ offer . id } ` ) ;
87+ > ( async ( offer , options ) => {
88+ if ( ! offer || ! options ) {
89+ throw new Error ( 'Invalid job execution configuration' ) ;
90+ }
91+
92+ const { contracts } = options ;
93+
94+ if ( ! contracts ) {
95+ throw new Error ( 'Contracts manager must be provided to job handler config' ) ;
96+ }
97+
98+ logger . trace ( `Checking for a deal. Offer #${ offer . id } ` ) ;
8199
82100 // Check for a deal
83101 const [ , , , buyer , , , status ] = await contracts . getDeal ( offer ) ;
@@ -98,10 +116,10 @@ const dealHandler = createJobHandler<
98116 } ,
99117 ) ;
100118
101- return true ; // Returning true means that the job must be stopped
119+ return false ; // Returning true means that the job must be stopped
102120 }
103121
104- return ; // Job continuing
122+ return true ; // Job continuing
105123} ) ;
106124
107125/**
@@ -155,17 +173,20 @@ const createRequestsHandler =
155173 checkOut : BigInt ( nowSec ( ) + 2000 ) ,
156174 } ) ;
157175
158- queue . addEventListener ( 'expired ' , ( { detail : job } ) => {
159- logger . trace ( `Job #${ job . id } is expired` ) ;
176+ queue . addEventListener ( 'status ' , ( { detail : job } ) => {
177+ logger . trace ( `Job #${ job . id } status changed` , job ) ;
160178 } ) ;
161179
162180 /**
163181 * On every published offer we expecting a deal.
164182 * So, we add a job for detection of deals
165183 */
166- queue . addJob ( 'deal' , offer , {
184+ queue . add ( {
185+ handlerName : 'deal' ,
186+ data : offer ,
187+ isRecurrent : true ,
188+ recurrenceInterval : 5000 ,
167189 expire : Number ( offer . expire ) ,
168- every : 5000 , // 5 sec
169190 } ) ;
170191 } ;
171192
@@ -211,8 +232,8 @@ const main = async (): Promise<void> => {
211232
212233 const queue = new Queue ( {
213234 storage,
214- hashKey : 'jobs ' ,
215- concurrentJobsNumber : 3 ,
235+ idsKeyName : 'jobsIds ' ,
236+ concurrencyLimit : 3 ,
216237 } ) ;
217238
218239 const requestManager = new NodeRequestManager < RequestQuery > ( {
@@ -234,7 +255,7 @@ const main = async (): Promise<void> => {
234255 requestManager . add ( topic , data ) ;
235256 } ) ;
236257
237- queue . addJobHandler ( 'deal' , dealHandler ( { contracts : contractsManager } ) ) ;
258+ queue . registerHandler ( 'deal' , dealHandler ( { contracts : contractsManager } ) ) ;
238259
239260 /**
240261 * Graceful Shutdown handler
0 commit comments