Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. const NodeResque = require('node-resque')
  2. const schedule = require('node-schedule')
  3. import { prisma } from './generated/prisma-client'
  4. import * as _ from 'lodash'
  5. import { completeUserFragment, completeBrandFragment } from './utils';
  6.  
  7. export async function matchCount (user, brands) {
  8. let userPassions = user.passions.map(element => {return element.id;})
  9. for (let i = 0; i < brands.length; i++) {
  10. let brand = brands[i];
  11. let brandPassions = brand.passions.map(element => {return element.id;})
  12. let passionsIntersection = _.intersection(userPassions, brandPassions)
  13. if (passionsIntersection.length > 0) {
  14. let matchScore = 0;
  15. let matchRecord = await prisma.userBrandMatches({
  16. where: {
  17. brandId: brand.id,
  18. userId: user.id
  19. }
  20. });
  21. if (matchRecord && matchRecord.length > 0) {
  22. // update value
  23. await prisma.updateUserBrandMatch({
  24. where: {id: matchRecord[0].id},
  25. data: {
  26. matchScore: matchScore.toString()
  27. }
  28. });
  29. } else {
  30. // create record
  31. await prisma.createUserBrandMatch({
  32. brandId: brand.id,
  33. userId: user.id,
  34. matchScore: matchScore.toString()
  35. });
  36. }
  37. await prisma.upsertUserBrandMatch({
  38. where: {id: null},
  39. create: {
  40. brandId: brand.id,
  41. userId: user.id,
  42. matchScore: matchScore.toString()
  43. },
  44. update: {
  45. matchScore: matchScore.toString()
  46. }
  47. });
  48. } else {
  49. // to remove
  50. let matchRecord = await prisma.userBrandMatches({
  51. where: {
  52. brandId: brand.id,
  53. userId: user.id
  54. }
  55. })
  56. if (matchRecord && matchRecord.length > 0) {
  57. await prisma.deleteUserBrandMatch({id: matchRecord[0].id});
  58. }
  59. }
  60. }
  61. }
  62.  
  63. export async function boot () {
  64. // ////////////////////////
  65. // SET UP THE CONNECTION //
  66. // ////////////////////////
  67.  
  68. const connectionDetails = {
  69. pkg: 'ioredis',
  70. host: '127.0.0.1',
  71. password: null,
  72. port: 6379,
  73. database: 0,
  74. namespace: 'resque',
  75. looping: true
  76. }
  77.  
  78. const jobs = {
  79. 'userMatch': {
  80. plugins: ['JobLock'],
  81. pluginOptions: {
  82. JobLock: {}
  83. },
  84. perform: async (user, brands) => {
  85. await matchCount(user, brands);
  86. }
  87. }
  88. }
  89.  
  90. // ///////////////////////////
  91. // DEFINE YOUR WORKER TASKS //
  92. // ///////////////////////////
  93.  
  94. const worker = new NodeResque.Worker({connection: connectionDetails, queues: ['match']}, jobs)
  95. await worker.connect()
  96. worker.start()
  97.  
  98. // ////////////////////
  99. // START A SCHEDULER //
  100. // ////////////////////
  101.  
  102. const scheduler = new NodeResque.Scheduler({connection: connectionDetails})
  103. await scheduler.connect()
  104. scheduler.start()
  105.  
  106. const queue = new NodeResque.Queue({connection: connectionDetails}, jobs)
  107. queue.on('error', function (error) { console.log(error) })
  108. await queue.connect()
  109.  
  110. let enqueuer = async () => {
  111. let users: any[] = await prisma.users({where: {active: true}}).$fragment(completeUserFragment);
  112. let brands = await prisma.brands().$fragment(completeBrandFragment);
  113. for (let i = 0; i < users.length; i++) {
  114. let user = users[i];
  115. await queue.enqueue('match', 'userMatch', [user, brands]);
  116. }
  117. }
  118.  
  119. schedule.scheduleJob('00 * * * *', async () => {
  120. if(scheduler.master){
  121. console.log(">>> enquing a job");
  122. enqueuer()
  123. }
  124. /*
  125. await queue.end()
  126. await scheduler.end()
  127. await worker.end()
  128. */
  129. });
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement