/models/m_cron_job.erl

https://github.com/helllamer/mod_cron · Erlang · 108 lines · 61 code · 22 blank · 25 comment · 0 complexity · 9023819419987acdd44bef3d3fc21214 MD5 · raw file

  1. %% @author Konstantin Nikiforov <helllamer@gmail.com>
  2. %% @copyright 2011 Konstantin Nikiforov
  3. %% @doc Display current mod_cron status
  4. %% Copyright 2011 Konstantin Nikiforov
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(m_cron_job).
  18. -behaviour(gen_model).
  19. -export([m_find_value/3, m_to_list/2, m_value/2]).
  20. -export([get_task/2, get_all/1, insert/3, update/3, delete/2, manage_schema/2]).
  21. -include("../include/cron.hrl").
  22. -include("zotonic.hrl").
  23. %% @todo
  24. m_find_value(_, _M, _Context) ->
  25. <<"Not yet implemented">>.
  26. m_to_list(_M, _Context) ->
  27. [].
  28. m_value(_M, _Context) ->
  29. undefined.
  30. %% get task from db -> undefined | {ok, Task}
  31. get_task(JobId, Context) ->
  32. case z_db:q1("SELECT task FROM " ++ ?T_CRON_JOB ++ " WHERE id = $1", [JobId], Context) of
  33. undefined -> undefined;
  34. Task -> {ok, binary_to_task(Task)}
  35. end.
  36. %% get all saved jobs
  37. get_all(Context) ->
  38. Result = z_db:q("SELECT id, task FROM " ++ ?T_CRON_JOB, [], Context),
  39. lists:map(fun({JobId,Task}) -> {JobId, binary_to_task(Task)} end, Result).
  40. %% insert/update task
  41. insert(JobId, Task, Context) when is_binary(JobId) ->
  42. case get_task(JobId, Context) of
  43. undefined -> insert_nocheck(JobId, Task, Context);
  44. {ok, _} -> update(JobId, Task, Context)
  45. end;
  46. insert(JobId, Task, Context) ->
  47. insert(z_convert:to_binary(JobId), Task, Context).
  48. insert_nocheck(JobId, Task, Context) ->
  49. Props = [
  50. {id, JobId},
  51. {task, task_to_binary(Task)}
  52. ],
  53. {ok,_} = Result = z_db:insert(?T_CRON_JOB, Props, Context),
  54. z_notifier:notify({cron_job_inserted, JobId, Task}, Context),
  55. Result.
  56. %% update task data for job
  57. update(JobId, Task, Context) when is_binary(JobId) ->
  58. RowsUpdated = z_db:q1("UPDATE " ++ ?T_CRON_JOB ++ " SET task = $2 WHERE id = $1", [JobId, Task], Context),
  59. RowsUpdated > 0 andalso z_notifier:notify({cron_job_inserted, JobId, Task}, Context),
  60. {ok, RowsUpdated};
  61. update(JobId, Task, Context) ->
  62. update(z_convert:to_binary(JobId), Task, Context).
  63. %% @doc delete existing job
  64. delete(JobId, Context) ->
  65. case z_db:delete(?T_CRON_JOB, JobId, Context) of
  66. {ok, RowsDeleted} = Result ->
  67. RowsDeleted > 0 andalso z_notifier:notify({cron_job_deleted, JobId}, Context),
  68. Result;
  69. E -> E
  70. end.
  71. %% @doc serialize/deserialize task data.
  72. task_to_binary(Task) when is_tuple(Task) ->
  73. term_to_binary(Task, [compressed]).
  74. binary_to_task(Binary) when is_binary(Binary) ->
  75. binary_to_term(Binary);
  76. binary_to_task(X) ->
  77. X.
  78. %% @doc create table for persistent tasks, if not exist.
  79. manage_schema(install, Context) ->
  80. z_db:create_table(?T_CRON_JOB, [
  81. #column_def{name=id, type="varchar", is_nullable=false},
  82. #column_def{name=task, type="bytea", is_nullable=false}
  83. ], Context),
  84. % Add some constrains, ignore errors
  85. z_db:equery("ALTER TABLE " ++ ?T_CRON_JOB ++ " ADD PRIMARY KEY (id)", Context),
  86. ok.