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

Add cron for Singapore time zone #6

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/send_alerts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Send Alerts
name: Send alerts for Indian time zone

env:
MIX_ENV: prod
Expand All @@ -25,9 +25,12 @@ jobs:
- name: Install dependencies
run: mix deps.get

- name: compile
run: mix compile

- name: Send hacker news email alerts
shell: bash
env:
GMAIL_USERNAME: ${{ secrets.GMAIL_USERNAME }}
GMAIL_PASSWORD: ${{ secrets.GMAIL_PASSWORD }}
run: mix hacker_news_alert
run: mix hacker_news_alert +5:30
36 changes: 36 additions & 0 deletions .github/workflows/send_alerts_for_singapore_tz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Send alerts for Singapore time zone

env:
MIX_ENV: prod

on:
schedule:
# Everyday at 9 AM
- cron: '00 01 * * *'
# Everyday at 3 PM
- cron: '00 07 * * *'
# Everyday at 9 PM
- cron: '00 13 * * *'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
otp-version: 22.3
elixir-version: 1.10.2

- name: Install dependencies
run: mix deps.get

- name: compile
run: mix compile

- name: Send hacker news email alerts
shell: bash
env:
GMAIL_USERNAME: ${{ secrets.GMAIL_USERNAME }}
GMAIL_PASSWORD: ${{ secrets.GMAIL_PASSWORD }}
run: mix hacker_news_alert +8:00
27 changes: 27 additions & 0 deletions .github/workflows/test_send_alerts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test send alerts

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-elixir@v1
with:
otp-version: 22.3
elixir-version: 1.10.2

- name: Install dependencies
run: mix deps.get

- name: compile
run: mix compile

- name: Send hacker news email alerts for Indian time zone
shell: bash
run: mix hacker_news_alert +5:30

- name: Send hacker news email alerts for Singapore time zone
shell: bash
run: mix hacker_news_alert +8:00
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
elixir 1.10.1
elixir 1.10.2
64 changes: 53 additions & 11 deletions lib/mix/tasks/hacker_news_alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,70 @@ defmodule Mix.Tasks.HackerNewsAlert do

@app_name :hacker_news_alert

def run(_) do
def run([time_zone_arg]) do
{:ok, _} = Application.ensure_all_started(@app_name)
news_items = HackerNews.fetch_top_news()

Path.join([
:code.priv_dir(@app_name),
"static",
"subscribers.txt"
])
subscribers_file_path()
|> File.stream!()
|> Stream.each(fn row ->
[name, email] = String.split(row, ",")
|> Stream.each(fn
"\n" ->
:skip

"#" <> _ ->
:skip

{:ok, _} =
HackerNews.send_top_news_alert(
row ->
[name, email, time_zone] = String.split(row, ",")
name = String.trim(name)
email = String.trim(email)
time_zone = String.trim(time_zone)

send_alert(
Mix.env(),
name,
String.trim(email),
email,
time_zone,
time_zone_arg,
news_items
)

:ok
end)
|> Stream.run()

IO.puts("Emails have been sent successfully.")
end

defp send_alert(:prod, name, email, time_zone, input_time_zone, news_items)
when input_time_zone == time_zone do
{:ok, _} =
HackerNews.send_top_news_alert(
name,
String.trim(email),
news_items
)
end

defp send_alert(env, _name, email, time_zone, input_time_zone, _news_items)
when input_time_zone == time_zone do
IO.puts("Mock sending email to #{email} in #{time_zone} time_zone in #{env} env.")
end

defp send_alert(_env, _name, _email, _time_zone, _input_time_zone, _news_items), do: :ok

defp subscribers_file_path do
file_name =
if Mix.env() == :prod do
"subscribers.txt"
else
"test_subscribers.txt"
end

Path.join([
:code.priv_dir(@app_name),
"static",
file_name
])
end
end
15 changes: 9 additions & 6 deletions priv/static/subscribers.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Vasu Adari,vasuakeel@gmail.com
Shubham Gupta,shubhamgblr@gmail.com
Bhupendra Singh,bhupendra.nitt@gmail.com
Nitin Misra,nitin.misra23@gmail.com
Prateek Gupta, prateek@nearprotocol.com
Srihari K, srih4ri+hnalert@gmail.com
# Indian time zone
Vasu Adari, vasuakeel@gmail.com, +5:30
Bhupendra Singh, bhupendra.nitt@gmail.com, +5:30
Nitin Misra, nitin.misra23@gmail.com, +5:30
Prateek Gupta, prateek@nearprotocol.com, +5:30
Srihari K, srih4ri+hnalert@gmail.com, +5:30

# Singapore time zone
Shubham Gupta, shubhamgblr@gmail.com, +8:00
5 changes: 5 additions & 0 deletions priv/static/test_subscribers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Indian time zone
Vasu Adari, vasuakeel@gmail.com, +5:30

# Singapore time zone
Vasu Adari, vasuakeel@gmail.com, +8:00