|
|
@ -1,8 +1,8 @@ |
|
|
|
import { Request, Router } from 'express'; |
|
|
|
import { Request, Router } from 'express'; |
|
|
|
// import { Queue } from 'bullmq';
|
|
|
|
// import { Queue } from 'bullmq';
|
|
|
|
// import axios from 'axios';
|
|
|
|
// import axios from 'axios';
|
|
|
|
import catchError from '../../helpers/catchError'; |
|
|
|
import catchError, { NcError } from '../../helpers/catchError'; |
|
|
|
import { Socket } from 'socket.io'; |
|
|
|
import { Server } from 'socket.io'; |
|
|
|
import NocoJobs from '../../../jobs/NocoJobs'; |
|
|
|
import NocoJobs from '../../../jobs/NocoJobs'; |
|
|
|
import job, { AirtableSyncConfig } from './helpers/job'; |
|
|
|
import job, { AirtableSyncConfig } from './helpers/job'; |
|
|
|
import SyncSource from '../../../models/SyncSource'; |
|
|
|
import SyncSource from '../../../models/SyncSource'; |
|
|
@ -17,17 +17,25 @@ enum SyncStatus { |
|
|
|
FAILED = 'FAILED', |
|
|
|
FAILED = 'FAILED', |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export default (router: Router, clients: { [id: string]: Socket }) => { |
|
|
|
export default (router: Router, sv: Server, jobs: { [id: string]: { last_message: any } }) => { |
|
|
|
// add importer job handler and progress notification job handler
|
|
|
|
// add importer job handler and progress notification job handler
|
|
|
|
NocoJobs.jobsMgr.addJobWorker(AIRTABLE_IMPORT_JOB, job); |
|
|
|
NocoJobs.jobsMgr.addJobWorker(AIRTABLE_IMPORT_JOB, job); |
|
|
|
NocoJobs.jobsMgr.addJobWorker( |
|
|
|
NocoJobs.jobsMgr.addJobWorker( |
|
|
|
AIRTABLE_PROGRESS_JOB, |
|
|
|
AIRTABLE_PROGRESS_JOB, |
|
|
|
({ payload, progress }) => { |
|
|
|
({ payload, progress }) => { |
|
|
|
clients?.[payload?.id]?.emit('progress', { |
|
|
|
sv.to(payload?.id).emit('progress', { |
|
|
|
msg: progress?.msg, |
|
|
|
msg: progress?.msg, |
|
|
|
level: progress?.level, |
|
|
|
level: progress?.level, |
|
|
|
status: progress?.status, |
|
|
|
status: progress?.status, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (payload?.id in jobs) { |
|
|
|
|
|
|
|
jobs[payload?.id].last_message = { |
|
|
|
|
|
|
|
msg: progress?.msg, |
|
|
|
|
|
|
|
level: progress?.level, |
|
|
|
|
|
|
|
status: progress?.status, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
@ -49,6 +57,7 @@ export default (router: Router, clients: { [id: string]: Socket }) => { |
|
|
|
status: SyncStatus.COMPLETED, |
|
|
|
status: SyncStatus.COMPLETED, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
delete jobs[payload?.id]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
NocoJobs.jobsMgr.addFailureCbk(AIRTABLE_IMPORT_JOB, (payload, error: any) => { |
|
|
|
NocoJobs.jobsMgr.addFailureCbk(AIRTABLE_IMPORT_JOB, (payload, error: any) => { |
|
|
|
NocoJobs.jobsMgr.add(AIRTABLE_PROGRESS_JOB, { |
|
|
|
NocoJobs.jobsMgr.add(AIRTABLE_PROGRESS_JOB, { |
|
|
@ -58,6 +67,7 @@ export default (router: Router, clients: { [id: string]: Socket }) => { |
|
|
|
status: SyncStatus.FAILED, |
|
|
|
status: SyncStatus.FAILED, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
delete jobs[payload?.id]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
router.post( |
|
|
|
router.post( |
|
|
@ -73,6 +83,10 @@ export default (router: Router, clients: { [id: string]: Socket }) => { |
|
|
|
router.post( |
|
|
|
router.post( |
|
|
|
'/api/v1/db/meta/syncs/:syncId/trigger', |
|
|
|
'/api/v1/db/meta/syncs/:syncId/trigger', |
|
|
|
catchError(async (req: Request, res) => { |
|
|
|
catchError(async (req: Request, res) => { |
|
|
|
|
|
|
|
if (req.params.syncId in jobs) { |
|
|
|
|
|
|
|
NcError.badRequest('Sync already in progress'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const syncSource = await SyncSource.get(req.params.syncId); |
|
|
|
const syncSource = await SyncSource.get(req.params.syncId); |
|
|
|
|
|
|
|
|
|
|
|
const user = await syncSource.getUser(); |
|
|
|
const user = await syncSource.getUser(); |
|
|
@ -90,12 +104,26 @@ export default (router: Router, clients: { [id: string]: Socket }) => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NocoJobs.jobsMgr.add<AirtableSyncConfig>(AIRTABLE_IMPORT_JOB, { |
|
|
|
NocoJobs.jobsMgr.add<AirtableSyncConfig>(AIRTABLE_IMPORT_JOB, { |
|
|
|
id: req.query.id, |
|
|
|
id: req.params.syncId, |
|
|
|
...(syncSource?.details || {}), |
|
|
|
...(syncSource?.details || {}), |
|
|
|
projectId: syncSource.project_id, |
|
|
|
projectId: syncSource.project_id, |
|
|
|
authToken: token, |
|
|
|
authToken: token, |
|
|
|
baseURL, |
|
|
|
baseURL, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
jobs[req.params.syncId] = { |
|
|
|
|
|
|
|
last_message: { |
|
|
|
|
|
|
|
msg: 'Sync started' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
res.json({}); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
router.post( |
|
|
|
|
|
|
|
'/api/v1/db/meta/syncs/:syncId/abort', |
|
|
|
|
|
|
|
catchError(async (req: Request, res) => { |
|
|
|
|
|
|
|
if (req.params.syncId in jobs) { |
|
|
|
|
|
|
|
delete jobs[req.params.syncId]; |
|
|
|
|
|
|
|
} |
|
|
|
res.json({}); |
|
|
|
res.json({}); |
|
|
|
}) |
|
|
|
}) |
|
|
|
); |
|
|
|
); |
|
|
|