From 4c12696d30b2f82a97e07f2c15bba4fc66ce3bec Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 12:41:25 -0300 Subject: [PATCH 01/14] Add removeJob function; --- Models/Queue.js | 671 ++++++++++++++++++++++++------------------------ 1 file changed, 337 insertions(+), 334 deletions(-) diff --git a/Models/Queue.js b/Models/Queue.js index 5386f1d..057bc09 100644 --- a/Models/Queue.js +++ b/Models/Queue.js @@ -14,420 +14,423 @@ import promiseReflect from 'promise-reflect'; export class Queue { - /** - * - * Set initial class properties. - * - * @constructor - */ - constructor() { - this.realm = null; - this.worker = new Worker(); - this.status = 'inactive'; - } - - /** - * - * Initializes the queue by connecting to Realm database. - * - */ - async init() { - if (this.realm === null) { - this.realm = await Database.getRealmInstance(); - } - } - - /** - * - * Add a worker function to the queue. - * - * Worker will be called to execute jobs associated with jobName. - * - * Worker function will receive job id and job payload as parameters. - * - * Example: - * - * function exampleJobWorker(id, payload) { - * console.log(id); // UUID of job. - * console.log(payload); // Payload of data related to job. - * } - * - * @param jobName {string} - Name associated with jobs assigned to this worker. - * @param worker {function} - The worker function that will execute jobs. - * @param options {object} - Worker options. See README.md for worker options info. - */ - addWorker(jobName, worker, options = {}) { - this.worker.addWorker(jobName, worker, options); - } - - /** - * - * Delete worker function from queue. - * - * @param jobName {string} - Name associated with jobs assigned to this worker. - */ - removeWorker(jobName) { - this.worker.removeWorker(jobName); - } - - /** - * - * Creates a new job and adds it to queue. - * - * Queue will automatically start processing unless startQueue param is set to false. - * - * @param name {string} - Name associated with job. The worker function assigned to this name will be used to execute this job. - * @param payload {object} - Object of arbitrary data to be passed into worker function when job executes. - * @param options {object} - Job related options like timeout etc. See README.md for job options info. - * @param startQueue - {boolean} - Whether or not to immediately begin prcessing queue. If false queue.start() must be manually called. - */ - createJob(name, payload = {}, options = {}, startQueue = true) { - - if (!name) { - throw new Error('Job name must be supplied.'); + /** + * + * Set initial class properties. + * + * @constructor + */ + constructor() { + this.realm = null; + this.worker = new Worker(); + this.status = 'inactive'; } - // Validate options - if (options.timeout < 0 || options.attempts < 0) { - throw new Error('Invalid job option.'); + /** + * + * Initializes the queue by connecting to Realm database. + * + */ + async init() { + if (this.realm === null) { + this.realm = await Database.getRealmInstance(); + } } - this.realm.write(() => { - - this.realm.create('Job', { - id: uuid.v4(), - name, - payload: JSON.stringify(payload), - data: JSON.stringify({ - attempts: options.attempts || 1 - }), - priority: options.priority || 0, - active: false, - timeout: (options.timeout >= 0) ? options.timeout : 25000, - created: new Date(), - failed: null - }); - - }); - - // Start queue on job creation if it isn't running by default. - if (startQueue && this.status == 'inactive') { - this.start(); + /** + * + * Add a worker function to the queue. + * + * Worker will be called to execute jobs associated with jobName. + * + * Worker function will receive job id and job payload as parameters. + * + * Example: + * + * function exampleJobWorker(id, payload) { + * console.log(id); // UUID of job. + * console.log(payload); // Payload of data related to job. + * } + * + * @param jobName {string} - Name associated with jobs assigned to this worker. + * @param worker {function} - The worker function that will execute jobs. + * @param options {object} - Worker options. See README.md for worker options info. + */ + addWorker(jobName, worker, options = {}) { + this.worker.addWorker(jobName, worker, options); } - } - - /** - * - * Start processing the queue. - * - * If queue was not started automatically during queue.createJob(), this - * method should be used to manually start the queue. - * - * If queue.start() is called again when queue is already running, - * queue.start() will return early with a false boolean value instead - * of running multiple queue processing loops concurrently. - * - * Lifespan can be passed to start() in order to run the queue for a specific amount of time before stopping. - * This is useful, as an example, for OS background tasks which typically are time limited. - * - * NOTE: If lifespan is set, only jobs with a timeout property at least 500ms less than remaining lifespan will be processed - * during queue processing lifespan. This is to buffer for the small amount of time required to query Realm for suitable - * jobs, and to mark such jobs as complete or failed when job finishes processing. - * - * IMPORTANT: Jobs with timeout set to 0 that run indefinitely will not be processed if the queue is running with a lifespan. - * - * @param lifespan {number} - If lifespan is passed, the queue will start up and run for lifespan ms, then queue will be stopped. - * @return {boolean|undefined} - False if queue is already started. Otherwise nothing is returned when queue finishes processing. - */ - async start(lifespan = 0) { - - // If queue is already running, don't fire up concurrent loop. - if (this.status == 'active') { - return false; + /** + * + * Delete worker function from queue. + * + * @param jobName {string} - Name associated with jobs assigned to this worker. + */ + removeWorker(jobName) { + this.worker.removeWorker(jobName); } - this.status = 'active'; + /** + * + * Creates a new job and adds it to queue. + * + * Queue will automatically start processing unless startQueue param is set to false. + * + * @param name {string} - Name associated with job. The worker function assigned to this name will be used to execute this job. + * @param payload {object} - Object of arbitrary data to be passed into worker function when job executes. + * @param options {object} - Job related options like timeout etc. See README.md for job options info. + * @param startQueue - {boolean} - Whether or not to immediately begin prcessing queue. If false queue.start() must be manually called. + */ + createJob(name, payload = {}, options = {}, startQueue = true) { + + if (!name) { + throw new Error('Job name must be supplied.'); + } + + // Validate options + if (options.timeout < 0 || options.attempts < 0) { + throw new Error('Invalid job option.'); + } + + this.realm.write(() => { + + this.realm.create('Job', { + id: uuid.v4(), + name, + payload: JSON.stringify(payload), + data: JSON.stringify({ + attempts: options.attempts || 1 + }), + priority: options.priority || 0, + active: false, + timeout: (options.timeout >= 0) ? options.timeout : 25000, + created: new Date(), + failed: null + }); - // Get jobs to process - const startTime = Date.now(); - let lifespanRemaining = null; - let concurrentJobs = []; + }); + + // Start queue on job creation if it isn't running by default. + if (startQueue && this.status == 'inactive') { + this.start(); + } - if (lifespan !== 0) { - lifespanRemaining = lifespan - (Date.now() - startTime); - lifespanRemaining = (lifespanRemaining === 0) ? -1 : lifespanRemaining; // Handle exactly zero lifespan remaining edge case. - concurrentJobs = await this.getConcurrentJobs(lifespanRemaining); - } else { - concurrentJobs = await this.getConcurrentJobs(); } - while (this.status == 'active' && concurrentJobs.length) { + /** + * + * Start processing the queue. + * + * If queue was not started automatically during queue.createJob(), this + * method should be used to manually start the queue. + * + * If queue.start() is called again when queue is already running, + * queue.start() will return early with a false boolean value instead + * of running multiple queue processing loops concurrently. + * + * Lifespan can be passed to start() in order to run the queue for a specific amount of time before stopping. + * This is useful, as an example, for OS background tasks which typically are time limited. + * + * NOTE: If lifespan is set, only jobs with a timeout property at least 500ms less than remaining lifespan will be processed + * during queue processing lifespan. This is to buffer for the small amount of time required to query Realm for suitable + * jobs, and to mark such jobs as complete or failed when job finishes processing. + * + * IMPORTANT: Jobs with timeout set to 0 that run indefinitely will not be processed if the queue is running with a lifespan. + * + * @param lifespan {number} - If lifespan is passed, the queue will start up and run for lifespan ms, then queue will be stopped. + * @return {boolean|undefined} - False if queue is already started. Otherwise nothing is returned when queue finishes processing. + */ + async start(lifespan = 0) { + + // If queue is already running, don't fire up concurrent loop. + if (this.status == 'active') { + return false; + } - // Loop over jobs and process them concurrently. - const processingJobs = concurrentJobs.map( job => { - return this.processJob(job); - }); + this.status = 'active'; - // Promise Reflect ensures all processingJobs resolve so - // we don't break await early if one of the jobs fails. - await Promise.all(processingJobs.map(promiseReflect)); + // Get jobs to process + const startTime = Date.now(); + let lifespanRemaining = null; + let concurrentJobs = []; - // Get next batch of jobs. - if (lifespan !== 0) { - lifespanRemaining = lifespan - (Date.now() - startTime); - lifespanRemaining = (lifespanRemaining === 0) ? -1 : lifespanRemaining; // Handle exactly zero lifespan remaining edge case. - concurrentJobs = await this.getConcurrentJobs(lifespanRemaining); - } else { - concurrentJobs = await this.getConcurrentJobs(); - } + if (lifespan !== 0) { + lifespanRemaining = lifespan - (Date.now() - startTime); + lifespanRemaining = (lifespanRemaining === 0) ? -1 : lifespanRemaining; // Handle exactly zero lifespan remaining edge case. + concurrentJobs = await this.getConcurrentJobs(lifespanRemaining); + } else { + concurrentJobs = await this.getConcurrentJobs(); + } - } + while (this.status == 'active' && concurrentJobs.length) { + + // Loop over jobs and process them concurrently. + const processingJobs = concurrentJobs.map(job => { + return this.processJob(job); + }); + + // Promise Reflect ensures all processingJobs resolve so + // we don't break await early if one of the jobs fails. + await Promise.all(processingJobs.map(promiseReflect)); + + // Get next batch of jobs. + if (lifespan !== 0) { + lifespanRemaining = lifespan - (Date.now() - startTime); + lifespanRemaining = (lifespanRemaining === 0) ? -1 : lifespanRemaining; // Handle exactly zero lifespan remaining edge case. + concurrentJobs = await this.getConcurrentJobs(lifespanRemaining); + } else { + concurrentJobs = await this.getConcurrentJobs(); + } + + } + + this.status = 'inactive'; - this.status = 'inactive'; + } - } + /** + * + * Stop processing queue. + * + * If queue.stop() is called, queue will stop processing until + * queue is restarted by either queue.createJob() or queue.start(). + * + */ + stop() { + this.status = 'inactive'; + } - /** - * - * Stop processing queue. - * - * If queue.stop() is called, queue will stop processing until - * queue is restarted by either queue.createJob() or queue.start(). - * - */ - stop() { - this.status = 'inactive'; - } + /** + * + * Get a collection of all the jobs in the queue. + * + * @param sync {boolean} - This should be true if you want to guarantee job data is fresh. Otherwise you could receive job data that is not up to date if a write transaction is occuring concurrently. + * @return {promise} - Promise that resolves to a collection of all the jobs in the queue. + */ + async getJobs(sync = false) { - /** - * - * Get a collection of all the jobs in the queue. - * - * @param sync {boolean} - This should be true if you want to guarantee job data is fresh. Otherwise you could receive job data that is not up to date if a write transaction is occuring concurrently. - * @return {promise} - Promise that resolves to a collection of all the jobs in the queue. - */ - async getJobs(sync = false) { + if (sync) { - if (sync) { + let jobs = null; + this.realm.write(() => { - let jobs = null; - this.realm.write(() => { + jobs = this.realm.objects('Job'); - jobs = this.realm.objects('Job'); + }); - }); + return jobs; - return jobs; + } else { + return await this.realm.objects('Job'); + } - } else { - return await this.realm.objects('Job'); } - } + /** + * + * Get the next job(s) that should be processed by the queue. + * + * If the next job to be processed by the queue is associated with a + * worker function that has concurrency X > 1, then X related (jobs with same name) + * jobs will be returned. + * + * If queue is running with a lifespan, only jobs with timeouts at least 500ms < than REMAINING lifespan + * AND a set timeout (ie timeout > 0) will be returned. See Queue.start() for more info. + * + * @param queueLifespanRemaining {number} - The remaining lifespan of the current queue process (defaults to indefinite). + * @return {promise} - Promise resolves to an array of job(s) to be processed next by the queue. + */ + async getConcurrentJobs(queueLifespanRemaining = 0) { - /** - * - * Get the next job(s) that should be processed by the queue. - * - * If the next job to be processed by the queue is associated with a - * worker function that has concurrency X > 1, then X related (jobs with same name) - * jobs will be returned. - * - * If queue is running with a lifespan, only jobs with timeouts at least 500ms < than REMAINING lifespan - * AND a set timeout (ie timeout > 0) will be returned. See Queue.start() for more info. - * - * @param queueLifespanRemaining {number} - The remaining lifespan of the current queue process (defaults to indefinite). - * @return {promise} - Promise resolves to an array of job(s) to be processed next by the queue. - */ - async getConcurrentJobs(queueLifespanRemaining = 0) { + let concurrentJobs = []; - let concurrentJobs = []; + this.realm.write(() => { - this.realm.write(() => { + // Get next job from queue. + let nextJob = null; - // Get next job from queue. - let nextJob = null; + // Build query string + // If queueLife + const timeoutUpperBound = (queueLifespanRemaining - 500 > 0) ? queueLifespanRemaining - 499 : 0; // Only get jobs with timeout at least 500ms < queueLifespanRemaining. - // Build query string - // If queueLife - const timeoutUpperBound = (queueLifespanRemaining - 500 > 0) ? queueLifespanRemaining - 499 : 0; // Only get jobs with timeout at least 500ms < queueLifespanRemaining. + const initialQuery = (queueLifespanRemaining) + ? 'active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound + : 'active == FALSE AND failed == null'; - const initialQuery = (queueLifespanRemaining) - ? 'active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound - : 'active == FALSE AND failed == null'; + let jobs = this.realm.objects('Job') + .filtered(initialQuery) + .sorted([['priority', true], ['created', false]]); - let jobs = this.realm.objects('Job') - .filtered(initialQuery) - .sorted([['priority', true], ['created', false]]); + if (jobs.length) { + nextJob = jobs[0]; + } - if (jobs.length) { - nextJob = jobs[0]; - } + // If next job exists, get concurrent related jobs appropriately. + if (nextJob) { - // If next job exists, get concurrent related jobs appropriately. - if (nextJob) { + const concurrency = this.worker.getConcurrency(nextJob.name); - const concurrency = this.worker.getConcurrency(nextJob.name); + const allRelatedJobsQuery = (queueLifespanRemaining) + ? 'name == "' + nextJob.name + '" AND active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound + : 'name == "' + nextJob.name + '" AND active == FALSE AND failed == null'; - const allRelatedJobsQuery = (queueLifespanRemaining) - ? 'name == "'+ nextJob.name +'" AND active == FALSE AND failed == null AND timeout > 0 AND timeout < ' + timeoutUpperBound - : 'name == "'+ nextJob.name +'" AND active == FALSE AND failed == null'; + const allRelatedJobs = this.realm.objects('Job') + .filtered(allRelatedJobsQuery) + .sorted([['priority', true], ['created', false]]); - const allRelatedJobs = this.realm.objects('Job') - .filtered(allRelatedJobsQuery) - .sorted([['priority', true], ['created', false]]); + let jobsToMarkActive = allRelatedJobs.slice(0, concurrency); - let jobsToMarkActive = allRelatedJobs.slice(0, concurrency); + // Grab concurrent job ids to reselect jobs as marking these jobs as active will remove + // them from initial selection when write transaction exits. + // See: https://stackoverflow.com/questions/47359368/does-realm-support-select-for-update-style-read-locking/47363356#comment81772710_47363356 + const concurrentJobIds = jobsToMarkActive.map(job => job.id); - // Grab concurrent job ids to reselect jobs as marking these jobs as active will remove - // them from initial selection when write transaction exits. - // See: https://stackoverflow.com/questions/47359368/does-realm-support-select-for-update-style-read-locking/47363356#comment81772710_47363356 - const concurrentJobIds = jobsToMarkActive.map( job => job.id); + // Mark concurrent jobs as active + jobsToMarkActive = jobsToMarkActive.map(job => { + job.active = true; + }); - // Mark concurrent jobs as active - jobsToMarkActive = jobsToMarkActive.map( job => { - job.active = true; - }); + // Reselect now-active concurrent jobs by id. + const reselectQuery = concurrentJobIds.map(jobId => 'id == "' + jobId + '"').join(' OR '); + const reselectedJobs = this.realm.objects('Job') + .filtered(reselectQuery) + .sorted([['priority', true], ['created', false]]); - // Reselect now-active concurrent jobs by id. - const reselectQuery = concurrentJobIds.map( jobId => 'id == "' + jobId + '"').join(' OR '); - const reselectedJobs = this.realm.objects('Job') - .filtered(reselectQuery) - .sorted([['priority', true], ['created', false]]); + concurrentJobs = reselectedJobs.slice(0, concurrency); - concurrentJobs = reselectedJobs.slice(0, concurrency); + } - } + }); - }); + return concurrentJobs; - return concurrentJobs; + } - } + /** + * + * Process a job. + * + * Job lifecycle callbacks are called as appropriate throughout the job processing lifecycle. + * + * Job is deleted upon successful completion. + * + * If job fails execution via timeout or other exception, error will be + * logged to job.data.errors array and job will be reset to inactive status. + * Job will be re-attempted up to the specified "attempts" setting (defaults to 1), + * after which it will be marked as failed and not re-attempted further. + * + * @param job {object} - Job realm model object + */ + async processJob(job) { - /** - * - * Process a job. - * - * Job lifecycle callbacks are called as appropriate throughout the job processing lifecycle. - * - * Job is deleted upon successful completion. - * - * If job fails execution via timeout or other exception, error will be - * logged to job.data.errors array and job will be reset to inactive status. - * Job will be re-attempted up to the specified "attempts" setting (defaults to 1), - * after which it will be marked as failed and not re-attempted further. - * - * @param job {object} - Job realm model object - */ - async processJob(job) { + // Data must be cloned off the realm job object for several lifecycle callbacks to work correctly. + // This is because realm job is deleted before some callbacks are called if job processed successfully. + // More info: https://github.com/billmalarky/react-native-queue/issues/2#issuecomment-361418965 + const jobName = job.name; + const jobId = job.id; + const jobPayload = JSON.parse(job.payload); - // Data must be cloned off the realm job object for several lifecycle callbacks to work correctly. - // This is because realm job is deleted before some callbacks are called if job processed successfully. - // More info: https://github.com/billmalarky/react-native-queue/issues/2#issuecomment-361418965 - const jobName = job.name; - const jobId = job.id; - const jobPayload = JSON.parse(job.payload); + // Fire onStart job lifecycle callback + this.worker.executeJobLifecycleCallback('onStart', jobName, jobId, jobPayload); - // Fire onStart job lifecycle callback - this.worker.executeJobLifecycleCallback('onStart', jobName, jobId, jobPayload); + try { - try { + await this.worker.executeJob(job); - await this.worker.executeJob(job); + // On successful job completion, remove job + this.realm.write(() => { - // On successful job completion, remove job - this.realm.write(() => { + this.realm.delete(job); - this.realm.delete(job); + }); - }); + // Job has processed successfully, fire onSuccess and onComplete job lifecycle callbacks. + this.worker.executeJobLifecycleCallback('onSuccess', jobName, jobId, jobPayload); + this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload); - // Job has processed successfully, fire onSuccess and onComplete job lifecycle callbacks. - this.worker.executeJobLifecycleCallback('onSuccess', jobName, jobId, jobPayload); - this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload); + } catch (error) { - } catch (error) { + // Handle job failure logic, including retries. + let jobData = JSON.parse(job.data); - // Handle job failure logic, including retries. - let jobData = JSON.parse(job.data); + this.realm.write(() => { - this.realm.write(() => { + // Increment failed attempts number + if (!jobData.failedAttempts) { + jobData.failedAttempts = 1; + } else { + jobData.failedAttempts++; + } - // Increment failed attempts number - if (!jobData.failedAttempts) { - jobData.failedAttempts = 1; - } else { - jobData.failedAttempts++; - } + // Log error + if (!jobData.errors) { + jobData.errors = [error.message]; + } else { + jobData.errors.push(error.message); + } - // Log error - if (!jobData.errors) { - jobData.errors = [ error.message ]; - } else { - jobData.errors.push(error.message); - } + job.data = JSON.stringify(jobData); - job.data = JSON.stringify(jobData); + // Reset active status + job.active = false; - // Reset active status - job.active = false; + // Mark job as failed if too many attempts + if (jobData.failedAttempts >= jobData.attempts) { + job.failed = new Date(); + } - // Mark job as failed if too many attempts - if (jobData.failedAttempts >= jobData.attempts) { - job.failed = new Date(); - } + }); - }); + // Execute job onFailure lifecycle callback. + this.worker.executeJobLifecycleCallback('onFailure', jobName, jobId, jobPayload); - // Execute job onFailure lifecycle callback. - this.worker.executeJobLifecycleCallback('onFailure', jobName, jobId, jobPayload); + // If job has failed all attempts execute job onFailed and onComplete lifecycle callbacks. + if (jobData.failedAttempts >= jobData.attempts) { + this.worker.executeJobLifecycleCallback('onFailed', jobName, jobId, jobPayload); + this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload); + } - // If job has failed all attempts execute job onFailed and onComplete lifecycle callbacks. - if (jobData.failedAttempts >= jobData.attempts) { - this.worker.executeJobLifecycleCallback('onFailed', jobName, jobId, jobPayload); - this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload); - } + } } - } + removeJob() { - /** - * - * Delete jobs in the queue. - * - * If jobName is supplied, only jobs associated with that name - * will be deleted. Otherwise all jobs in queue will be deleted. - * - * @param jobName {string} - Name associated with job (and related job worker). - */ - flushQueue(jobName = null) { + } - if (jobName) { + /** + * + * Delete jobs in the queue. + * + * If jobName is supplied, only jobs associated with that name + * will be deleted. Otherwise all jobs in queue will be deleted. + * + * @param jobName {string} - Name associated with job (and related job worker). + */ + flushQueue(jobName = null) { - this.realm.write(() => { + if (jobName) { - let jobs = this.realm.objects('Job') - .filtered('name == "' + jobName + '"'); + this.realm.write(() => { - if (jobs.length) { - this.realm.delete(jobs); - } + let jobs = this.realm.objects('Job') + .filtered('name == "' + jobName + '"'); - }); + if (jobs.length) { + this.realm.delete(jobs); + } - } else { - this.realm.write(() => { + }); - this.realm.deleteAll(); + } else { + this.realm.write(() => { - }); - } + this.realm.deleteAll(); - } + }); + } + } } @@ -439,9 +442,9 @@ export class Queue { */ export default async function queueFactory() { - const queue = new Queue(); - await queue.init(); + const queue = new Queue(); + await queue.init(); - return queue; + return queue; } From 380d311d015fe7cd611e920f53d12edc60df5298 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 12:47:33 -0300 Subject: [PATCH 02/14] Add empty service; --- Models/Queue.js | 4 ++++ Models/Service.js | 1 + 2 files changed, 5 insertions(+) create mode 100644 Models/Service.js diff --git a/Models/Queue.js b/Models/Queue.js index 5386f1d..cb6824d 100644 --- a/Models/Queue.js +++ b/Models/Queue.js @@ -394,6 +394,10 @@ export class Queue { } + removeJob() { + + } + /** * * Delete jobs in the queue. diff --git a/Models/Service.js b/Models/Service.js new file mode 100644 index 0000000..2b21e74 --- /dev/null +++ b/Models/Service.js @@ -0,0 +1 @@ +/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file From b7769cf1c7089280e8e79b40af7f8afbce82d5ac Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:00:28 -0300 Subject: [PATCH 03/14] Fix --- Models/Service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Models/Service.js b/Models/Service.js index 2b21e74..40dc8d7 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -1 +1 @@ -/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file +/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file From 3fe1175a5c81f3239a00bc5ab458a8d7a7a8f5ad Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:05:26 -0300 Subject: [PATCH 04/14] Fix --- Models/Service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Models/Service.js b/Models/Service.js index 40dc8d7..ca88aa0 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -1 +1 @@ -/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file +/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ \ No newline at end of file From 1e9f20c33ee3a40628c611f4c6d3ca6741b47b75 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:05:53 -0300 Subject: [PATCH 05/14] Fix --- Models/Service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Models/Service.js b/Models/Service.js index ca88aa0..e69de29 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -1 +0,0 @@ -/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ \ No newline at end of file From 50cff6f8b43aac83150cf7d1066329cd07dff55f Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:06:13 -0300 Subject: [PATCH 06/14] Fix --- Models/Service.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Models/Service.js b/Models/Service.js index e69de29..cd90125 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -0,0 +1 @@ +/** * */ \ No newline at end of file From 8ec167c0fb392773ea0d9055baf7da182fffcf08 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:12:30 -0300 Subject: [PATCH 07/14] Saving files before refreshing line endings --- Models/Service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Models/Service.js b/Models/Service.js index cd90125..40dc8d7 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -1 +1 @@ -/** * */ \ No newline at end of file +/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file From 7dccff19e972247b26191c75f2869290a3f5b6e3 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:20:24 -0300 Subject: [PATCH 08/14] Saving files before refreshing line endings --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7b9def7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +# Set the default behavior, in case people don't have core.autocrlf set. * text=auto # Declare files that will always have CRLF line endings on checkout. *.sln text eol=crlf \ No newline at end of file From 7e2860baa896613c71c52447ad5398d85a9af646 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sun, 16 Sep 2018 13:21:04 -0300 Subject: [PATCH 09/14] Fix --- Models/Service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Models/Service.js b/Models/Service.js index 40dc8d7..2b21e74 100644 --- a/Models/Service.js +++ b/Models/Service.js @@ -1 +1 @@ -/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file +/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file From fb5e196d8b382f5a87d6ddc572ca61d6dc448763 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Sat, 22 Sep 2018 15:10:37 -0300 Subject: [PATCH 10/14] Implemented a function that flush a job by id; --- Models/Queue.js | 12 +++++++++++- Models/Service.js | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) delete mode 100644 Models/Service.js diff --git a/Models/Queue.js b/Models/Queue.js index b31fb32..69c7380 100644 --- a/Models/Queue.js +++ b/Models/Queue.js @@ -394,8 +394,18 @@ export class Queue { } - removeJob() { + flushJob(jobId) { + this.realm.write(() => { + + let jobs = this.realm.objects('Job') + .filtered('id == "' + jobId + '"'); + + if (jobs.length) { + this.realm.delete(jobs); + } + + }); } /** diff --git a/Models/Service.js b/Models/Service.js deleted file mode 100644 index 2b21e74..0000000 --- a/Models/Service.js +++ /dev/null @@ -1 +0,0 @@ -/** * @author Haroldo Shigueaki Teruya * @version 1.0.0 */ //========================================================================== // IMPORTS import {} from "react-native"; //========================================================================== const TAG = 'Service'; /** * @class * @classdesc */ class Service { //========================================================================== // CONSTRUCTOR constructor() { } //========================================================================== // METHODS } /** * @module Service */ module.exports = new Service(); \ No newline at end of file From 8c03c74289dee78d4da3624898345c69cf4a9d6f Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Tue, 25 Sep 2018 16:19:05 -0300 Subject: [PATCH 11/14] Implemented the function to remove a job from the queue by his id; --- Models/Queue.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Models/Queue.js b/Models/Queue.js index 69c7380..c076213 100644 --- a/Models/Queue.js +++ b/Models/Queue.js @@ -21,6 +21,7 @@ export class Queue { * @constructor */ constructor() { + this.TAG = 'Queue'; this.realm = null; this.worker = new Worker(); this.status = 'inactive'; @@ -394,18 +395,28 @@ export class Queue { } + /** + * + * Delete a job in the queue with jobId. + * + * @param jobId {string} - id associated with job. + */ flushJob(jobId) { - - this.realm.write(() => { - - let jobs = this.realm.objects('Job') - .filtered('id == "' + jobId + '"'); - - if (jobs.length) { - this.realm.delete(jobs); + try { + if (jobId) { + this.realm.write(() => { + let jobs = this.realm + .objects('Job') + .filtered('id == "' + jobId + '"'); + if (jobs.length) { + this.realm.delete(jobs); + return; + } + }); } - - }); + } catch (e) { + console.log(this.TAG, 'flushJob failed', jobId); + } } /** From 7401d061c0075d04850110159bb5ee7f2caf88b5 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Tue, 25 Sep 2018 16:40:27 -0300 Subject: [PATCH 12/14] Bump version from 1.2.1 to 1.2.2; --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8d72126..20cbea8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-queue", - "version": "1.2.1", + "version": "1.2.2", "description": "A React Native Job Queue", "main": "index.js", "scripts": { From 9b6dec7fffc58e15a258a05c8b72fde66dd95db7 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Thu, 27 Sep 2018 17:35:07 -0300 Subject: [PATCH 13/14] Update react-native-queue and and remove re scheduling job; --- Models/Queue.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Models/Queue.js b/Models/Queue.js index c076213..3c7e454 100644 --- a/Models/Queue.js +++ b/Models/Queue.js @@ -339,11 +339,11 @@ export class Queue { await this.worker.executeJob(job); // On successful job completion, remove job - this.realm.write(() => { - - this.realm.delete(job); - - }); + // this.realm.write(() => { + // + // this.realm.delete(job); + // + // }); // Job has processed successfully, fire onSuccess and onComplete job lifecycle callbacks. this.worker.executeJobLifecycleCallback('onSuccess', jobName, jobId, jobPayload); From 676a75af8a53cbb0c9604560978500cf6717dc06 Mon Sep 17 00:00:00 2001 From: HaroldoTeruya Date: Thu, 27 Sep 2018 22:17:14 -0300 Subject: [PATCH 14/14] Bump version from 1.2.2 to 1.2.3; --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 20cbea8..e2effe7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-queue", - "version": "1.2.2", + "version": "1.2.3", "description": "A React Native Job Queue", "main": "index.js", "scripts": {