<commit_before>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%% @author <NAME> <<EMAIL>>
%% @copyright 2011-2012 Opscode Inc.

-module(oc_chef_wm_sup).

-behaviour(supervisor).

-include_lib("amqp_client/include/amqp_client.hrl").

%% External exports
-export([start_link/0, upgrade/0]).

%% supervisor callbacks
-export([init/1]).

-include_lib("oc_chef_wm.hrl").

%% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor.
start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% @spec upgrade() -> ok
%% @doc Add processes if necessary.
upgrade() ->
    {ok, {_, Specs}} = init([]),

    Old = sets:from_list(
            [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
    Kill = sets:subtract(Old, New),

    sets:fold(fun (Id, ok) ->
                      supervisor:terminate_child(?MODULE, Id),
                      supervisor:delete_child(?MODULE, Id),
                      ok
              end, ok, Kill),

    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
    ok.

%% @spec init([]) -> SupervisorTree
%% @doc ervisor callback.
init([]) ->
    ok = load_ibrowse_config(),
    ok = enable_org_cache(),

    Action = envy:get(oc_chef_wm, enable_actions, false, boolean),

    Ip = envy:get(oc_chef_wm, ip, string),
    Port = envy:get(oc_chef_wm, port, pos_integer),
    WebConfig = [
                 {ip, Ip},
                 {port, Port},
                 {log_dir, "priv/log"},
                 {dispatch, dispatch_table()}],

    Web = {webmachine_mochiweb,
           {webmachine_mochiweb, start, [WebConfig]},
           permanent, 5000, worker, dynamic},

    KeyRing = {chef_keyring,
               {chef_keyring, start_link, []},
               permanent, brutal_kill, worker, [chef_keyring]},

    KeyGenWorkerSup = {chef_keygen_worker_sup,
                       {chef_keygen_worker_sup, start_link, []},
                       permanent, 5000, supervisor, [chef_keygen_worker_sup]},

    KeyCache = {chef_keygen_cache,
                {chef_keygen_cache, start_link, []},
                permanent, 5000, worker, [chef_keygen_cache]},

    Index = {chef_index_sup,
             {chef_index_sup, start_link, []},
             permanent, 5000, supervisor, [chef_index_sup]},

    {ok, { {one_for_one, 10, 10}, maybe_start_action(Action, [KeyRing,
                                                              Index,
                                                              KeyGenWorkerSup,
                                                              KeyCache,
                                                              Web])}}.

maybe_start_action(true, Workers) ->
    lager:info("Starting oc_chef_action", []),
    [amqp_child_spec() | Workers];
maybe_start_action(false, Workers) ->
    lager:info("Not starting Actionlog supervisor since actionlog is disabled."),
    Workers.

load_ibrowse_config() ->
    %% FIXME: location of the ibrowse.config should be itself configurable. Also need to
    %% revisit what's in that config to ensure it is as useful as possible.
    ConfigFile = filename:absname(filename:join(["etc", "ibrowse", "ibrowse.config"])),
    lager:info("Loading ibrowse configuration from ~s~n", [ConfigFile]),
    ok = ibrowse:rescan_config(ConfigFile),
    ok.

enable_org_cache() ->
    %% FIXME: should this config live at the oc_chef_wm level?
    case envy:get(chef_db, cache_defaults, undefined, any) of
        undefined ->
            lager:info("Org guid cache disabled~n");
        _Defaults ->
            chef_cache:init(org_guid),
            lager:info("Org guid cache enabled~n")
    end,
    ok.

dispatch_table() ->
    {ok, Dispatch} = file:consult(filename:join(<commit_msg>Correct tab name to reflect usage in chef_db<commit_after>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%% @author <NAME> <<EMAIL>>
%% @copyright 2011-2012 Opscode Inc.

-module(oc_chef_wm_sup).

-behaviour(supervisor).

-include_lib("amqp_client/include/amqp_client.hrl").

%% External exports
-export([start_link/0, upgrade/0]).

%% supervisor callbacks
-export([init/1]).

-include_lib("oc_chef_wm.hrl").

%% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor.
start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% @spec upgrade() -> ok
%% @doc Add processes if necessary.
upgrade() ->
    {ok, {_, Specs}} = init([]),

    Old = sets:from_list(
            [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
    New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
    Kill = sets:subtract(Old, New),

    sets:fold(fun (Id, ok) ->
                      supervisor:terminate_child(?MODULE, Id),
                      supervisor:delete_child(?MODULE, Id),
                      ok
              end, ok, Kill),

    [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
    ok.

%% @spec init([]) -> SupervisorTree
%% @doc ervisor callback.
init([]) ->
    ok = load_ibrowse_config(),
    ok = enable_org_cache(),

    Action = envy:get(oc_chef_wm, enable_actions, false, boolean),

    Ip = envy:get(oc_chef_wm, ip, string),
    Port = envy:get(oc_chef_wm, port, pos_integer),
    WebConfig = [
                 {ip, Ip},
                 {port, Port},
                 {log_dir, "priv/log"},
                 {dispatch, dispatch_table()}],

    Web = {webmachine_mochiweb,
           {webmachine_mochiweb, start, [WebConfig]},
           permanent, 5000, worker, dynamic},

    KeyRing = {chef_keyring,
               {chef_keyring, start_link, []},
               permanent, brutal_kill, worker, [chef_keyring]},

    KeyGenWorkerSup = {chef_keygen_worker_sup,
                       {chef_keygen_worker_sup, start_link, []},
                       permanent, 5000, supervisor, [chef_keygen_worker_sup]},

    KeyCache = {chef_keygen_cache,
                {chef_keygen_cache, start_link, []},
                permanent, 5000, worker, [chef_keygen_cache]},

    Index = {chef_index_sup,
             {chef_index_sup, start_link, []},
             permanent, 5000, supervisor, [chef_index_sup]},

    {ok, { {one_for_one, 10, 10}, maybe_start_action(Action, [KeyRing,
                                                              Index,
                                                              KeyGenWorkerSup,
                                                              KeyCache,
                                                              Web])}}.

maybe_start_action(true, Workers) ->
    lager:info("Starting oc_chef_action", []),
    [amqp_child_spec() | Workers];
maybe_start_action(false, Workers) ->
    lager:info("Not starting Actionlog supervisor since actionlog is disabled."),
    Workers.

load_ibrowse_config() ->
    %% FIXME: location of the ibrowse.config should be itself configurable. Also need to
    %% revisit what's in that config to ensure it is as useful as possible.
    ConfigFile = filename:absname(filename:join(["etc", "ibrowse", "ibrowse.config"])),
    lager:info("Loading ibrowse configuration from ~s~n", [ConfigFile]),
    ok = ibrowse:rescan_config(ConfigFile),
    ok.

enable_org_cache() ->
    %% FIXME: should this config live at the oc_chef_wm level?
    case envy:get(chef_db, cache_defaults, undefined, any) of
        undefined ->
            lager:info("Org guid cache disabled~n");
        _Defaults ->
            chef_cache:init(org_metadata),
            lager:info("Org guid cache enabled~n")
    end,
    ok.

dispatch_table() ->
    {ok, Dispatch} = file:consult(filename:join(
{'fraction_non_alphanumeric': 0.136676627257373, 'fraction_numerical': 0.009354293880732754, 'mean_word_length': 2.286934244235696, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7184174', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2360, 'n_words': 514}