Skip to content

Commit

Permalink
Add oddjobs background runner, add readme builder (#116)
Browse files Browse the repository at this point in the history
This adds oddjobs, and the readme job which will build a readme
in the background by pulling it from hackage, putting it through
pandoc and then rendering it.

The job is started from the admin-ui.
There is now also a link to the odd-jobs user interface
which gives an overview of all running and failed jobs.

Readme is used as an example of how to build background tasks.

Note that normally I'd put the job runner in a seperate process,
which is mounted in systemd, but I'm unsure how to do this with
the current system.
It /should/ work fine as a thread as well.
But the process separation makes ops easier.
I think mounting the UI in servant like this is fantastic!
  • Loading branch information
jappeace authored Jun 20, 2022
1 parent d30d8e7 commit 59540bd
Show file tree
Hide file tree
Showing 30 changed files with 766 additions and 200 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ endif
.PHONY: all $(MAKECMDGOALS)

.DEFAULT_GOAL := help

.PHONY: migration
migration: ## Generate timestamped database migration boilerplate files
@if test -z "$$name"; then \
echo "Usage: make migration name=some-name"; \
else \
migName="`date -u '+%Y%m%d%H%M%S'`_$$name"; \
fname="migrations/$$migName.sql"; \
touch "$$fname"; \
echo "Touched $$fname";\
fi
2 changes: 1 addition & 1 deletion app/cli/Main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Main where

import Data.Password.Types
import Database.PostgreSQL.Entity.DBT
import Optics.Core
import Options.Applicative

Expand All @@ -14,6 +13,7 @@ import Control.Exception
import CoverageReport
import Data.Maybe
import Data.Text (Text)
import Database.PostgreSQL.Entity.DBT
import Database.PostgreSQL.Simple.Errors (ConstraintViolation (..), catchViolation)
import DesignSystem (generateComponents)
import Flora.Import.Package.Bulk (importAllFilesInRelativeDirectory)
Expand Down
3 changes: 3 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ div[class="bullets"] {
.category-card:hover {
@apply bg-gray-7 dark:bg-blue-1;
}
.package-readme-column{
margin: 0 2em;
}

ul.package-list a {
@apply py-3 pl-4;
Expand Down
13 changes: 8 additions & 5 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ source-repository-package
tag: 9dacea2bec9c6f5d9f7d46a2a1d9094cf6147fbf
subdir: ./raven-haskell

source-repository-package
type: git
location: https://github.com/scrive/pool
tag: b0cfae3

source-repository-package
type: git
location: https://github.com/supki/envparse
Expand Down Expand Up @@ -58,3 +53,11 @@ source-repository-package
location: https://github.com/scrive/log
tag: 73b4735
subdir: ./log-base

-- need to use jappeace until this is merged
-- (provides resource-pool 3 support)
-- https://github.com/saurabhnanda/odd-jobs/pull/90
source-repository-package
type: git
location: https://github.com/jappeace/odd-jobs
tag: a75515791f2c743614ec05d54493ef12b143002e
15 changes: 13 additions & 2 deletions flora.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ common common-extensions

common common-ghc-options
ghc-options:
-Wall -Wcompat -Werror -Widentities -Wincomplete-record-updates
-Wall -Wcompat -Widentities -Wincomplete-record-updates
-Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints
-fhide-source-paths -Wno-unused-do-bind -fshow-hole-constraints

Expand All @@ -84,6 +84,8 @@ library
Data.Aeson.Orphans
Data.Text.Display.Orphans
Flora.Environment
Flora.Environment.Config
Flora.Environment.OddJobs
Flora.Import.Categories
Flora.Import.Categories.Tuning
Flora.Import.Package
Expand Down Expand Up @@ -113,6 +115,7 @@ library
Flora.Model.User.Orphans
Flora.Model.User.Query
Flora.Model.User.Update
Flora.OddJobs
Flora.Publish
Flora.Search
Flora.ThirdParties.Hackage.API
Expand Down Expand Up @@ -182,15 +185,18 @@ library
, clock ^>=0.8
, cmark-gfm ^>=0.2
, colourista ^>=0.1
, commonmark
, containers ^>=0.6
, cookie ^>=0.4
, cryptohash ^>=0.11
, data-default
, directory ^>=1.3
, envparse ^>=0.5
, filepath ^>=1.4
, http-api-data ^>=0.4
, http-client ==0.7.10
, http-types ^>=0.12
, lens
, log-base ^>=0.11
, lucid ^>=2.11
, lucid-alpine ^>=0.1
Expand All @@ -199,6 +205,7 @@ library
, monad-control ^>=1.0
, monad-time ^>=0.3
, mtl ^>=2.2
, odd-jobs
, optics-core ^>=0.4
, optparse-applicative ^>=0.16
, password ^>=3.0
Expand All @@ -214,7 +221,8 @@ library
, prometheus-proc ^>=0.1
, PyF ^>=0.10
, raven-haskell ^>=0.1
, resource-pool ^>=0.2
, resource-pool >=0.3 && <0.4.0.0
, safe-exceptions
, servant ^>=0.19
, servant-client ^>=0.19
, servant-client-core ^>=0.19
Expand Down Expand Up @@ -242,6 +250,7 @@ library
, wai-middleware-prometheus ^>=1.0
, warp ^>=3.3
, websockets ^>=0.12
, wreq >=0.5.3.3

executable flora-server
import: common-extensions
Expand Down Expand Up @@ -297,6 +306,7 @@ test-suite flora-test
main-is: Main.hs
hs-source-dirs: test
build-depends:
, aeson
, base
, bytestring
, Cabal
Expand Down Expand Up @@ -329,6 +339,7 @@ test-suite flora-test
other-modules:
Flora.CabalSpec
Flora.CategorySpec
Flora.OddJobSpec
Flora.PackageSpec
Flora.TemplateSpec
Flora.TestUtils
Expand Down
113 changes: 113 additions & 0 deletions migrations/20220531232510_create_oddjobs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
--
-- Name: notify_job_monitor_for_oddjobs(); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.notify_job_monitor_for_oddjobs ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM
pg_notify(
'"jobs_created_oddjobs"',
json_build_object(
'id', NEW.id,
'run_at', NEW.run_at,
'locked_at', NEW.locked_at
)::text
);
RETURN new;
END;
$$;

ALTER FUNCTION public.notify_job_monitor_for_oddjobs () OWNER TO postgres;

--
-- Name: oddjobs; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.oddjobs (
id integer NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
run_at timestamp with time zone DEFAULT now() NOT NULL,
status text NOT NULL,
payload jsonb NOT NULL,
last_error jsonb,
attempts integer DEFAULT 0 NOT NULL,
locked_at timestamp with time zone,
locked_by text,
CONSTRAINT incorrect_locking_info
CHECK ((((status <> 'locked'::text)
AND (locked_at IS NULL)
AND (locked_by IS NULL))
OR ((status = 'locked'::text)
AND (locked_at IS NOT NULL)
AND (locked_by IS NOT NULL))))
);

ALTER TABLE public.oddjobs OWNER TO postgres;

--
-- Name: oddjobs_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.oddjobs_id_seq
AS integer START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER TABLE public.oddjobs_id_seq OWNER TO postgres;

--
-- Name: oddjobs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.oddjobs_id_seq OWNED BY public.oddjobs.id;

--
-- Name: oddjobs id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.oddjobs
ALTER COLUMN id SET DEFAULT nextval('public.oddjobs_id_seq'::regclass);

-- Name: oddjobs oddjobs_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.oddjobs
ADD CONSTRAINT oddjobs_pkey PRIMARY KEY (id);

--
-- Name: idx_oddjobs_created_at; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_created_at ON public.oddjobs USING btree (created_at);

--
-- Name: idx_oddjobs_locked_at; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_locked_at ON public.oddjobs USING btree (locked_at);

--
-- Name: idx_oddjobs_locked_by; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_locked_by ON public.oddjobs USING btree (locked_by);

--
-- Name: idx_oddjobs_run_at; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_run_at ON public.oddjobs USING btree (run_at);

--
-- Name: idx_oddjobs_status; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_status ON public.oddjobs USING btree (status);

--
-- Name: idx_oddjobs_updated_at; Type: INDEX; Schema: public; Owner: postgres
--
CREATE INDEX idx_oddjobs_updated_at ON public.oddjobs USING btree (updated_at);

--
-- Name: oddjobs trg_notify_job_monitor_for_oddjobs; Type: TRIGGER; Schema: public; Owner: postgres
--
CREATE TRIGGER trg_notify_job_monitor_for_oddjobs
AFTER INSERT ON public.oddjobs
FOR EACH ROW
EXECUTE FUNCTION public.notify_job_monitor_for_oddjobs ();
1 change: 1 addition & 0 deletions migrations/20220605185406_add-html-column-to-releases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table releases add column readme text;
6 changes: 6 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pkgs.mkShell rec {
pkgs.yarn
pkgs.libffi
pkgs.zlib
(pkgs.haskell.lib.dontCheck
(pkgs.haskell.packages."ghc921".callHackageDirect {
pkg = "fourmolu";
ver = "0.7.0.1";
sha256 = "0wrcmd7v0sfyagiwqxnh117xqikid3hfz2vkxzihywx0ld7jp780";
} { }))
(import ./nix/pin2.nix { }).souffle
];
exactDeps = true;
Expand Down
Loading

0 comments on commit 59540bd

Please sign in to comment.