Skip to content

Commit 6874e10

Browse files
authored
Feature/scheduled automations (#44)
* change cron job algorithm * update readme
1 parent a9ffcb8 commit 6874e10

File tree

12 files changed

+54
-24
lines changed

12 files changed

+54
-24
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ in json to provide additional functionality. The functionality aids in adding fl
99
custom code or complexity. This product is meant to simplify digital archiving by providing tools that can automate most
1010
of the collection process.
1111

12+
# Privacy Policy
13+
We do not collect your data, share your data , store your data or even handle your data. All your data stays on your computer. Please see our privacy policy and feel free to reach
14+
out with any suggestions on improvements. https://bakerstreet.llc/your-rapport-privacy-policy/
15+
16+
**This is an open source project, which strives to prioritize your privacy.**
17+
1218
### Getting Started
1319
After installing the Chrome extension from https://chromewebstore.google.com/detail/your-rapport/clkaalonjdkliiaadkgodlfbiipidjmn,
1420
"Your Rapport" will automatically be ready to collect.
@@ -40,7 +46,7 @@ Check out the wiki for more in depth information. Or review the source code in t
4046

4147
# Technical and Product Roadmap
4248

43-
Below are the stages used to process collected information, commonly referred to as an ETL pipeline. This workflow is
49+
Below are the stages used to process collected information, that stays locally on your machine, commonly referred to as an ETL pipeline. This workflow is
4450
designed to run entirely inside a Chrome extension. Discovery Plugins connect events to actions. For example, you might
4551
save a web page, an MHTML file, an image, or a snippet of JavaScript and then trigger a scan by a remote service to
4652
extract people, places, or names from the text, or run OCR to pull text from an image. The extension collects the

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "your-rapport",
3-
"version": "0.21.0",
3+
"version": "0.22.0",
44
"description": "A chrome extension for saving screenshots and making them searchable.",
55
"license": "Commercial",
66
"scripts": {

src/backgrounds/automation-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function complete(queuedJob: BulkAutomationUrl) {
4545
queuedJob.status = 'done';
4646
queuedJob.leaseUntil = null;
4747
queuedJob.completedOn = new Date().getTime();
48-
queuedJob.description = 'Completed Successfully: '+ queuedJob.description ?? '';
48+
queuedJob.description = 'Completed Successfully';
4949
await db.bulkAutomation.put(queuedJob);
5050
}
5151

src/backgrounds/automation-runner.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { db } from '../models/db/dexieDb';
1111
import BulkAutomationUrl from '../models/schemas/BulkAutomationUrl';
1212
import { NoChangeDetectedError } from '../errors/NoChangeDetectedError';
1313
import { Tag } from '../models/schemas/Tag';
14-
import { getUtcNow } from '../utilities/transformers';
14+
import { getUtc, getUtcNow } from '../utilities/transformers';
1515

1616

1717
let processing: boolean = false;
@@ -50,19 +50,20 @@ export function initializeAutomationRunner() {
5050
}
5151

5252
async function queueScheduledAutomations(){
53+
5354
// upon trigger, we need to check for any active scheduled automations that must be run by checking their crontab evaluation and generate
5455
// any associated bulk automations
5556
db.transaction('rw', db.bulkAutomation, db.scheduledAutomation, async () => {
5657
processing = true;
5758
const scheduledAutomations: ScheduledAutomation[] = await db.scheduledAutomation.filter(scheduled => scheduled.active).toArray();
58-
const utcNow: Date = new Date();
59+
const utcNow: number = getUtcNow();
5960
debug('Scheduling automations', {scheduledAutomations});
60-
utcNow.setSeconds(0, 0);
6161
const automations: BulkAutomationUrl[] = [];
6262
for (const scheduledAutomation of scheduledAutomations) {
6363
const interval = CronExpressionParser.parse(scheduledAutomation.crontab);
64-
if(interval.includesDate(utcNow)){
65-
// TODO: queue the job
64+
// run automation
65+
if(!scheduledAutomation?.nextRunOn || scheduledAutomation.nextRunOn <= utcNow) {
66+
// add bulk automation url
6667
const automation = BulkAutomationUrl.createBulkAutomationJob(scheduledAutomation.url, {
6768
keepTabOpen: scheduledAutomation.keepTabOpen ?? false,
6869
isDeepSave: scheduledAutomation.isDeepSave ?? true,
@@ -73,7 +74,14 @@ async function queueScheduledAutomations(){
7374
// MUST be set to active to trigger running in the automation queue
7475
automation.active = true;
7576
automations.push(automation)
76-
scheduledAutomation.lastRanOn = getUtcNow();
77+
scheduledAutomation.prevRanOn = utcNow;
78+
if(interval.hasNext()){
79+
scheduledAutomation.nextRunOn = getUtc(interval.next().toDate());
80+
}
81+
else{
82+
// deactivate the scheduled automation if no future time exists
83+
scheduledAutomation.active = false;
84+
}
7785
await db.scheduledAutomation.put(scheduledAutomation);
7886
}
7987
}
@@ -82,6 +90,7 @@ async function queueScheduledAutomations(){
8290
await db.bulkAutomation.bulkAdd(automations);
8391
return automations
8492
}
93+
return []
8594
}).then((automations) => {
8695
if(automations?.length ?? [].length > 0){
8796
debug('The following bulk automations were scheduled', {automations})
@@ -173,7 +182,9 @@ async function processQueue() {
173182
; // do nothing, no change was detected
174183
job.description = 'No Change Detected';
175184
await complete(job)
176-
} else {
185+
}
186+
else {
187+
await ('automationRunner:failed ' + String(e), {job})
177188
await fail(job, String(e?.message ?? e));
178189
}
179190
} finally {

src/components/tables/ScheduledAutomationDataTable.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ export default function ScheduledAutomationDataTable(): JSX.Element {
271271
try {
272272
const now = new Date()
273273
const interval = CronExpressionParser.parse(record.crontab);
274+
scheduledAutomation.nextRunOn = interval.next().getTime();
274275
debug(interval.fields.minute);
275276
if(interval.includesDate(now)){
276277
debug('now '+ now.toISOString());

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"default_title": "Your Rapport is a screenshot tool with auto scroll capabilities. Right click, select Collect or Alt+S to save. Alt+A for auto scrolling captures. Alt+X open the dashboard. Alt+Q for doing a quick scan.",
1010
"default_popup": "popup.html"
1111
},
12-
"version": "0.21.0",
12+
"version": "0.22.0",
1313
"icons": {
1414
"128": "icon-128.png",
1515
"48": "icon-48.png",

src/models/schemas/BulkAutomationUrl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IBulkAutomationRecord } from '../../types';
22
import { ScheduledAutomation } from './ScheduledAutomation';
33
import { DiscoveryPlugin } from './DiscoveryPlugin';
4+
import { getUtc, getUtcNow } from '../../utilities/transformers';
45

56
export default class BulkAutomationUrl implements IBulkAutomationRecord {
67
uuid: string;
@@ -9,7 +10,7 @@ export default class BulkAutomationUrl implements IBulkAutomationRecord {
910
value: number;
1011
screenShotsCollected: number;
1112
keepTabOpen: boolean;
12-
createdOn: Date;
13+
createdOn: number;
1314
ranOn: number | null;
1415
completedOn: number | null;
1516
description: string | null;
@@ -33,7 +34,7 @@ export default class BulkAutomationUrl implements IBulkAutomationRecord {
3334
this.value = 0; // sane default
3435
this.screenShotsCollected = 0;
3536
this.keepTabOpen = false;
36-
this.createdOn = new Date(); // matches type Date
37+
this.createdOn = getUtcNow();
3738
this.ranOn = null;
3839
this.completedOn = null;
3940
this.description = null;

src/models/schemas/ScheduledAutomation.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { getUtcNow } from '../../utilities/transformers';
1+
import { getUtc, getUtcNow } from '../../utilities/transformers';
22
import { ChangeDetection } from '../../types';
33
import { DiscoveryPlugin } from './DiscoveryPlugin';
44
import { db } from '../db/dexieDb';
55
import { debug } from '../../services/logger_services';
6+
import { CronExpressionParser } from 'cron-parser';
67

78
export class ScheduledAutomation {
89
uuid: string;
@@ -20,7 +21,8 @@ export class ScheduledAutomation {
2021
enableSelectorChangeDetector: boolean;
2122
enableTextChangeDetector: boolean;
2223
onlySaveOnChange: boolean;
23-
lastRanOn: number | null;
24+
prevRanOn: number | null;
25+
nextRunOn: number | null;
2426
lastError: string | null;
2527
discoveryPlugin: DiscoveryPlugin | null;
2628
tags: string[];
@@ -31,18 +33,19 @@ export class ScheduledAutomation {
3133
this.unit = 'count';
3234
this.value = 100;
3335
this.keepTabOpen = false;
34-
this.createdOn = getUtcNow(); // matches type Date
36+
this.createdOn = getUtcNow();
3537
this.active = true;
3638
this.isDeepSave = true;
3739
this.changeDetectors = [];
3840
this.onlySaveOnChange = true;
3941
this.enableImageChangeDetector = true;
4042
this.enableSelectorChangeDetector = true;
4143
this.enableTextChangeDetector = true;
42-
this.lastRanOn = null;
44+
this.prevRanOn = null;
45+
this.nextRunOn = null;
4346
this.runInServiceWorker = false;
4447
this.lastError = null
45-
this.crontab = '0 * * * * *';
48+
this.crontab = '0 0 * * * *';
4649
this.discoveryPlugin = null;
4750
this.tags = [];
4851
this.caseManagementUuid = "30583002-f730-4383-bf28-fdd8aadcf387"
@@ -59,6 +62,11 @@ export class ScheduledAutomation {
5962
const scheduledAutomation = new ScheduledAutomation();
6063
scheduledAutomation.url = url;
6164
scheduledAutomation.crontab = crontab;
65+
const interval = CronExpressionParser.parse(crontab);
66+
67+
if(interval.hasNext()){
68+
scheduledAutomation.nextRunOn = getUtc(interval.next().toDate());
69+
}
6270

6371
await db.transaction("rw", db.scheduledAutomation, async () => {
6472
try {

src/services/logger_services.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export async function debug(
1515
try {
1616

1717
const tsMessage = `[${new Date().toISOString()}] `+message
18-
1918
const value: boolean = await Configuration.getConfigurationValue<boolean>(
2019
'debugMessagesEnabled',
2120
true

0 commit comments

Comments
 (0)