Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const NodeResque = require('node-resque')
- const schedule = require('node-schedule')
- import { prisma } from './generated/prisma-client'
- import * as _ from 'lodash'
- import { completeUserFragment, completeBrandFragment } from './utils';
- export async function matchCount (user, brands) {
- let userPassions = user.passions.map(element => {return element.id;})
- for (let i = 0; i < brands.length; i++) {
- let brand = brands[i];
- let brandPassions = brand.passions.map(element => {return element.id;})
- let passionsIntersection = _.intersection(userPassions, brandPassions)
- if (passionsIntersection.length > 0) {
- let matchScore = 0;
- let matchRecord = await prisma.userBrandMatches({
- where: {
- brandId: brand.id,
- userId: user.id
- }
- });
- if (matchRecord && matchRecord.length > 0) {
- // update value
- await prisma.updateUserBrandMatch({
- where: {id: matchRecord[0].id},
- data: {
- matchScore: matchScore.toString()
- }
- });
- } else {
- // create record
- await prisma.createUserBrandMatch({
- brandId: brand.id,
- userId: user.id,
- matchScore: matchScore.toString()
- });
- }
- await prisma.upsertUserBrandMatch({
- where: {id: null},
- create: {
- brandId: brand.id,
- userId: user.id,
- matchScore: matchScore.toString()
- },
- update: {
- matchScore: matchScore.toString()
- }
- });
- } else {
- // to remove
- let matchRecord = await prisma.userBrandMatches({
- where: {
- brandId: brand.id,
- userId: user.id
- }
- })
- if (matchRecord && matchRecord.length > 0) {
- await prisma.deleteUserBrandMatch({id: matchRecord[0].id});
- }
- }
- }
- }
- export async function boot () {
- // ////////////////////////
- // SET UP THE CONNECTION //
- // ////////////////////////
- const connectionDetails = {
- pkg: 'ioredis',
- host: '127.0.0.1',
- password: null,
- port: 6379,
- database: 0,
- namespace: 'resque',
- looping: true
- }
- const jobs = {
- 'userMatch': {
- plugins: ['JobLock'],
- pluginOptions: {
- JobLock: {}
- },
- perform: async (user, brands) => {
- await matchCount(user, brands);
- }
- }
- }
- // ///////////////////////////
- // DEFINE YOUR WORKER TASKS //
- // ///////////////////////////
- const worker = new NodeResque.Worker({connection: connectionDetails, queues: ['match']}, jobs)
- await worker.connect()
- worker.start()
- // ////////////////////
- // START A SCHEDULER //
- // ////////////////////
- const scheduler = new NodeResque.Scheduler({connection: connectionDetails})
- await scheduler.connect()
- scheduler.start()
- const queue = new NodeResque.Queue({connection: connectionDetails}, jobs)
- queue.on('error', function (error) { console.log(error) })
- await queue.connect()
- let enqueuer = async () => {
- let users: any[] = await prisma.users({where: {active: true}}).$fragment(completeUserFragment);
- let brands = await prisma.brands().$fragment(completeBrandFragment);
- for (let i = 0; i < users.length; i++) {
- let user = users[i];
- await queue.enqueue('match', 'userMatch', [user, brands]);
- }
- }
- schedule.scheduleJob('00 * * * *', async () => {
- if(scheduler.master){
- console.log(">>> enquing a job");
- enqueuer()
- }
- /*
- await queue.end()
- await scheduler.end()
- await worker.end()
- */
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement