Skip to content

Commit

Permalink
feat: add trigger name to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 25, 2023
1 parent 0c18906 commit 5b68643
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ void watch({
// If {interruptible: false}, then Watchrow will wait until the onChange routine completes.
// Defaults to true.
interruptible: false,
// Name of the trigger. Used for debugging
// Must match /^[a-z0-9-_]+$/ pattern and must be unique.
name: 'build',
// Routine that is executed when file changes are detected.
onChange: async ({ spawn }: ChangeEvent) => {
await spawn`tsc`;
Expand Down Expand Up @@ -176,6 +179,7 @@ void watch({
{
expression: ['allof', ['match', '*.ts']],
interruptible: false,
name: 'sleep',
onChange: async ({ signal }) => {
await interrupt($`sleep 30`, signal);
},
Expand Down
3 changes: 3 additions & 0 deletions src/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ it('evaluates onChange', async () => {
} as unknown as WatchmanClient;
const trigger = {
id: 'foo',
name: 'foo',
onChange: () => {},
} as unknown as Trigger;

Expand Down Expand Up @@ -82,6 +83,7 @@ it('throws if onChange produces an error', async () => {
} as unknown as WatchmanClient;
const trigger = {
id: 'foo',
name: 'foo',
onChange: () => {},
retry: {
retries: 0,
Expand Down Expand Up @@ -115,6 +117,7 @@ it('retries failing routines', async () => {
} as unknown as WatchmanClient;
const trigger = {
id: 'foo',
name: 'foo',
onChange: () => {},
retry: {
retries: 1,
Expand Down
10 changes: 5 additions & 5 deletions src/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const subscribe = (

if (activeTask) {
if (trigger.interruptible) {
log.warn('aborted task %s', activeTask.id);
log.warn('aborted task %s (%s)', trigger.name, activeTask.id);

if (!activeTask.abortController) {
throw new Error('Expected abort controller to be set');
Expand All @@ -110,7 +110,7 @@ export const subscribe = (

activeTask = null;
} else {
log.warn('waiting for %s task to complete', activeTask.id);
log.warn('waiting for %s (%s) task to complete', trigger.name, activeTask.id);

if (activeTask.queued) {
return;
Expand Down Expand Up @@ -152,13 +152,13 @@ export const subscribe = (
const taskPromise = retry(onChange, {
...trigger.retry,
onFailedAttempt: () => {
log.warn('retrying task %s...', taskId);
log.warn('retrying task %s (%s)...', trigger.name, taskId);
},
})
// eslint-disable-next-line promise/prefer-await-to-then
.then(() => {
if (taskId === activeTask?.id) {
log.trace('completed task %s', activeTask.id);
log.trace('completed task %s (%s)', trigger.name, taskId);

activeTask = null;
}
Expand All @@ -176,7 +176,7 @@ export const subscribe = (
queued: false,
};

log.trace('started task %s', activeTask.id);
log.trace('started task %s (%s)', trigger.name, taskId);
});
});
};
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ type Retry = {

/**
* @property expression watchman expression, e.g. https://facebook.github.io/watchman/docs/expr/allof.html
* @property onChange Routine that is executed when file changes are detected.
* @property interruptible Sends abort signal to an ongoing routine, if any. Otherwise, waits for routine to finish. (default: true)
* @property name Name of the trigger. Used for debugging. Must match /^[a-z0-9-_]+$/ pattern and must be unique.
* @property onChange Routine that is executed when file changes are detected.
*/
type TriggerInput = {
expression: Expression,
interruptible?: boolean,
name: string,
onChange: OnChangeEventHandler,
retry?: Retry,
};
Expand All @@ -158,6 +160,7 @@ export type Trigger = {
expression: Expression,
id: string,
interruptible: boolean,
name: string,
onChange: OnChangeEventHandler,
relativePath: string,
retry: Retry,
Expand Down
1 change: 1 addition & 0 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const watch = (configurationInput: ConfigurationInput) => {
expression: trigger.expression,
id: randomUUID(),
interruptible: trigger.interruptible ?? true,
name: trigger.name,
onChange: trigger.onChange,
relativePath: response.relative_path,
retry: trigger.retry ?? {
Expand Down

0 comments on commit 5b68643

Please sign in to comment.