Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create jobs in the database using triggers #68

Closed
christiaanwesterbeek opened this issue Mar 19, 2018 · 2 comments
Closed

Create jobs in the database using triggers #68

christiaanwesterbeek opened this issue Mar 19, 2018 · 2 comments

Comments

@christiaanwesterbeek
Copy link

I would like to create jobs from within the database and have boss workers process them (sending emails and such). Ideally, I would use a trigger on some table that creates the job (based on rules). Can I create a job with SQL in a trigger?

@timgit
Copy link
Owner

timgit commented Mar 20, 2018

👍 I have defaults defined for most fields, so unless you need to change them, they can be excluded from COPY or INSERT.

@timgit timgit closed this as completed Apr 6, 2018
@bradleyayers
Copy link
Contributor

bradleyayers commented Jul 16, 2018

I've gone this route (queuing jobs via a trigger) and have had a great success with it. I use singletonkey and startin (where startin is the throttle window). I'm using it to update my search index when records change that would affect the index. It's turned out to be extremely easy to setup!

In case it's helpful to others, here's what I'm using:

create or replace function dovetail_private.queuenoteforsearchindexing(note_id uuid) returns void as $$
  insert into dovetail_pgboss.job (id, name, startin, singletonkey, data)
  values
    (
      uuid_generate_v1(),
      'SEARCH_INDEXING_INDEX_NOTE',
      '10 seconds'::interval,
      -- Using the note ID as the singletonkey serves to debounce based on note.
      $1::text,
      json_build_object('noteId', $1::text)
    )
  on conflict do nothing
$$ language sql strict security definer;

create function dovetail_private.note$queuesearchindexing() returns trigger as $$
begin
  perform dovetail_private.queuenoteforsearchindexing(new.id);
  -- result is ignored since this is an AFTER trigger
  return null;
end;
$$ language plpgsql strict;

create trigger afterupdate050$queuesearchindexing after update
  on dovetail.note
  for each row
  when (
    new.title is distinct from old.title
    or new.deleted is distinct from old.deleted
    or new.type is distinct from old.type
    or new.content_version is distinct from old.content_version
    or new.ranges::text is distinct from old.ranges::text
  )
  execute procedure dovetail_private.note$queuesearchindexing();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants