Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import bftp from 'basic-ftp';
  2. import { FtpJobFactory } from './FtpJobFactory';
  3. import { JobExecutor } from './JobExecutor';
  4. import { JobDataInterface } from './interfaces/JobDataInterface';
  5. import { FtpEventConstants } from './interfaces/FtpEventConstants';
  6. import { FtpConfigInterface } from './interfaces/FtpConfigInterface';
  7.  
  8. export class FtpQueueService<EventConstants extends FtpEventConstants> {
  9. protected queue: JobDataInterface[];
  10. protected connections: bftp.Client[];
  11. protected interval: NodeJS.Timeout;
  12.  
  13. constructor(
  14. protected config: FtpConfigInterface,
  15. protected connectionsNumber: number,
  16. protected jobExecutor: JobExecutor<EventConstants>,
  17. protected jobMaker: FtpJobFactory,
  18. ) {
  19. this.connections = [];
  20. this.queue = [];
  21. // Add events here, possibly move callback out from here
  22. for (let i = 0; i < connectionsNumber; i += 1) {
  23. const client = new bftp.Client(0);
  24. client.busy = false;
  25. this.connections.push(client);
  26. }
  27.  
  28. this.interval = setInterval(() => {}, 10000);
  29. }
  30.  
  31. async initFTPConnections() {
  32. for (const connectionId in this.connections) {
  33. try {
  34. await this.connectCliect(Number(connectionId));
  35. } catch (e) {
  36. console.error({
  37. user: this.config.DEFAULT_FTP_USER,
  38. password: this.config.DEFAULT_FTP_PASS,
  39. host: this.config.DEFAULT_FTP_HOST,
  40. port: this.config.DEFAULT_FTP_PORT,
  41. });
  42. }
  43. }
  44. }
  45.  
  46. push(job: JobDataInterface) {
  47. if (!this.tryToRunJob(job)) {
  48. return this.queue.push(job);
  49. }
  50. }
  51.  
  52. protected pop(): JobDataInterface | undefined {
  53. if (this.queue.length) {
  54. return this.queue.pop();
  55. }
  56. }
  57.  
  58. protected getConnection(connectionId: number): bftp.Client {
  59. return this.connections[connectionId];
  60. }
  61.  
  62. protected executeJob(job: JobDataInterface, freeConnectionId: number) {
  63. this.getConnection(freeConnectionId).busy = true;
  64. return this.getEnsurecConnection(freeConnectionId)
  65. .then(() => this.jobExecutor.executeJob(this.jobMaker.makeJob(job), this.getConnection(freeConnectionId)))
  66. .then(() => this.getConnection(freeConnectionId).cd('/'))
  67. .then(() => {
  68. const job = this.pop();
  69. if (job) {
  70. this.executeJob(job, freeConnectionId);
  71. } else {
  72. this.getConnection(freeConnectionId).busy = false;
  73. }
  74. })
  75. .catch(() => this.regenerateConnection(freeConnectionId));
  76. }
  77.  
  78. protected getEnsurecConnection(freeConnectionId: number) {
  79. return Promise.resolve(
  80. Promise.resolve(this.getConnection(freeConnectionId).pwd()).catch((e) =>
  81. this.regenerateConnection(freeConnectionId),
  82. ),
  83. );
  84. }
  85.  
  86. protected tryToRunJob(job: JobDataInterface) {
  87. const freeConnectionId = this.getEmptyConnectionId();
  88. if (freeConnectionId !== undefined) {
  89. return this.executeJob(job, freeConnectionId);
  90. }
  91. }
  92.  
  93. protected getEmptyConnectionId(): number | undefined {
  94. for (const connectionId in this.connections) {
  95. if (this.connections[connectionId] && !this.connections[connectionId].busy) {
  96. return Number(connectionId);
  97. }
  98. }
  99. }
  100.  
  101. protected regenerateConnection(connectionId: number) {
  102. this.getConnection(connectionId).busy = false;
  103. this.getConnection(connectionId).close();
  104.  
  105. delete this.connections[connectionId];
  106. this.connections[connectionId] = new bftp.Client(0);
  107. this.connections[connectionId].busy = false;
  108.  
  109. return this.connectCliect(connectionId);
  110. }
  111.  
  112. protected connectCliect(connectionId: number) {
  113. return this.connections[connectionId].access({
  114. user: this.config.DEFAULT_FTP_USER,
  115. password: this.config.DEFAULT_FTP_PASS,
  116. host: this.config.DEFAULT_FTP_HOST,
  117. port: this.config.DEFAULT_FTP_PORT,
  118. });
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement